This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: clean up old FastAPI logic, temporary utils
- Loading branch information
Showing
59 changed files
with
30 additions
and
1,487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,39 @@ | ||
""" | ||
Authentication & authorization middleware logic. | ||
""" | ||
import logging | ||
|
||
import jwt.exceptions | ||
from fastapi import Request | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
import django.http | ||
import django.contrib.auth | ||
|
||
import auth.use_cases as auth_use_cases | ||
import auth.jwt | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
AuthUser = django.contrib.auth.get_user_model() | ||
|
||
class AuthenticationMiddleware(BaseHTTPMiddleware): | ||
""" | ||
Decodes Authorization headers if present on the request and sets | ||
identifying fields in the request state. | ||
|
||
This information is then leveraged by individual routes to determine | ||
authorization. | ||
class JwtMiddleware: | ||
""" | ||
Middleware that handles using credentials supplied via the authorization | ||
headers on requests to log users in seamlessly. | ||
""" | ||
|
||
async def dispatch(self, request: Request, call_next): | ||
auth_header = request.headers.get("authorization") | ||
decoded_token = None | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
if auth_header is not None: | ||
_, token = auth_header.split(" ") | ||
def __call__(self, request: django.http.HttpRequest) -> django.http.HttpResponse: | ||
authorization_header = request.META.get("HTTP_AUTHORIZATION") | ||
|
||
if authorization_header is not None: | ||
try: | ||
decoded_token = auth_use_cases.decode_token(token) | ||
except jwt.exceptions.ExpiredSignatureError as exc: | ||
logger.exception(exc) | ||
|
||
if decoded_token is not None: | ||
logger.info(decoded_token) | ||
request.state.user = { | ||
"username": decoded_token["username"], | ||
"user_id": decoded_token["user_id"], | ||
} | ||
|
||
return await call_next(request) | ||
_, token = authorization_header.split(" ") | ||
decoded_token = auth.jwt.decode_token(token) | ||
|
||
logger.info("Token: %s\nDecoded token: %s", token, decoded_token) | ||
|
||
user = AuthUser.objects.get(pk=decoded_token["user_id"]) | ||
|
||
request.user = user | ||
except Exception as e: | ||
print(e) | ||
return django.http.HttpResponse(status=401) | ||
|
||
return self.get_response(request) |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.