@@ -9,10 +9,8 @@ use thiserror::Error;
99pub enum EnterpriseCheckoutError {
1010 #[ error( "enterprise server returned error: {0}" ) ]
1111 ServerError ( String ) ,
12- #[ error( "no username set for credential authentication" ) ]
13- NoUsername ,
14- #[ error( "no password set for credential authentication" ) ]
15- NoPassword ,
12+ #[ error( "no credentials set for authentication" ) ]
13+ NoCredentials ,
1614 #[ error( "failed to authenticate with username and password" ) ]
1715 NotAuthenticated ,
1816 #[ error( "failed to refresh expired license: {0}" ) ]
@@ -59,16 +57,36 @@ pub fn checkout_license(
5957
6058 #[ allow( clippy:: collapsible_if) ]
6159 if !is_server_authenticated ( ) {
62- // We have yet to authenticate with the server, we should try all available authentication methods.
63- if !authenticate_server_with_method ( "Keychain" , false ) {
60+ ' auth: {
61+ // We have yet to authenticate with the server, we should try all available authentication methods.
62+ if authenticate_server_with_method ( "Keychain" , false ) {
63+ break ' auth;
64+ }
65+
6466 // We could not authenticate with the system keychain, we should try with credentials.
65- let username = std:: env:: var ( "BN_ENTERPRISE_USERNAME" )
66- . map_err ( |_| EnterpriseCheckoutError :: NoUsername ) ?;
67- let password = std:: env:: var ( "BN_ENTERPRISE_PASSWORD" )
68- . map_err ( |_| EnterpriseCheckoutError :: NoPassword ) ?;
69- if !authenticate_server_with_credentials ( & username, & password, true ) {
70- return Err ( EnterpriseCheckoutError :: NotAuthenticated ) ;
67+ let username = std:: env:: var ( "BN_ENTERPRISE_USERNAME" ) ;
68+ let password = std:: env:: var ( "BN_ENTERPRISE_PASSWORD" ) ;
69+ if let Ok ( username) = username {
70+ if let Ok ( password) = password {
71+ // Having creds that don't work is a hard error
72+ if !authenticate_server_with_credentials ( & username, & password, true ) {
73+ return Err ( EnterpriseCheckoutError :: NotAuthenticated ) ;
74+ }
75+ // Otherwise, if the creds worked, we got auth
76+ break ' auth;
77+ }
78+ }
79+
80+ let token = std:: env:: var ( "BN_ENTERPRISE_TOKEN" ) ;
81+ if let Ok ( token) = token {
82+ if !authenticate_server_with_token ( & token, true ) {
83+ return Err ( EnterpriseCheckoutError :: NotAuthenticated ) ;
84+ }
85+ break ' auth;
7186 }
87+
88+ // If we're still here, we don't have any credentials for authentication.
89+ return Err ( EnterpriseCheckoutError :: NoCredentials ) ;
7290 }
7391 }
7492 }
@@ -183,6 +201,16 @@ pub fn is_server_license_still_activated() -> bool {
183201 unsafe { binaryninjacore_sys:: BNIsEnterpriseServerLicenseStillActivated ( ) }
184202}
185203
204+ pub fn authenticate_server_with_token ( token : & str , remember : bool ) -> bool {
205+ let token = token. to_cstr ( ) ;
206+ unsafe {
207+ binaryninjacore_sys:: BNAuthenticateEnterpriseServerWithToken (
208+ token. as_ref ( ) . as_ptr ( ) as * const std:: os:: raw:: c_char ,
209+ remember,
210+ )
211+ }
212+ }
213+
186214pub fn authenticate_server_with_credentials (
187215 username : & str ,
188216 password : & str ,
0 commit comments