Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
refactor: clean up old FastAPI logic, temporary utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mcataford committed Nov 23, 2023
1 parent d188158 commit 9477f1a
Show file tree
Hide file tree
Showing 59 changed files with 30 additions and 1,487 deletions.
9 changes: 0 additions & 9 deletions Taskfile.backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ tasks:
deps: [docker-build]
cmd: docker run -d -p 8000:8000 --name {{ .APP_CONTAINER_NAME }} {{ .CLI_ARGS }} --add-host docker.host.internal:host-gateway rotini:dev
dir: backend/rotini
start-django:
desc: "Starts the backend application."
deps: [docker-build-django]
cmd: docker run -d -p 8000:8000 --name {{ .APP_CONTAINER_NAME }} {{ .CLI_ARGS }} --add-host docker.host.internal:host-gateway rotini:dev-django
dir: backend/rotini
stop:
desc: "Stops the backend application."
cmd: docker rm -f {{ .APP_CONTAINER_NAME }}
Expand Down Expand Up @@ -78,8 +73,4 @@ tasks:
desc: "Builds a docker image from /backend"
cmd: docker build --build-arg PYTHON_VERSION=$(cat .python-version) -t rotini:dev .
dir: backend
docker-build-django:
desc: "Builds a docker image from /backend"
cmd: docker build --file Dockerfile-django -t rotini:dev-django .
dir: backend

6 changes: 2 additions & 4 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
ARG PYTHON_VERSION

FROM python:$PYTHON_VERSION-slim
FROM python:3.11-slim

ENV DEBIAN_FRONTEND=noninteractive

Expand All @@ -21,4 +19,4 @@ COPY ./rotini ./rotini

WORKDIR ./rotini

CMD python3 -m uvicorn main:app --host 0.0.0.0
CMD python3 -m uvicorn base.asgi:application --host 0.0.0.0
22 changes: 0 additions & 22 deletions backend/Dockerfile-django

This file was deleted.

2 changes: 1 addition & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages = ["rotini"]
DJANGO_SETTINGS_MODULE="base.settings"
pythonpath=[
".",
"./rotini_django",
"./rotini",
]

[tool.pylint.'MASTER']
Expand Down
File renamed without changes.
File renamed without changes.
31 changes: 0 additions & 31 deletions backend/rotini/auth/base.py

This file was deleted.

25 changes: 0 additions & 25 deletions backend/rotini/auth/decorators.py

This file was deleted.

File renamed without changes.
57 changes: 27 additions & 30 deletions backend/rotini/auth/middleware.py
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)
68 changes: 0 additions & 68 deletions backend/rotini/auth/routes.py

This file was deleted.

File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9477f1a

Please sign in to comment.