]> git.ipfire.org Git - pbs.git/commitdiff
API: Group all authentication endpoints together
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Jun 2025 17:11:57 +0000 (17:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 16 Jun 2025 17:11:57 +0000 (17:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/api/auth.py

index a26e2e2a5c133692cba9ed32639eaeb9e5df2b72..d5420f087a624fa48d2a9f3edea1710cc3832246 100644 (file)
@@ -42,6 +42,12 @@ TOKEN_ALGO   = "HS256"
 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"
@@ -72,7 +78,7 @@ def create_token(subject, type, expires_after, **kwargs):
        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
@@ -109,7 +115,7 @@ async def auth_user(credentials: fastapi.security.OAuth2PasswordRequestForm =
 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])
@@ -132,3 +138,6 @@ async def auth_refresh(data: RefreshRequest):
 
        # Send the response
        return AuthResponse(access_token=access_token, refresh_token=data.refresh_token)
+
+# Add everything to the app
+app.include_router(router)