' + data + '
' + data + '
diff --git a/requirements.in b/requirements.in index 0100067..6795e1d 100644 --- a/requirements.in +++ b/requirements.in @@ -7,4 +7,6 @@ s3fs aiofiles duckdb pyjwt[crypto] -python-multipart \ No newline at end of file +python-multipart +authlib +itsdangerous \ No newline at end of file diff --git a/src/hellocomputer/config.py b/src/hellocomputer/config.py index 6e86baa..aabaf44 100644 --- a/src/hellocomputer/config.py +++ b/src/hellocomputer/config.py @@ -2,11 +2,16 @@ from pydantic_settings import BaseSettings, SettingsConfigDict class Settings(BaseSettings): + base_url: str = "http://localhost:8000" anyscale_api_key: str = "Awesome API" gcs_access: str = "access" gcs_secret: str = "secret" gcs_bucketname: str = "bucket" auth: bool = True + auth0_client_id: str = "" + auth0_client_secret: str = "" + auth0_domain: str = "" + app_secret_key: str = "" model_config = SettingsConfigDict(env_file=".env") diff --git a/src/hellocomputer/main.py b/src/hellocomputer/main.py index 127dfc7..ec17aab 100644 --- a/src/hellocomputer/main.py +++ b/src/hellocomputer/main.py @@ -1,16 +1,76 @@ from pathlib import Path from fastapi import FastAPI, status +from fastapi.responses import RedirectResponse, HTMLResponse from fastapi.staticfiles import StaticFiles +from starlette.middleware.sessions import SessionMiddleware +from starlette.requests import Request +from authlib.integrations.starlette_client import OAuth, OAuthError from pydantic import BaseModel +import json + import hellocomputer from .routers import analysis, files, sessions +from .config import settings 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.add_middleware(SessionMiddleware, secret_key=settings.app_secret_key) + + +@app.get("/") +async def homepage(request: Request): + user = request.session.get("user") + if user: + print(json.dumps(user)) + return RedirectResponse("/app") + + with open(static_path / "login.html") as f: + return HTMLResponse(f.read()) + + +@app.route("/login") +async def login(request: Request): + return await oauth.auth0.authorize_redirect( + request, + redirect_uri="http://localhost:8000/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"