ACCESS_TOKEN_EXPIRY_TIME = datetime.timedelta(minutes=60)
REFRESH_TOKEN_EXPIRY_TIME = datetime.timedelta(days=7)
+# Create a new router for authentication requests
+router = fastapi.APIRouter(
+ prefix="/auth",
+ tags=["Authentication"],
+)
+
class AuthResponse(pydantic.BaseModel):
# Token Type
type: str = "Bearer"
return jwt.encode(payload, TOKEN_SECRET, algorithm=TOKEN_ALGO)
-@app.post("/auth/user")
+@router.post("/user")
async def auth_user(credentials: fastapi.security.OAuth2PasswordRequestForm =
fastapi.Depends()) -> AuthResponse:
# Set keytab to use
class RefreshRequest(pydantic.BaseModel):
refresh_token: str
-@app.post("/auth/refresh")
+@router.post("/refresh")
async def auth_refresh(data: RefreshRequest):
try:
payload = jwt.decode(data.refresh_token, TOKEN_SECRET, algorithms=[TOKEN_ALGO])
# Send the response
return AuthResponse(access_token=access_token, refresh_token=data.refresh_token)
+
+# Add everything to the app
+app.include_router(router)