1414
1515
1616class StaxAuth :
17- def __init__ (self , config_branch , max_retries : int = 3 ):
18- config = StaxConfig . api_config
19-
20- self .identity_pool = config .get (config_branch ).get ("identityPoolId" )
21- self .user_pool = config .get (config_branch ).get ("userPoolId" )
22- self .client_id = config .get (config_branch ).get ("userPoolWebClientId" )
23- self .aws_region = config .get (config_branch ).get ("region" )
17+ def __init__ (self , config_branch : str , config : StaxConfig , max_retries : int = 3 ):
18+ self . config = config
19+ api_config = self . config . api_config
20+ self .identity_pool = api_config .get (config_branch ).get ("identityPoolId" )
21+ self .user_pool = api_config .get (config_branch ).get ("userPoolId" )
22+ self .client_id = api_config .get (config_branch ).get ("userPoolWebClientId" )
23+ self .aws_region = api_config .get (config_branch ).get ("region" )
2424 self .max_retries = max_retries
2525
26- def requests_auth (self , username , password , ** kwargs ):
26+ def requests_auth (self , ** kwargs ):
27+ username = self .config .access_key
28+ password = self .config .secret_key
2729 if username is None :
2830 raise InvalidCredentialsException (
2931 "Please provide an Access Key to your config"
@@ -37,10 +39,10 @@ def requests_auth(self, username, password, **kwargs):
3739 id_creds = self .sts_from_cognito_identity_pool (id_token , ** kwargs )
3840 auth = self .sigv4_signed_auth_headers (id_creds )
3941
40- StaxConfig .expiration = id_creds .get ("Credentials" ).get ("Expiration" )
41- StaxConfig .auth = auth
42+ self . config .expiration = id_creds .get ("Credentials" ).get ("Expiration" )
43+ self . config .auth = auth
4244
43- return StaxConfig .auth
45+ return self . config .auth
4446
4547 def id_token_from_cognito (
4648 self , username = None , password = None , srp_client = None , ** kwargs
@@ -59,6 +61,7 @@ def id_token_from_cognito(
5961 client_id = self .client_id ,
6062 client = srp_client ,
6163 )
64+
6265 try :
6366 tokens = aws .authenticate_user ()
6467 except ClientError as e :
@@ -69,7 +72,7 @@ def id_token_from_cognito(
6972 elif e .response ["Error" ]["Code" ] == "UserNotFoundException" :
7073 raise InvalidCredentialsException (
7174 message = str (e ),
72- detail = "Please check your Access Key, that you have created your Api Token and that you are using the right STAX REGION" ,
75+ detail = f "Please check your Access Key, that you have created your Api Token and that you are using the right STAX REGION" ,
7376 )
7477 else :
7578 raise InvalidCredentialsException (
@@ -121,7 +124,7 @@ def sigv4_signed_auth_headers(self, id_creds):
121124 aws_access_key = id_creds .get ("Credentials" ).get ("AccessKeyId" ),
122125 aws_secret_access_key = id_creds .get ("Credentials" ).get ("SecretKey" ),
123126 aws_token = id_creds .get ("Credentials" ).get ("SessionToken" ),
124- aws_host = f"{ StaxConfig .hostname } " ,
127+ aws_host = f"{ self . config .hostname } " ,
125128 aws_region = self .aws_region ,
126129 aws_service = "execute-api" ,
127130 )
@@ -133,17 +136,20 @@ class RootAuth:
133136 def requests_auth (username , password , ** kwargs ):
134137 if StaxConfig .expiration and StaxConfig .expiration > datetime .now (timezone .utc ):
135138 return StaxConfig .auth
136-
137- return StaxAuth ("JumaAuth" ).requests_auth (username , password , ** kwargs )
139+ config = StaxConfig .GetDefaultConfig ()
140+ config .init ()
141+ config .access_key = username
142+ config .secret_key = password
143+ return StaxAuth ("JumaAuth" , config ).requests_auth (** kwargs )
138144
139145
140146class ApiTokenAuth :
141147 @staticmethod
142- def requests_auth (username , password , ** kwargs ):
148+ def requests_auth (config : StaxConfig , ** kwargs ):
143149 # Minimize the potentical for token to expire while still being used for auth (say within a lambda function)
144- if StaxConfig .expiration and StaxConfig .expiration - timedelta (
150+ if config .expiration and config .expiration - timedelta (
145151 minutes = int (environ .get ("TOKEN_EXPIRY_THRESHOLD_IN_MINS" , 1 ))
146152 ) > datetime .now (timezone .utc ):
147- return StaxConfig .auth
153+ return config .auth
148154
149- return StaxAuth ("ApiAuth" ).requests_auth (username , password , ** kwargs )
155+ return StaxAuth ("ApiAuth" , config ).requests_auth (** kwargs )
0 commit comments