Skip to content

Add FastAPI integration with dependencies #3

@genro

Description

@genro

Context

Users need easy way to protect FastAPI endpoints with token authentication. Standard FastAPI approach is dependency injection.

Goal

Create FastAPI dependencies for authentication and scope-based authorization.

Implementation

  • Create genro_auth/fastapi.py module
  • Implement create_auth_dependency(token_manager) function
  • Implement create_scope_dependency(required_scopes) function
  • Use HTTPBearer for token extraction from Authorization header
  • Return user data dict from dependency
  • Raise HTTPException(401) for invalid/missing tokens
  • Raise HTTPException(403) for insufficient scopes

Acceptance Criteria

  • require_auth = create_auth_dependency(manager) works
  • @app.get('/protected') with Depends(require_auth) protects endpoint
  • Invalid token returns 401 with clear message
  • Missing scopes returns 403 with clear message
  • User data accessible in route: user['user_id'], user['scopes']

Dependencies

Notes

  • Use standard FastAPI Depends() pattern
  • Extract token from Authorization: Bearer <token> header
  • Scope check: exact match (no wildcards in v0.1.0)
  • Return user dict: {'user_id': str, 'scopes': list, 'type': str, 'expires_at': datetime}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions