Skip to content

Commit 5e3fb49

Browse files
committed
removing jwt auth
1 parent 8aca009 commit 5e3fb49

File tree

1 file changed

+2
-59
lines changed

1 file changed

+2
-59
lines changed

aws_lambda_powertools/utilities/auth/aws_auth.py

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
from __future__ import annotations
22

33
import base64
4-
import json
54
from enum import Enum
65
from typing import Optional
76

87
import botocore.session
9-
import urllib3
108
from botocore.auth import SigV4Auth
119
from botocore.awsrequest import AWSRequest
1210
from botocore.credentials import Credentials, ReadOnlyCredentials
@@ -24,60 +22,11 @@ def _authorization_header(client_id: str, client_secret: str) -> str:
2422
str: Base64 encoded Authorization header
2523
"""
2624
auth_string = f"{client_id}:{client_secret}"
27-
encoded_auth_string = base64.b64encode(auth_string.encode("utf-8")).decode("utf-8")
25+
encoded_auth_bytes = base64.b64encode(auth_string)
26+
encoded_auth_string = encoded_auth_bytes.decode("utf-8")
2827
return f"Basic {encoded_auth_string}"
2928

3029

31-
def _get_token(response: dict) -> str:
32-
"""
33-
Gets the token from the response
34-
35-
Args:
36-
response (dict): Response from the authentication endpoint
37-
38-
Returns:
39-
str: Token
40-
"""
41-
if "access_token" in response:
42-
return response["access_token"]
43-
elif "id_token" in response:
44-
return response["id_token"]
45-
else:
46-
raise Exception("Unable to get token from response")
47-
48-
49-
def _request_access_token(auth_endpoint: str, body: dict, headers: dict) -> str:
50-
"""
51-
Gets the token from the Auth0 authentication endpoint
52-
53-
Args:
54-
client_id (str): Client ID
55-
client_secret (str): Client Secret
56-
audience (str): Audience
57-
auth_endpoint (str): Auth0 authentication endpoint
58-
59-
Returns:
60-
str: Token
61-
"""
62-
headers["Content-Type"] = "application/x-www-form-urlencoded"
63-
64-
http = urllib3.PoolManager()
65-
66-
if isinstance(body, dict):
67-
json_body = json.dumps(body)
68-
elif isinstance(body, str):
69-
json_body = body
70-
71-
try:
72-
response = http.request("POST", auth_endpoint, headers=headers, body=json_body)
73-
response = response.json()
74-
return _get_token(response)
75-
except (urllib3.exceptions.RequestError, urllib3.exceptions.HTTPError) as error:
76-
# If there is an error with the request, handle it here
77-
# REVIEW: CREATE A CUSTOM EXCEPTION FOR THIS
78-
raise Exception(error) from error
79-
80-
8130
class AWSServicePrefix(Enum):
8231
"""
8332
AWS Service Prefixes - Enumerations of the supported service proxy types
@@ -141,7 +90,6 @@ def __init__(
14190
secret_key: Optional[str] = None,
14291
token: Optional[str] = None,
14392
):
144-
14593
self.service = service.value
14694
self.region = region
14795
self.method = method
@@ -180,7 +128,6 @@ def __call__(self):
180128

181129

182130
class JWTAuth:
183-
184131
def __init__(
185132
self,
186133
client_id: str,
@@ -190,7 +137,6 @@ def __init__(
190137
audience: Optional[str] = None,
191138
scope: Optional[list] = None,
192139
):
193-
194140
self.client_id = client_id
195141
self.client_secret = client_secret
196142
self.auth_endpoint = auth_endpoint.removesuffix("/")
@@ -205,22 +151,19 @@ def __init__(
205151
}
206152

207153
if self.provider == AuthProvider.COGNITO.value:
208-
209154
encoded_auth_string = _authorization_header(self.client_id, self.client_secret)
210155
self.headers["Authorization"] = f"Basic {encoded_auth_string}"
211156
self.body["grant_type"] = "client_credentials"
212157
if self.scope:
213158
self.body["scope"] = " ".join(self.scope)
214159

215160
if self.provider == AuthProvider.AUTH0.value:
216-
217161
self.body["client_id"] = self.client_id
218162
self.body["client_secret"] = self.client_secret
219163
self.body["grant_type"] = "client_credentials"
220164
self.body["audience"] = self.audience
221165

222166
if self.provider == AuthProvider.OKTA.value:
223-
224167
encoded_auth_string = _authorization_header(self.client_id, self.client_secret)
225168
self.headers["Accept"] = "application/json"
226169
self.headers["Authorization"] = f"Basic {encoded_auth_string}"

0 commit comments

Comments
 (0)