Skip to main content

fastapi-cloudauth-lenient

PyPI version Python Versions License: MIT

Lenient Firebase authentication for FastAPI with configurable grace periods for time-based JWT claims.

Overview

fastapi-cloudauth-lenient extends fastapi-cloudauth to address issues where Firebase (and other JWT providers) may issue tokens with iat (issued at) or auth_time claims slightly in the future, causing unexpected verification failures.

This library provides a drop-in replacement for FirebaseCurrentUser with configurable grace periods for time-based claim validation.

Related Issue: fastapi-cloudauth#65

Features

  • Drop-in replacement for fastapi-cloudauth.firebase.FirebaseCurrentUser
  • Configurable grace periods for iat and auth_time claims
  • Maintains all standard JWT validation (signature, expiration, audience, etc.)
  • Full compatibility with FastAPI dependency injection
  • Support for custom claims via Pydantic models

Requirements

  • Python 3.8+
  • FastAPI
  • fastapi-cloudauth >= 0.4.0

Installation

# pip
pip install fastapi-cloudauth-lenient
# uv
uv add fastapi-cloudauth-lenient
# Poetry
poetry add fastapi-cloudauth-lenient

Usage

Basic Authentication

The simplest usage mirrors fastapi-cloudauth:

from fastapi import FastAPI, Depends
from fastapi_cloudauth_lenient import FirebaseCurrentUserLenient, FirebaseClaims

app = FastAPI()

get_current_user = FirebaseCurrentUserLenient(
    project_id="your-firebase-project-id"
)

@app.get("/protected/")
def protected_route(current_user: FirebaseClaims = Depends(get_current_user)):
    """Protected endpoint that requires valid Firebase authentication."""
    return {
        "user_id": current_user.user_id,
        "email": current_user.email,
    }

Custom Grace Periods

Configure grace periods for iat and auth_time validation (defaults to 5 seconds):

get_current_user = FirebaseCurrentUserLenient(
    project_id="your-firebase-project-id",
    iat_grace_period_seconds=10,  # Allow iat up to 10 seconds in the future
    auth_time_grace_period_seconds=10,  # Allow auth_time up to 10 seconds in the future
)

Custom Claims

Extend FirebaseClaims to capture additional custom claims from your Firebase tokens:

from pydantic import Field
from fastapi_cloudauth.firebase import FirebaseClaims
from fastapi_cloudauth_lenient import FirebaseCurrentUserLenient

class CustomFirebaseClaims(FirebaseClaims):
    """Extended claims model with custom fields."""
    name: str = Field(None, alias="name")
    admin: bool = Field(None, alias="admin")
    auth_time: int = Field(None, alias="auth_time")

# Initialize the authentication handler
get_current_user = FirebaseCurrentUserLenient(
    project_id="your-firebase-project-id"
)

# Override the user info model
get_current_user.user_info = CustomFirebaseClaims

@app.get("/user/profile/")
def user_profile(current_user: CustomFirebaseClaims = Depends(get_current_user)):
    return {
        "user_id": current_user.user_id,
        "email": current_user.email,
        "name": current_user.name,
        "is_admin": current_user.admin,
    }

Alternatively, use the claim() method:

get_user_detail = get_current_user.claim(CustomFirebaseClaims)

@app.get("/detailed/")
def detailed_user(user: CustomFirebaseClaims = Depends(get_user_detail)):
    return f"Hello, {user.name}"

How It Works

When Firebase issues a JWT token, the iat (issued at) and auth_time claims may occasionally be set to a timestamp slightly in the future (typically 1 second). This is due to clock skew between Firebase servers and validation servers.

Standard JWT libraries reject these tokens as invalid, causing authentication failures.

fastapi-cloudauth-lenient adds a configurable grace period:

  • iat grace period: Allows the iat claim to be up to N seconds in the future
  • auth_time grace period: Allows the auth_time claim to be up to N seconds in the future

All other JWT validations (signature, expiration, audience, issuer) remain unchanged.

API Reference

FirebaseCurrentUserLenient

class FirebaseCurrentUserLenient(FirebaseCurrentUser):
    def __init__(
        self,
        project_id: str,
        iat_grace_period_seconds: int = 5,
        auth_time_grace_period_seconds: int = 5,
    ):
        ...

Parameters:

  • project_id (str): Firebase project ID
  • iat_grace_period_seconds (int, optional): Grace period for iat claim validation. Default: 5
  • auth_time_grace_period_seconds (int, optional): Grace period for auth_time claim validation. Default: 5

Inherits all methods from: fastapi_cloudauth.firebase.FirebaseCurrentUser

License

MIT License - see LICENSE file for details.

Acknowledgments

Metadata

Release files for fastapi-cloudauth-lenient 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-cloudauth-lenient 1.0.0
File Size Uploaded Provenance Actions
fastapi_cloudauth_lenient-1.0.0.tar.gz 30.1 kB Without provenance Details for fastapi_cloudauth_lenient-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-cloudauth-lenient 1.0.0
File Interpreter ABI Platform Actions
fastapi_cloudauth_lenient-1.0.0-py3-none-any.whl Python 3 none any Details for fastapi_cloudauth_lenient-1.0.0-py3-none-any.whl

Total release size: 36.7 kB

Release files / fastapi_cloudauth_lenient-1.0.0.tar.gz

Download URL fastapi_cloudauth_lenient-1.0.0.tar.gz
Size 30.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c92c64309e88d3373305efc2947b2b0b23b5d5065c30f428295cfef927f5de52
BLAKE2b-256 checksum
How to use checksums
8c67ac520403686fe93a0dcf51565a06a2ae73ab8b1378c23f28e829fcc3ffaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.14

Release files / fastapi_cloudauth_lenient-1.0.0-py3-none-any.whl

Download URL fastapi_cloudauth_lenient-1.0.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27d1747f4a2509d649011202649bec1d3c555d5725bdecfe5bda85b15c311a6f
BLAKE2b-256 checksum
How to use checksums
bf86bf2c0c5b228c0385f91ed56d2e9c55c50f93b8254b7926f83843cdf55ad0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.14

Security for fastapi-cloudauth-lenient 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page