diff --git a/src/hellocomputer/routers/analysis.py b/src/hellocomputer/routers/analysis.py index 363378b..e0976b9 100644 --- a/src/hellocomputer/routers/analysis.py +++ b/src/hellocomputer/routers/analysis.py @@ -1,5 +1,3 @@ -import os - from fastapi import APIRouter from fastapi.responses import PlainTextResponse diff --git a/src/hellocomputer/routers/auth.py b/src/hellocomputer/routers/auth.py new file mode 100644 index 0000000..f9cbc8b --- /dev/null +++ b/src/hellocomputer/routers/auth.py @@ -0,0 +1,50 @@ +from authlib.integrations.starlette_client import OAuth, OAuthError +from fastapi import APIRouter +from fastapi.responses import HTMLResponse, RedirectResponse +from starlette.requests import Request + +from ..config import settings + +router = APIRouter() + +oauth = OAuth() +oauth.register( + "auth0", + client_id=settings.auth0_client_id, + client_secret=settings.auth0_client_secret, + client_kwargs={"scope": "openid profile email", "verify": False}, + server_metadata_url=f"https://{settings.auth0_domain}/.well-known/openid-configuration", +) + + +@router.get("/login") +async def login(request: Request): + return await oauth.auth0.authorize_redirect( + request, + redirect_uri=f"{settings.base_url}/callback", + ) + + +@router.route("/callback", methods=["GET", "POST"]) +async def callback(request: Request): + try: + token = await oauth.auth0.authorize_access_token(request) + except OAuthError as error: + return HTMLResponse(f"

{error.error}

") + user = token.get("userinfo") + if user: + request.session["user"] = dict(user) + + return RedirectResponse(url="/app") + + +@router.get("/logout") +async def logout(request: Request): + request.session.pop("user", None) + return RedirectResponse(url="/") + + +@router.get("/user") +async def user(request: Request): + user = request.session.get("user") + return user diff --git a/src/hellocomputer/routers/health.py b/src/hellocomputer/routers/health.py new file mode 100644 index 0000000..190669a --- /dev/null +++ b/src/hellocomputer/routers/health.py @@ -0,0 +1,31 @@ +from fastapi import APIRouter, status +from pydantic import BaseModel + +router = APIRouter() + + +class HealthCheck(BaseModel): + """Response model to validate and return when performing a health check.""" + + status: str = "OK" + + +@router.get( + "/health", + tags=["healthcheck"], + summary="Perform a Health Check", + response_description="Return HTTP Status Code 200 (OK)", + status_code=status.HTTP_200_OK, + response_model=HealthCheck, +) +def get_health() -> HealthCheck: + """ + ## Perform a Health Check + Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker + to ensure a robust container orchestration and management is in place. Other + services which rely on proper functioning of the API service will not deploy if this + endpoint returns any other HTTP status code except 200 (OK). + Returns: + HealthCheck: Returns a JSON response with the health status + """ + return HealthCheck(status="OK") diff --git a/src/hellocomputer/routers/sessions.py b/src/hellocomputer/routers/sessions.py index 3fc99ae..bee8fd1 100644 --- a/src/hellocomputer/routers/sessions.py +++ b/src/hellocomputer/routers/sessions.py @@ -1,9 +1,8 @@ from uuid import uuid4 from fastapi import APIRouter -from starlette.requests import Request from fastapi.responses import PlainTextResponse - +from starlette.requests import Request # Scheme for the Authorization header diff --git a/src/hellocomputer/security.py b/src/hellocomputer/security.py deleted file mode 100644 index e69de29..0000000