@@ -59,16 +59,36 @@ pub fn checkout_license(
5959
6060 #[ allow( clippy:: collapsible_if) ]
6161 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 ) {
62+ ' auth: {
63+ // We have yet to authenticate with the server, we should try all available authentication methods.
64+ if authenticate_server_with_method ( "Keychain" , false ) {
65+ break ' auth;
66+ }
67+
6468 // 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 ) ;
69+ let username = std:: env:: var ( "BN_ENTERPRISE_USERNAME" ) ;
70+ let password = std:: env:: var ( "BN_ENTERPRISE_PASSWORD" ) ;
71+ if let Ok ( username) = username {
72+ if let Ok ( password) = password {
73+ // Having creds that don't work is a hard error
74+ if !authenticate_server_with_credentials ( & username, & password, true ) {
75+ return Err ( EnterpriseCheckoutError :: NotAuthenticated ) ;
76+ }
77+ // Otherwise, if the creds worked, we got auth
78+ break ' auth;
79+ }
80+ }
81+
82+ let token = std:: env:: var ( "BN_ENTERPRISE_TOKEN" ) ;
83+ if let Ok ( token) = token {
84+ if !authenticate_server_with_token ( & token, true ) {
85+ return Err ( EnterpriseCheckoutError :: NotAuthenticated ) ;
86+ }
87+ break ' auth;
7188 }
89+
90+ // If we're still here, auth has failed.
91+ return Err ( EnterpriseCheckoutError :: NotAuthenticated ) ;
7292 }
7393 }
7494 }
@@ -183,6 +203,16 @@ pub fn is_server_license_still_activated() -> bool {
183203 unsafe { binaryninjacore_sys:: BNIsEnterpriseServerLicenseStillActivated ( ) }
184204}
185205
206+ pub fn authenticate_server_with_token ( token : & str , remember : bool ) -> bool {
207+ let token = token. to_cstr ( ) ;
208+ unsafe {
209+ binaryninjacore_sys:: BNAuthenticateEnterpriseServerWithToken (
210+ token. as_ref ( ) . as_ptr ( ) as * const std:: os:: raw:: c_char ,
211+ remember,
212+ )
213+ }
214+ }
215+
186216pub fn authenticate_server_with_credentials (
187217 username : & str ,
188218 password : & str ,
0 commit comments