Skip to content

Commit

Permalink
following fastapi security to add oauth. wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fullerzz committed Aug 3, 2024
1 parent 757c4f5 commit 688a905
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/smolvault/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Annotated

from fastapi import Depends
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")


class User(BaseModel):
username: str
email: str | None = None
full_name: str | None = None
disabled: bool | None = None


def decode_token(token: str) -> User:
return User(
username="fakeuser",
email="[email protected]",
full_name="Fake User",
)


async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]) -> User:
user = decode_token(token)
return user
7 changes: 5 additions & 2 deletions src/smolvault/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import FileResponse, Response

from smolvault.auth import User, get_current_user
from smolvault.cache.cache_manager import CacheManager
from smolvault.clients.aws import S3Client
from smolvault.clients.database import DatabaseClient, FileMetadataRecord
Expand All @@ -25,6 +26,7 @@
logger = logging.getLogger(__name__)

app = FastAPI(title="smolvault")

app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(
CORSMiddleware,
Expand All @@ -34,14 +36,15 @@
allow_headers=["*"],
)


settings: Settings = get_settings()
s3_client = S3Client(bucket_name=settings.smolvault_bucket)
cache = CacheManager(cache_dir=settings.smolvault_cache)


@app.get("/")
async def read_root() -> dict[str, str]:
return {"Hello": "World"}
async def read_root(current_user: Annotated[User, Depends(get_current_user)]) -> User:
return current_user


@app.post("/file/upload")
Expand Down

0 comments on commit 688a905

Please sign in to comment.