Skip to content

Commit

Permalink
feat: staging-only /auth/test endpoint for dummy user auth
Browse files Browse the repository at this point in the history
Co-Authored-By: Benjamin Bolte <[email protected]>
  • Loading branch information
devin-ai-integration[bot] and codekansas committed Dec 25, 2024
1 parent 9b8e07d commit 43d2eff
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions www/app/routers/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from www.app.routers.auth.email import router as email_router
from www.app.routers.auth.github import router as github_router
from www.app.routers.auth.google import router as google_router
from www.app.routers.auth.test import router as test_router

router = APIRouter()

router.include_router(api_router, prefix="/api")
router.include_router(github_router, prefix="/github")
router.include_router(google_router, prefix="/google")
router.include_router(email_router, prefix="/email")
router.include_router(test_router, prefix="/test")
52 changes: 52 additions & 0 deletions www/app/routers/auth/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Defines the test authentication endpoint for staging environment."""

from typing import Annotated

from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel

from www.app.crud.users import UserCrud
from www.app.model import APIKeySource
from www.settings import settings

router = APIRouter()


class TestAuthResponse(BaseModel):
"""Response model for test authentication endpoint."""

user_id: str
token: str


@router.post("/test", response_model=TestAuthResponse)
async def test_auth_endpoint(user_crud: Annotated[UserCrud, Depends()]) -> TestAuthResponse:
"""Authenticates as a test user in staging environment.
This endpoint is only available in development and staging environments.
It creates (or reuses) a test user account and returns authentication credentials.
"""
# Only allow in development/staging environments
if settings.environment not in ["development", "staging"]:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Test auth endpoint not available in this environment",
)

# Attempt to find or create the test user
test_email = "[email protected]"
test_password = "dummy-test-password-123" # Password doesn't matter as we use API key

existing_user = await user_crud.get_user_from_email(test_email)
if not existing_user:
# Create new test user if it doesn't exist
existing_user = await user_crud._create_user_from_email(test_email, test_password)

# Generate an API key for the test user
api_key = await user_crud.add_api_key(
existing_user.id,
source=APIKeySource("password"),
permissions="full", # Test user gets full permissions like normal users
)

return TestAuthResponse(user_id=existing_user.id, token=api_key.id)

0 comments on commit 43d2eff

Please sign in to comment.