Skip to content

Commit e1e0e3b

Browse files
committed
Refactored auth algorithm
1 parent 999b3a9 commit e1e0e3b

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
lines changed

src/api/middlewares/GateWayMiddleware.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def __init__(self, logger: Logger) -> None:
1616

1717
def authenticate(self, request: HttpRequest, key: str | None) -> str | None:
1818
try:
19-
api_key = request.headers["X-API-GATEWAY-KEY"]
2019
api_timestamp = request.headers["X-API-GATEWAY-TIMESTAMP"]
2120
api_signature = request.headers["X-API-GATEWAY-SIGNATURE"]
2221
user_id = request.headers["X-USER-ID"]
@@ -32,22 +31,10 @@ def authenticate(self, request: HttpRequest, key: str | None) -> str | None:
3231
)
3332
raise AuthenticationError(message=message)
3433

35-
valid_api_key = api_gateway["key"]
36-
if api_key != valid_api_key:
37-
message = "Invalid API key!"
38-
self.logger.error(
39-
{
40-
"activity_type": "Authenticate Gateway Request",
41-
"message": message,
42-
"metadata": {"headers": request.headers},
43-
}
44-
)
45-
raise AuthenticationError(message=message)
46-
4734
signature_data: SignatureData = {
4835
"signature": api_signature,
4936
"timestamp": api_timestamp,
50-
"key": valid_api_key,
37+
"key": api_gateway["key"],
5138
"ttl": api_gateway["ttl"],
5239
"title": SIGNATURE_SOURCES["gateway"],
5340
}

src/api/routes/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ def custom_openapi_schema(path_params: dict | None = None) -> OpenAPISchema:
1919
schema = original_get_openapi_schema()
2020

2121
schema["components"]["securitySchemes"] = {
22-
"Gateway Key": {
23-
"type": "apiKey",
24-
"in": "header",
25-
"name": "X-API-GATEWAY-KEY",
26-
},
2722
"API Timestamp": {
2823
"type": "apiKey",
2924
"in": "header",
@@ -48,7 +43,6 @@ def custom_openapi_schema(path_params: dict | None = None) -> OpenAPISchema:
4843

4944
schema["security"] = [
5045
{
51-
"Gateway Key": [],
5246
"API Timestamp": [],
5347
"API Signature": [],
5448
"User ID": [],

src/api/services/UtilityService.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hmac
22
import hashlib
33
from uuid import uuid4
4+
from base64 import b64encode
45
from typing import TypedDict
56
from datetime import UTC, datetime, timedelta
67

@@ -96,9 +97,12 @@ def generate_uuid() -> ExpireUUID:
9697

9798
@staticmethod
9899
def generate_signature(key: str, timestamp: str) -> str:
99-
signature = hmac.new(
100-
key=key.encode(), msg=timestamp.encode(), digestmod=hashlib.sha256
101-
).hexdigest()
100+
digest = hmac.new(
101+
key=key.encode(),
102+
msg=f"{key}:{timestamp}".encode(),
103+
digestmod=hashlib.sha256,
104+
).digest()
105+
signature = b64encode(digest).decode()
102106
return signature
103107

104108
@staticmethod

0 commit comments

Comments
 (0)