2323 help = "Disable verifying server certificate, use only for testing" ,
2424 is_flag = True ,
2525)
26- @click .option (
27- "--email" , help = "Email to authenticate on the ONEKEY platform" , required = True
28- )
26+ @click .option ("--email" , help = "Email to authenticate on the ONEKEY platform" )
2927@click .option (
3028 "--password" ,
3129 hide_input = True ,
32- required = True ,
33- prompt = True ,
3430 help = "Password to authenticate on the ONEKEY platform" ,
3531)
36- @click .option (
37- "--tenant" , "tenant_name" , required = True , help = "Tenant name on ONEKEY platform"
38- )
32+ @click .option ("--tenant" , "tenant_name" , help = "Tenant name on ONEKEY platform" )
33+ @click .option ("--token" , help = "API token to authenticate on the ONEKEY platform" )
3934@click .pass_context
40- def cli (ctx , api_url , disable_tls_verify , email , password , tenant_name ):
35+ def cli (ctx , api_url , disable_tls_verify , email , password , tenant_name , token ):
4136 client = Client (api_url = api_url , disable_tls_verify = disable_tls_verify )
37+ if token is not None and (
38+ email is not None or password is not None or tenant_name is not None
39+ ):
40+ click .echo (
41+ "Invalid authentication details, either specify token or email/password/tenant, but not both!"
42+ )
43+ sys .exit (1 )
44+
45+ if token is None and (email is None or password is None or tenant_name is None ):
46+ click .echo (
47+ "Invalid authentication details, specify email, password and tenant, if token is not specified!"
48+ )
49+ sys .exit (1 )
50+
51+ if token is not None :
52+ login_with_token (client , token , api_url )
53+ else :
54+ login_with_email (client , email , password , tenant_name , api_url )
55+ ctx .obj = client
56+
57+
58+ def login_with_email (client , email , password , tenant_name , api_url ):
4259 try :
4360 client .login (email , password )
4461 except httpx .HTTPStatusError as e :
@@ -62,7 +79,20 @@ def cli(ctx, api_url, disable_tls_verify, email, password, tenant_name):
6279 sys .exit (3 )
6380
6481 client .use_tenant (tenant )
65- ctx .obj = client
82+
83+
84+ def login_with_token (client , token , api_url ):
85+ try :
86+ client .use_token (token )
87+ except httpx .HTTPStatusError as e :
88+ if e .response .status_code == httpx .codes .UNAUTHORIZED :
89+ click .echo (f"Authentication failed with token on { api_url } " )
90+ sys .exit (1 )
91+ else :
92+ click .echo (
93+ f"Error connecting to ONEKEY platform: '{ api_url } ', error: { e .response .status_code } "
94+ )
95+ sys .exit (2 )
6696
6797
6898cli .add_command (list_tenants )
0 commit comments