Skip to content

Commit e7d9a6f

Browse files
authored
Merge pull request #13 from Ifechukwu001/fix/auth
Refactored auth algorithm
2 parents b44c6b7 + 8f63d61 commit e7d9a6f

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
except KeyError as e:
@@ -30,22 +29,10 @@ def authenticate(self, request: HttpRequest, key: str | None) -> str | None:
3029
)
3130
raise AuthenticationError(message=message)
3231

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

src/api/routes/__init__.py

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

2222
schema["components"]["securitySchemes"] = {
23-
"Gateway Key": {
24-
"type": "apiKey",
25-
"in": "header",
26-
"name": "X-API-GATEWAY-KEY",
27-
},
2823
"API Timestamp": {
2924
"type": "apiKey",
3025
"in": "header",
@@ -39,7 +34,6 @@ def custom_openapi_schema(path_params: dict | None = None) -> OpenAPISchema:
3934

4035
schema["security"] = [
4136
{
42-
"Gateway Key": [],
4337
"API Timestamp": [],
4438
"API Signature": [],
4539
}

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

@@ -111,9 +112,12 @@ def generate_uuid() -> ExpireUUID:
111112

112113
@staticmethod
113114
def generate_signature(key: str, timestamp: str) -> str:
114-
signature = hmac.new(
115-
key=key.encode(), msg=timestamp.encode(), digestmod=hashlib.sha256
116-
).hexdigest()
115+
digest = hmac.new(
116+
key=key.encode(),
117+
msg=f"{key}:{timestamp}".encode(),
118+
digestmod=hashlib.sha256,
119+
).digest()
120+
signature = b64encode(digest).decode()
117121
return signature
118122

119123
@staticmethod

0 commit comments

Comments
 (0)