Compare commits
No commits in common. "04351888a841a8a34124ec609aa2b38ac99a7639" and "faba0fd14f88c0f3935a26c13515f85f2d3fab4a" have entirely different histories.
04351888a8
...
faba0fd14f
|
@ -1,19 +1,30 @@
|
||||||
import json
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI, status
|
||||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
from fastapi.responses import RedirectResponse, HTMLResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from starlette.middleware.sessions import SessionMiddleware
|
from starlette.middleware.sessions import SessionMiddleware
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
|
from authlib.integrations.starlette_client import OAuth, OAuthError
|
||||||
|
from pydantic import BaseModel
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
import hellocomputer
|
import hellocomputer
|
||||||
|
|
||||||
|
from .routers import analysis, files, sessions
|
||||||
from .config import settings
|
from .config import settings
|
||||||
from .routers import analysis, auth, files, sessions, health
|
|
||||||
|
|
||||||
static_path = Path(hellocomputer.__file__).parent / "static"
|
static_path = Path(hellocomputer.__file__).parent / "static"
|
||||||
|
|
||||||
|
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",
|
||||||
|
)
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.add_middleware(SessionMiddleware, secret_key=settings.app_secret_key)
|
app.add_middleware(SessionMiddleware, secret_key=settings.app_secret_key)
|
||||||
|
|
||||||
|
@ -29,11 +40,69 @@ async def homepage(request: Request):
|
||||||
return HTMLResponse(f.read())
|
return HTMLResponse(f.read())
|
||||||
|
|
||||||
|
|
||||||
app.include_router(health.router)
|
@app.route("/login")
|
||||||
|
async def login(request: Request):
|
||||||
|
return await oauth.auth0.authorize_redirect(
|
||||||
|
request,
|
||||||
|
redirect_uri=f"{settings.base_url}/callback",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.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"<h1>{error.error}</h1>")
|
||||||
|
user = token.get("userinfo")
|
||||||
|
if user:
|
||||||
|
request.session["user"] = dict(user)
|
||||||
|
|
||||||
|
return RedirectResponse(url="/app")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/logout")
|
||||||
|
async def logout(request: Request):
|
||||||
|
request.session.pop("user", None)
|
||||||
|
return RedirectResponse(url="/")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/user")
|
||||||
|
async def user(request: Request):
|
||||||
|
user = request.session.get("user")
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
class HealthCheck(BaseModel):
|
||||||
|
"""Response model to validate and return when performing a health check."""
|
||||||
|
|
||||||
|
status: str = "OK"
|
||||||
|
|
||||||
|
|
||||||
|
@app.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")
|
||||||
|
|
||||||
|
|
||||||
app.include_router(sessions.router)
|
app.include_router(sessions.router)
|
||||||
app.include_router(files.router)
|
app.include_router(files.router)
|
||||||
app.include_router(analysis.router)
|
app.include_router(analysis.router)
|
||||||
app.include_router(auth.router)
|
|
||||||
app.mount(
|
app.mount(
|
||||||
"/app",
|
"/app",
|
||||||
StaticFiles(directory=static_path, html=True),
|
StaticFiles(directory=static_path, html=True),
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import os
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from fastapi.responses import PlainTextResponse
|
from fastapi.responses import PlainTextResponse
|
||||||
|
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
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"<h1>{error.error}</h1>")
|
|
||||||
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
|
|
|
@ -1,31 +0,0 @@
|
||||||
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")
|
|
|
@ -1,8 +1,9 @@
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
from fastapi.responses import PlainTextResponse
|
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
|
from fastapi.responses import PlainTextResponse
|
||||||
|
|
||||||
|
|
||||||
# Scheme for the Authorization header
|
# Scheme for the Authorization header
|
||||||
|
|
||||||
|
|
0
src/hellocomputer/security.py
Normal file
0
src/hellocomputer/security.py
Normal file
Loading…
Reference in a new issue