mirror of
https://github.com/cssgunc/compass.git
synced 2025-04-18 17:50:16 -04:00
FastAPI auth tutorial
This commit is contained in:
parent
d0a315c365
commit
89e7e38cb6
|
@ -0,0 +1,27 @@
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends
|
||||||
|
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||||
|
|
||||||
|
from backend.models.user_model import User
|
||||||
|
|
||||||
|
api = APIRouter(prefix="/api/auth")
|
||||||
|
|
||||||
|
openapi_tags = {
|
||||||
|
"name": "Authentication",
|
||||||
|
"description": "Authentication and authorization handling.",
|
||||||
|
}
|
||||||
|
|
||||||
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
||||||
|
|
||||||
|
def decode_token(token):
|
||||||
|
"""To implement later. Payload for JWT will be a User object returned by this function."""
|
||||||
|
return token
|
||||||
|
|
||||||
|
async def get_user(token: Annotated[str, Depends(oauth2_scheme)]):
|
||||||
|
user = decode_token(token)
|
||||||
|
return user
|
||||||
|
|
||||||
|
@api.get("/user")
|
||||||
|
async def get_subject(subject: Annotated[User, Depends(get_user)]):
|
||||||
|
return subject
|
|
@ -3,7 +3,7 @@ from fastapi.responses import JSONResponse
|
||||||
from fastapi.middleware.gzip import GZipMiddleware
|
from fastapi.middleware.gzip import GZipMiddleware
|
||||||
|
|
||||||
|
|
||||||
from .api import user, health, service, resource, tag
|
from .api import user, health, service, resource, authentication
|
||||||
|
|
||||||
description = """
|
description = """
|
||||||
Welcome to the **COMPASS** RESTful Application Programming Interface.
|
Welcome to the **COMPASS** RESTful Application Programming Interface.
|
||||||
|
@ -18,13 +18,13 @@ app = FastAPI(
|
||||||
health.openapi_tags,
|
health.openapi_tags,
|
||||||
service.openapi_tags,
|
service.openapi_tags,
|
||||||
resource.openapi_tags,
|
resource.openapi_tags,
|
||||||
tag.openapi_tags
|
authentication.openapi_tags,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
app.add_middleware(GZipMiddleware)
|
app.add_middleware(GZipMiddleware)
|
||||||
|
|
||||||
feature_apis = [user, health, service, resource, tag]
|
feature_apis = [user, health, service, resource, authentication]
|
||||||
|
|
||||||
for feature_api in feature_apis:
|
for feature_api in feature_apis:
|
||||||
app.include_router(feature_api.api)
|
app.include_router(feature_api.api)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user