1
+ from __future__ import annotations
1
2
2
- from typing import Optional
3
- from enum import Enum
4
- from botocore .auth import SigV4Auth
5
- from botocore .awsrequest import AWSRequest
6
- from botocore .credentials import Credentials
7
- import botocore .session
8
- from abc import ABC , abstractmethod
9
-
3
+ import base64
10
4
import json
5
+ from enum import Enum
6
+ from typing import Optional
11
7
8
+ import botocore .session
12
9
import urllib3
13
- import base64
10
+ from botocore .auth import SigV4Auth
11
+ from botocore .awsrequest import AWSRequest
12
+ from botocore .credentials import Credentials , ReadOnlyCredentials
14
13
15
14
16
15
def _authorization_header (client_id : str , client_secret : str ) -> str :
@@ -28,6 +27,7 @@ def _authorization_header(client_id: str, client_secret: str) -> str:
28
27
encoded_auth_string = base64 .b64encode (auth_string .encode ("utf-8" )).decode ("utf-8" )
29
28
return f"Basic { encoded_auth_string } "
30
29
30
+
31
31
def _get_token (response : dict ) -> str :
32
32
"""
33
33
Gets the token from the response
@@ -45,7 +45,8 @@ def _get_token(response: dict) -> str:
45
45
else :
46
46
raise Exception ("Unable to get token from response" )
47
47
48
- def _request_access_token (auth_endpoint : str , body : dict , headers : dict ) -> dict :
48
+
49
+ def _request_access_token (auth_endpoint : str , body : dict , headers : dict ) -> str :
49
50
"""
50
51
Gets the token from the Auth0 authentication endpoint
51
52
@@ -71,14 +72,10 @@ def _request_access_token(auth_endpoint: str, body: dict, headers: dict) -> dict
71
72
response = http .request ("POST" , auth_endpoint , headers = headers , body = json_body )
72
73
response = response .json ()
73
74
return _get_token (response )
74
- except urllib3 .exceptions .RequestError as error :
75
+ except ( urllib3 .exceptions .RequestError , urllib3 . exceptions . HTTPError ) as error :
75
76
# If there is an error with the request, handle it here
76
- raise error
77
- except urllib3 .exceptions .HTTPError as error :
78
- raise error
79
-
80
-
81
-
77
+ # REVIEW: CREATE A CUSTOM EXCEPTION FOR THIS
78
+ raise Exception (error )
82
79
83
80
84
81
class AWSServicePrefix (Enum ):
@@ -88,6 +85,7 @@ class AWSServicePrefix(Enum):
88
85
URLs:
89
86
https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html
90
87
"""
88
+
91
89
LATTICE = "vpc-lattice-svcs"
92
90
RESTAPI = "execute-api"
93
91
HTTPAPI = "apigateway"
@@ -98,10 +96,12 @@ class AuthProvider(Enum):
98
96
"""
99
97
Auth Provider - Enumerations of the supported authentication providers
100
98
"""
99
+
101
100
AUTH0 = "auth0"
102
101
COGNITO = "cognito"
103
102
OKTA = "okta"
104
103
104
+
105
105
class AWSSigV4Auth :
106
106
"""
107
107
Authenticating Requests (AWS Signature Version 4)
@@ -128,11 +128,10 @@ class AWSSigV4Auth:
128
128
>>> auth = AWSSigV4Auth(region="us-east-2", service=AWSServicePrefix.LATTICE, url="https://test-fake-service.vpc-lattice-svcs.us-east-2.on.aws")
129
129
"""
130
130
131
-
132
131
def __init__ (
133
132
self ,
134
133
url : str ,
135
- region : Optional [ str ] ,
134
+ region : str ,
136
135
body : Optional [str ] = None ,
137
136
params : Optional [dict ] = None ,
138
137
headers : Optional [dict ] = None ,
@@ -151,6 +150,8 @@ def __init__(
151
150
self .params = params
152
151
self .headers = headers
153
152
153
+ self .credentials : Credentials | ReadOnlyCredentials
154
+
154
155
if access_key and secret_key and token :
155
156
self .access_key = access_key
156
157
self .secret_key = secret_key
@@ -178,18 +179,17 @@ def __call__(self):
178
179
return self .signed_request
179
180
180
181
181
-
182
182
class JWTAuth :
183
183
184
184
def __init__ (
185
- self ,
186
- client_id : str ,
187
- client_secret : str ,
188
- auth_endpoint : str ,
189
- provider : Enum = AuthProvider .COGNITO ,
190
- audience : Optional [str ] = None ,
191
- scope : Optional [list ] = None
192
- ):
185
+ self ,
186
+ client_id : str ,
187
+ client_secret : str ,
188
+ auth_endpoint : str ,
189
+ provider : Enum = AuthProvider .COGNITO ,
190
+ audience : Optional [str ] = None ,
191
+ scope : Optional [list ] = None ,
192
+ ):
193
193
194
194
self .client_id = client_id
195
195
self .client_secret = client_secret
@@ -230,7 +230,4 @@ def __init__(
230
230
if scope :
231
231
self .body ["scope" ] = " " .join (self .scope )
232
232
233
-
234
- response = _request_access_token (auth_endpoint = self .auth_endpoint , body = self .body , headers = self .headers )
235
-
236
-
233
+ # response = _request_access_token(auth_endpoint=self.auth_endpoint, body=self.body, headers=self.headers) # noqa ERA001
0 commit comments