fastapi_tools.auth.google
¶
Authentication service for Google OAuth and session management.
Classes:
-
GoogleAuthService–Service for Google OAuth 2.0 authentication.
-
SessionStore–In-memory session storage.
GoogleAuthService
¶
GoogleAuthService(
oauth_config: GoogleOAuthConfig,
session_config: SessionConfig,
session_store: SessionStore,
)
Service for Google OAuth 2.0 authentication.
Initialize auth service.
Parameters:
-
oauth_config(GoogleOAuthConfig) –Google OAuth configuration.
-
session_config(SessionConfig) –Session management configuration.
-
session_store(SessionStore) –Session storage backend.
Methods:
-
authenticate–Complete authentication flow.
-
create_session–Create a new session for authenticated user.
-
exchange_code_for_tokens–Exchange authorization code for tokens.
-
get_authorization_url–Generate Google OAuth authorization URL.
-
get_session–Get session by ID.
-
get_user_info–Get user information from Google.
-
revoke_session–Revoke a session.
-
validate_state–Validate OAuth state parameter.
Source code in src/fastapi_tools/auth/google.py
authenticate
async
¶
Complete authentication flow.
Parameters:
-
code(str) –Authorization code from Google.
-
state(str) –State parameter for CSRF validation.
-
redirect_uri(str | None, default:None) –Override redirect URI. Must match the one used during login.
Returns:
-
SessionData–SessionData for the authenticated user.
Raises:
-
ValueError–If the state parameter is invalid.
Source code in src/fastapi_tools/auth/google.py
create_session
¶
Create a new session for authenticated user.
Source code in src/fastapi_tools/auth/google.py
exchange_code_for_tokens
async
¶
Exchange authorization code for tokens.
Parameters:
-
code(str) –Authorization code from Google.
-
redirect_uri(str | None, default:None) –Override redirect URI. Must match the one used during login.
Returns:
-
dict–Token response dictionary.
Source code in src/fastapi_tools/auth/google.py
get_authorization_url
¶
Generate Google OAuth authorization URL.
Parameters:
-
redirect_uri(str | None, default:None) –Override redirect URI (e.g. for reverse-proxy setups). If None, uses the value from oauth_config.
Returns:
Source code in src/fastapi_tools/auth/google.py
get_session
¶
get_user_info
async
¶
Get user information from Google.
Source code in src/fastapi_tools/auth/google.py
revoke_session
¶
SessionStore
¶
In-memory session storage.
For production, consider using Redis or a database.
Initialize empty session store.
Methods:
-
cleanup_expired–Remove expired sessions and state tokens.
-
create_session–Store a new session.
-
delete_session–Delete a session.
-
get_session–Retrieve a session by ID.
-
store_state_token–Store OAuth state token for CSRF protection.
-
validate_state_token–Validate and consume a state token.
Source code in src/fastapi_tools/auth/google.py
cleanup_expired
¶
Remove expired sessions and state tokens.
Source code in src/fastapi_tools/auth/google.py
create_session
¶
delete_session
¶
get_session
¶
Retrieve a session by ID.
Source code in src/fastapi_tools/auth/google.py
store_state_token
¶
validate_state_token
¶
Validate and consume a state token.