11#!/usr/local/bin/python3
2- import logging
3- import sys
4- from datetime import date , datetime , timedelta , timezone
2+ from datetime import datetime , timezone
53
64import boto3
7- import jwt
85from aws_requests_auth .aws_auth import AWSRequestsAuth
96from botocore import UNSIGNED
107from botocore .client import Config as BotoConfig
118from botocore .exceptions import ClientError
12- from warrant import AWSSRP , Cognito
9+ from warrant import AWSSRP
1310
14- from staxapp .config import Config as JumaConfig
11+ from staxapp .config import Config as StaxConfig
1512from staxapp .exceptions import InvalidCredentialsException
1613
1714
1815class StaxAuth :
1916 def __init__ (self , config_branch ):
20- config = JumaConfig .api_config
17+ config = StaxConfig .api_config
2118
2219 self .identity_pool = config .get (config_branch ).get ("identityPoolId" )
2320 self .user_pool = config .get (config_branch ).get ("userPoolId" )
2421 self .client_id = config .get (config_branch ).get ("userPoolWebClientId" )
2522 self .aws_region = config .get (config_branch ).get ("region" )
2623
27- def requests_auth (self , username , password ):
24+ def requests_auth (self , username , password , ** kwargs ):
2825 if username is None :
2926 raise InvalidCredentialsException (
3027 "Please provide an Access Key to your config"
@@ -34,51 +31,52 @@ def requests_auth(self, username, password):
3431 "Please provide a Secret Key to your config"
3532 )
3633
37- id_token = self .id_token_from_cognito (username , password )
38- id_creds = self .sts_from_cognito_identity_pool (id_token )
34+ id_token = self .id_token_from_cognito (username , password , ** kwargs )
35+ id_creds = self .sts_from_cognito_identity_pool (id_token , ** kwargs )
3936 auth = self .sigv4_signed_auth_headers (id_creds )
4037
41- JumaConfig .expiration = id_creds .get ("Credentials" ).get ("Expiration" )
42- JumaConfig .auth = auth
38+ StaxConfig .expiration = id_creds .get ("Credentials" ).get ("Expiration" )
39+ StaxConfig .auth = auth
4340
44- return JumaConfig .auth
41+ return StaxConfig .auth
4542
46- def id_token_from_cognito (self , username = None , password = None , client = None ):
43+ def id_token_from_cognito (
44+ self , username = None , password = None , srp_client = None , ** kwargs
45+ ):
4746 token = None
48- if username and password :
49- if not client :
50- client = boto3 .client (
51- "cognito-idp" ,
52- region_name = self .aws_region ,
53- config = BotoConfig (signature_version = UNSIGNED ),
54- )
55- aws = AWSSRP (
56- username = username ,
57- password = password ,
58- pool_id = self .user_pool ,
59- client_id = self .client_id ,
60- client = client ,
47+ if not srp_client :
48+ srp_client = boto3 .client (
49+ "cognito-idp" ,
50+ region_name = self .aws_region ,
51+ config = BotoConfig (signature_version = UNSIGNED ),
6152 )
62- try :
63- tokens = aws .authenticate_user ()
64- except ClientError as e :
65- if e .response ["Error" ]["Code" ] == "NotAuthorizedException" :
66- raise InvalidCredentialsException (
67- message = str (e ), detail = "Please check your Secret Key is correct"
68- )
69- elif e .response ["Error" ]["Code" ] == "UserNotFoundException" :
70- raise InvalidCredentialsException (
71- 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" ,
73- )
74- else :
75- raise InvalidCredentialsException (
76- f"Unexpected Client Error. Error details: { e } "
77- )
78- token = tokens ["AuthenticationResult" ]["IdToken" ]
53+ aws = AWSSRP (
54+ username = username ,
55+ password = password ,
56+ pool_id = self .user_pool ,
57+ client_id = self .client_id ,
58+ client = srp_client ,
59+ )
60+ try :
61+ tokens = aws .authenticate_user ()
62+ except ClientError as e :
63+ if e .response ["Error" ]["Code" ] == "NotAuthorizedException" :
64+ raise InvalidCredentialsException (
65+ message = str (e ), detail = "Please check your Secret Key is correct"
66+ )
67+ elif e .response ["Error" ]["Code" ] == "UserNotFoundException" :
68+ raise InvalidCredentialsException (
69+ message = str (e ),
70+ detail = "Please check your Access Key, that you have created your Api Token and that you are using the right STAX REGION" ,
71+ )
72+ else :
73+ raise InvalidCredentialsException (
74+ f"Unexpected Client Error. Error details: { e } "
75+ )
76+ token = tokens ["AuthenticationResult" ]["IdToken" ]
7977 return token
8078
81- def sts_from_cognito_identity_pool (self , token , cognito_client = None ):
79+ def sts_from_cognito_identity_pool (self , token , cognito_client = None , ** kwargs ):
8280 if not cognito_client :
8381 cognito_client = boto3 .client (
8482 "cognito-identity" ,
@@ -91,43 +89,39 @@ def sts_from_cognito_identity_pool(self, token, cognito_client=None):
9189 f"cognito-idp.{ self .aws_region } .amazonaws.com/{ self .user_pool } " : token
9290 },
9391 )
94- # logging.debug(f"ID: {id}")
95-
9692 id_creds = cognito_client .get_credentials_for_identity (
9793 IdentityId = id ["IdentityId" ],
9894 Logins = {
9995 f"cognito-idp.{ self .aws_region } .amazonaws.com/{ self .user_pool } " : token
10096 },
10197 )
102- # logging.debug(f"CREDS: {id_creds}")
10398 return id_creds
10499
105100 def sigv4_signed_auth_headers (self , id_creds ):
106101 auth = AWSRequestsAuth (
107102 aws_access_key = id_creds .get ("Credentials" ).get ("AccessKeyId" ),
108103 aws_secret_access_key = id_creds .get ("Credentials" ).get ("SecretKey" ),
109104 aws_token = id_creds .get ("Credentials" ).get ("SessionToken" ),
110- aws_host = f"{ JumaConfig .hostname } " ,
105+ aws_host = f"{ StaxConfig .hostname } " ,
111106 aws_region = self .aws_region ,
112107 aws_service = "execute-api" ,
113108 )
114- # logging.debug(f"AUTH: {auth}")
115109 return auth
116110
117111
118112class RootAuth :
119113 @staticmethod
120- def requests_auth (username , password ):
121- if JumaConfig .expiration and JumaConfig .expiration > datetime .now (timezone .utc ):
122- return JumaConfig .auth
114+ def requests_auth (username , password , ** kwargs ):
115+ if StaxConfig .expiration and StaxConfig .expiration > datetime .now (timezone .utc ):
116+ return StaxConfig .auth
123117
124- return StaxAuth ("JumaAuth" ).requests_auth (username , password )
118+ return StaxAuth ("JumaAuth" ).requests_auth (username , password , ** kwargs )
125119
126120
127121class ApiTokenAuth :
128122 @staticmethod
129- def requests_auth (username , password ):
130- if JumaConfig .expiration and JumaConfig .expiration > datetime .now (timezone .utc ):
131- return JumaConfig .auth
123+ def requests_auth (username , password , ** kwargs ):
124+ if StaxConfig .expiration and StaxConfig .expiration > datetime .now (timezone .utc ):
125+ return StaxConfig .auth
132126
133- return StaxAuth ("ApiAuth" ).requests_auth (username , password )
127+ return StaxAuth ("ApiAuth" ).requests_auth (username , password , ** kwargs )
0 commit comments