Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /descoped_authly /src /authly /api /oidc_router.py
"""
OpenID Connect (OIDC) API Router.
Provides OpenID Connect 1.0 endpoints including discovery, UserInfo, and JWKS.
"""
import logging
from datetime import UTC, datetime
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
from fastapi.responses import HTMLResponse, RedirectResponse
from authly.api.auth_dependencies import get_client_repository
from authly.api.oauth_router import get_discovery_service
from authly.api.users_dependencies import get_current_user, get_token_scopes, get_user_service, get_userinfo_service
from authly.oauth.client_repository import ClientRepository
from authly.oauth.discovery_service import DiscoveryService
from authly.oidc.discovery import OIDCDiscoveryService, OIDCServerMetadata
from authly.oidc.id_token import IDTokenGenerator
from authly.oidc.userinfo import UserInfoResponse, UserInfoService, UserInfoUpdateRequest
from authly.tokens import TokenService, get_token_service
from authly.users.models import UserModel
logger = logging.getLogger(__name__)
# Create OIDC router
oidc_router = APIRouter(tags=["OpenID Connect"])
def get_base_url(request: Request) -> str:
"""
Extract base URL from request.
Args:
request: FastAPI request object
Returns:
Base URL as string
"""
# Handle both direct access and reverse proxy scenarios
if request.headers.get("x-forwarded-proto"):
scheme = request.headers.get("x-forwarded-proto", "https")
else:
scheme = request.url.scheme
if request.headers.get("x-forwarded-host"):
host = request.headers.get("x-forwarded-host")
else:
host = request.headers.get("host", request.url.netloc)
return f"{scheme}://{host}"
@oidc_router.get(
"/.well-known/openid-configuration",
response_model=OIDCServerMetadata,
summary="OpenID Connect Discovery",
description="""
OpenID Connect Discovery endpoint as defined in OpenID Connect Discovery 1.0.
Returns server capabilities, supported features, and endpoint URLs for
OpenID Connect clients. This endpoint extends OAuth 2.1 server metadata
with OIDC-specific capabilities.
**Key Features:**
- Complete OpenID Connect 1.0 metadata
- ID token signing algorithms and capabilities
- UserInfo endpoint information
- JWKS URI for key discovery
- Claims and scopes supported
- Response types and modes for OIDC flows
**Security:**
- No authentication required (public endpoint)
- Rate limiting applied through server configuration
""",
responses={
200: {
"description": "OpenID Connect server metadata",
"content": {
"application/json": {
"example": {
"issuer": "https://auth.example.com",
"authorization_endpoint": "https://auth.example.com/api/v1/oauth/authorize",
"token_endpoint": "https://auth.example.com/api/v1/oauth/token",
"userinfo_endpoint": "https://auth.example.com/oidc/userinfo",
"jwks_uri": "https://auth.example.com/.well-known/jwks.json",
"response_types_supported": ["code", "id_token", "code id_token"],
"id_token_signing_alg_values_supported": ["RS256", "HS256"],
"subject_types_supported": ["public"],
"claims_supported": ["sub", "name", "email", "email_verified", "profile"],
"scopes_supported": ["openid", "profile", "email", "address", "phone"],
"grant_types_supported": ["authorization_code", "refresh_token"],
"code_challenge_methods_supported": ["S256"],
"require_pkce": True,
}
}
},
},
500: {"description": "Internal server error"},
},
)
async def oidc_discovery(
request: Request, oauth_discovery_service: DiscoveryService = Depends(get_discovery_service)
) -> OIDCServerMetadata:
"""
OpenID Connect Discovery endpoint.
Returns comprehensive OIDC server metadata including OAuth 2.1 capabilities
extended with OpenID Connect specific features.
Args:
request: FastAPI request object for URL extraction
oauth_discovery_service: OAuth 2.1 discovery service
Returns:
OIDCServerMetadata: Complete OIDC server metadata
Raises:
HTTPException: If metadata generation fails
"""
try:
# Extract base URL from request
base_url = get_base_url(request)
# Get API prefix from config (default to /api/v1)
api_prefix = "/api/v1"
# Create OIDC discovery service
oidc_discovery_service = OIDCDiscoveryService(oauth_discovery_service)
# Generate OIDC server metadata
metadata = await oidc_discovery_service.get_oidc_server_metadata(issuer_url=base_url, api_prefix=api_prefix)
logger.info(f"OIDC discovery request from {request.client.host if request.client else 'unknown'}")
return metadata
except Exception as e:
logger.error(f"Error generating OIDC discovery metadata: {e}")
# Fallback to static metadata to prevent service disruption
try:
base_url = get_base_url(request)
oidc_discovery_service = OIDCDiscoveryService(oauth_discovery_service)
static_metadata = oidc_discovery_service.get_static_oidc_metadata(base_url)
logger.warning("Returned static OIDC discovery metadata due to error")
return static_metadata
except Exception as fallback_error:
logger.error(f"Failed to generate fallback OIDC discovery metadata: {fallback_error}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to generate OIDC discovery metadata"
) from fallback_error
@oidc_router.get(
"/oidc/userinfo",
response_model=UserInfoResponse,
summary="OpenID Connect UserInfo Endpoint",
description="""
OpenID Connect UserInfo endpoint as defined in OIDC Core 1.0 Section 5.3.
Returns user claims based on the access token and granted scopes.
**Requirements:**
- Valid access token with 'openid' scope
- Token must be active and not revoked
**Scopes and Claims:**
- `openid`: Required scope, returns 'sub' claim
- `profile`: Returns profile claims (name, given_name, family_name, etc.)
- `email`: Returns email and email_verified claims
- `phone`: Returns phone_number and phone_number_verified claims
- `address`: Returns address claim
**Security:**
- Bearer token authentication required
- Only returns claims for granted scopes
- Respects user privacy through scope-based filtering
""",
responses={
200: {
"description": "User claims based on granted scopes",
"content": {
"application/json": {
"example": {
"sub": "123e4567-e89b-12d3-a456-426614174000",
"name": "John Doe",
"given_name": "John",
"family_name": "Doe",
"email": "john.doe@example.com",
"email_verified": True,
"preferred_username": "johndoe",
"updated_at": 1640995200,
}
}
},
},
401: {"description": "Invalid or expired access token"},
403: {"description": "Insufficient scope (missing 'openid' scope)"},
500: {"description": "Internal server error"},
},
)
async def userinfo_endpoint(
current_user: UserModel = Depends(get_current_user),
token_scopes: list[str] = Depends(get_token_scopes),
userinfo_service: UserInfoService = Depends(get_userinfo_service),
) -> UserInfoResponse:
"""
OpenID Connect UserInfo endpoint.
Returns user claims based on the access token and granted scopes.
Args:
current_user: Current authenticated user
token_scopes: Scopes granted to the access token
userinfo_service: Service for generating UserInfo response
Returns:
UserInfoResponse: User claims filtered by granted scopes
Raises:
HTTPException: If request is invalid or user access is denied
"""
try:
# Validate UserInfo request (requires 'openid' scope)
if not userinfo_service.validate_userinfo_request(token_scopes):
logger.warning(f"UserInfo request without 'openid' scope for user {current_user.id}")
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="UserInfo endpoint requires 'openid' scope",
headers={"WWW-Authenticate": 'Bearer scope="openid"'},
)
# Generate UserInfo response
userinfo_response = userinfo_service.create_userinfo_response(user=current_user, granted_scopes=token_scopes)
logger.info(f"UserInfo response generated for user {current_user.id} with scopes {token_scopes}")
return userinfo_response
except HTTPException:
# Re-raise HTTP exceptions without modification
raise
except Exception as e:
logger.error(f"Error generating UserInfo response for user {current_user.id}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to generate UserInfo response"
) from None
@oidc_router.put(
"/oidc/userinfo",
response_model=UserInfoResponse,
summary="Update OpenID Connect UserInfo",
description="""
Update user profile information via OpenID Connect UserInfo endpoint.
This endpoint allows users to update their profile information using OIDC standard claims.
Only claims allowed by the granted scopes can be updated, and only standard OIDC claims
are permitted for security and compliance reasons.
**Requirements:**
- Valid access token with 'openid' scope
- Token must be active and not revoked
- Update request must contain only OIDC standard claims
**Updatable Claims by Scope:**
- `profile`: name, given_name, family_name, middle_name, nickname, preferred_username,
profile, picture, website, gender, birthdate, zoneinfo, locale
- `phone`: phone_number (verification status cannot be changed by users)
- `address`: address information
**Security Restrictions:**
- email, email_verified, phone_number_verified are NOT updatable by users
- Only claims for granted scopes can be updated
- Non-OIDC custom claims are rejected
**Behavior:**
- Returns updated UserInfo response after successful update
- Only updates fields provided in the request (partial updates supported)
- Validates all claims against OIDC standards
""",
responses={
200: {
"description": "User information updated successfully",
"content": {
"application/json": {
"example": {
"sub": "123e4567-e89b-12d3-a456-426614174000",
"name": "John Smith",
"given_name": "John",
"family_name": "Smith",
"email": "john.doe@example.com",
}
}
},
},
400: {"description": "Invalid update request or non-OIDC claims provided"},
401: {"description": "Invalid or expired access token"},
403: {"description": "Insufficient scope (missing 'openid' scope or scope for requested claims)"},
500: {"description": "Internal server error"},
},
)
async def update_userinfo_endpoint(
update_request: UserInfoUpdateRequest,
current_user: UserModel = Depends(get_current_user),
token_scopes: list[str] = Depends(get_token_scopes),
userinfo_service: UserInfoService = Depends(get_userinfo_service),
user_service=Depends(get_user_service),
) -> UserInfoResponse:
"""
Update user profile information via OpenID Connect UserInfo endpoint.
This endpoint provides OIDC-compliant user profile updates, allowing users to
modify their profile information using standard OIDC claims based on granted scopes.
Args:
update_request: UserInfo update request with OIDC standard claims
current_user: Current authenticated user
token_scopes: Scopes granted to the access token
userinfo_service: Service for validating UserInfo operations
user_service: Service for updating user data
Returns:
UserInfoResponse: Updated user claims filtered by granted scopes
Raises:
HTTPException: If request is invalid, user access is denied, or update fails
"""
try:
# Validate UserInfo update request and get allowed fields
try:
validated_updates = userinfo_service.validate_userinfo_update_request(token_scopes, update_request)
except ValueError as e:
logger.warning(f"UserInfo update validation failed for user {current_user.id}: {e}")
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=str(e),
headers={"WWW-Authenticate": 'Bearer scope="openid"'},
) from None
if not validated_updates:
logger.info(f"No valid updates provided for user {current_user.id}")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="No valid OIDC claims provided for update, or insufficient scope for requested claims",
)
# Update user data through UserService
updated_user = await user_service.update_user(
user_id=current_user.id,
update_data=validated_updates,
requesting_user=current_user,
admin_override=False,
)
# Generate updated UserInfo response
updated_userinfo = userinfo_service.create_userinfo_response(user=updated_user, granted_scopes=token_scopes)
logger.info(
f"UserInfo updated successfully for user {current_user.id}, fields: {list(validated_updates.keys())}"
)
return updated_userinfo
except HTTPException:
# Re-raise HTTP exceptions without modification
raise
except Exception as e:
logger.error(f"Error updating UserInfo for user {current_user.id}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to update UserInfo"
) from None
@oidc_router.get(
"/.well-known/jwks.json",
summary="JSON Web Key Set (JWKS)",
description="""
JSON Web Key Set endpoint as defined in RFC 7517.
Returns public keys that clients can use to verify ID token signatures.
This endpoint is essential for OpenID Connect ID token verification.
**Key Features:**
- RSA public keys in JWK format
- Support for key rotation
- Proper HTTP caching headers
- No authentication required (public endpoint)
**Usage:**
- Clients fetch this endpoint to get verification keys
- Keys are used to verify ID token signatures
- Cache-Control headers optimize performance
**Security:**
- Only public keys are exposed
- No authentication required as per OIDC specification
- Supports key rotation for enhanced security
""",
responses={
200: {
"description": "JSON Web Key Set with public keys",
"content": {
"application/json": {
"example": {
"keys": [
{
"kty": "RSA",
"use": "sig",
"alg": "RS256",
"kid": "key_20250709123456",
"n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbPFRP1fOL...",
"e": "AQAB",
}
]
}
}
},
},
500: {"description": "Internal server error"},
},
)
async def jwks_endpoint():
"""
JSON Web Key Set endpoint.
Returns public keys for ID token signature verification according to
RFC 7517 and OpenID Connect Core 1.0 specification.
Returns:
JWKSModel: JSON Web Key Set with public keys
Raises:
HTTPException: If JWKS generation fails
"""
try:
from authly.oidc.jwks import get_jwks_response
# Get JWKS response
jwks_response = get_jwks_response()
logger.info("JWKS endpoint accessed successfully")
# Return with proper caching headers
from fastapi.responses import JSONResponse
# Get cache config from dependency injection (fallback for test compatibility)
try:
from authly.core.dependencies import get_config
config = get_config()
cache_max_age = config.jwks_cache_max_age_seconds
except Exception:
# Fallback for tests or when config is not available
cache_max_age = 3600
return JSONResponse(
content=jwks_response,
headers={
"Cache-Control": f"public, max-age={cache_max_age}",
},
)
except Exception as e:
logger.error(f"Error generating JWKS response: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to generate JWKS response"
) from None
@oidc_router.get(
"/oidc/logout",
summary="OpenID Connect End Session",
description="""
OpenID Connect End Session endpoint as defined in OIDC Session Management 1.0.
This endpoint enables OIDC-compliant logout flows for browser-based applications.
Unlike the API logout endpoint (/auth/logout), this endpoint handles query parameters
and supports HTTP redirects for browser clients.
**Parameters:**
- `id_token_hint`: Optional ID token hint for client validation
- `post_logout_redirect_uri`: Optional URI to redirect after logout
- `state`: Optional state parameter for client-side flow management
**Behavior:**
- Terminates user session and invalidates tokens
- Validates redirect URI against client registration if provided
- Returns HTTP redirect or success page (not JSON)
- Preserves state parameter for client security
**Security:**
- Validates id_token_hint if provided
- Ensures redirect URI is registered with the client
- Prevents open redirect vulnerabilities
- Compatible with existing /auth/logout for API clients
""",
responses={
200: {"description": "Logout successful (HTML response or redirect)"},
302: {"description": "Redirect to post_logout_redirect_uri"},
400: {"description": "Invalid request parameters"},
500: {"description": "Internal server error"},
},
)
async def oidc_end_session(
request: Request,
id_token_hint: str | None = Query(None, description="Optional ID token hint for client validation"),
post_logout_redirect_uri: str | None = Query(None, description="URI to redirect after logout"),
state: str | None = Query(None, description="State parameter for client flow"),
token_service: TokenService = Depends(get_token_service),
client_repository: ClientRepository = Depends(get_client_repository),
):
"""
OpenID Connect End Session endpoint.
Provides OIDC-compliant logout functionality for browser-based applications.
This endpoint complements the existing /auth/logout API endpoint by handling
query parameters and HTTP redirects instead of Bearer tokens and JSON responses.
Args:
request: FastAPI request object
id_token_hint: Optional ID token for client validation
post_logout_redirect_uri: Optional redirect URI after logout
state: Optional state parameter for client security
token_service: Token service for session invalidation
client_repository: Client repository for redirect URI validation
Returns:
RedirectResponse: If post_logout_redirect_uri is valid
HTMLResponse: Success page if no redirect URI
Raises:
HTTPException: If request parameters are invalid
"""
try:
# Extract user information from session/cookies if available
user_id = None
client_id = None
# Validate id_token_hint if provided
if id_token_hint:
try:
from authly.core.dependencies import get_config
config = get_config()
IDTokenGenerator(config)
# Decode token without full validation (we just need client_id)
from jose import jwt
# Get claims without verification for hint processing
unverified_claims = jwt.get_unverified_claims(id_token_hint)
if "aud" in unverified_claims:
# Get client_id from audience claim (can be string or list)
aud = unverified_claims["aud"]
client_id = aud[0] if isinstance(aud, list) else aud
logger.info(f"OIDC logout with id_token_hint for client: {client_id}")
if "sub" in unverified_claims:
user_id = unverified_claims["sub"]
logger.info(f"OIDC logout for user: {user_id}")
except Exception as e:
logger.warning(f"Invalid id_token_hint provided: {e}")
# Continue with logout even if token hint is invalid
pass
# Validate post_logout_redirect_uri if provided
if post_logout_redirect_uri:
if client_id:
# Get client to validate redirect URI
try:
client = await client_repository.get_by_client_id(client_id)
if client and not client.is_redirect_uri_allowed(post_logout_redirect_uri):
logger.warning(
f"Invalid post_logout_redirect_uri for client {client_id}: {post_logout_redirect_uri}"
)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid post_logout_redirect_uri for this client",
)
except Exception:
# If we can't validate, be conservative and reject
logger.warning(f"Could not validate redirect URI for client {client_id}")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST, detail="Could not validate post_logout_redirect_uri"
) from None
else:
# No client_id from token hint, cannot validate redirect URI
logger.warning("post_logout_redirect_uri provided without valid client identification")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="post_logout_redirect_uri requires valid id_token_hint",
)
# Perform session termination
invalidated_count = 0
if user_id:
try:
# Use token service to invalidate user sessions
# Note: We don't have the specific token, so we invalidate all user tokens
from uuid import UUID
user_uuid = UUID(user_id)
# Invalidate all tokens for the user (similar to /auth/logout)
invalidated_count = await token_service.invalidate_user_tokens(user_uuid)
logger.info(f"OIDC logout: invalidated {invalidated_count} tokens for user {user_id}")
except Exception as e:
logger.error(f"Error during OIDC logout for user {user_id}: {e}")
# Continue to show success page even if token invalidation fails
# This prevents information disclosure about token existence
pass
else:
# No user_id available, but that's okay for OIDC logout
# The user might be logged out already or session expired
logger.info("OIDC logout without user identification (session may already be expired)")
# Handle redirect or success response
if post_logout_redirect_uri:
# Build redirect URL with state parameter if provided
redirect_url = post_logout_redirect_uri
if state:
separator = "&" if "?" in redirect_url else "?"
redirect_url = f"{redirect_url}{separator}state={state}"
logger.info(f"OIDC logout redirecting to: {redirect_url}")
return RedirectResponse(url=redirect_url, status_code=status.HTTP_302_FOUND)
else:
# Return HTML success page
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Logout Successful</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
.container { max-width: 500px; margin: 0 auto; padding: 20px; }
.success { color: #28a745; }
</style>
</head>
<body>
<div class="container">
<h1 class="success">Logout Successful</h1>
<p>You have been successfully logged out.</p>
<p>It is safe to close this browser window.</p>
</div>
</body>
</html>
"""
logger.info("OIDC logout completed with success page")
return HTMLResponse(content=html_content, status_code=status.HTTP_200_OK)
except HTTPException:
# Re-raise HTTP exceptions without modification
raise
except Exception as e:
logger.error(f"Unexpected error during OIDC logout: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to complete logout request"
) from None
@oidc_router.get(
"/oidc/session/iframe",
summary="OIDC Session Management iframe",
description="""
Session management iframe endpoint as defined in OIDC Session Management 1.0.
This endpoint serves an iframe that enables client-side session monitoring
for Single Page Applications (SPAs) and browser-based OIDC clients.
**Features:**
- JavaScript-based session state monitoring
- Cross-origin message handling for session events
- Client-side session validation support
- Real-time session status updates
**Usage:**
- Clients embed this iframe for session monitoring
- JavaScript communicates session changes via postMessage
- Supports silent re-authentication flows
- Essential for OIDC session management compliance
**Security:**
- Same-origin policy enforcement
- Secure cross-origin communication
- No authentication required (public iframe)
- Session state validation via secure methods
""",
responses={
200: {"description": "Session management iframe HTML"},
500: {"description": "Internal server error"},
},
)
async def oidc_session_iframe(request: Request):
"""
OIDC Session Management iframe endpoint.
Serves an HTML iframe that enables client-side session monitoring
according to OIDC Session Management 1.0 specification.
Args:
request: FastAPI request object
Returns:
HTMLResponse: Session management iframe
Raises:
HTTPException: If iframe generation fails
"""
try:
# Get base URL for iframe communication
get_base_url(request)
# Session management iframe HTML content
iframe_html = """
<!DOCTYPE html>
<html>
<head>
<title>OIDC Session Management</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { margin: 0; padding: 0; background: transparent; }
</style>
</head>
<body>
<script type="text/javascript">
(function() {
'use strict';
// Session management state
var sessionState = null;
var clientOrigin = null;
var checkSessionInterval = null;
// Initialize session monitoring
function initSessionMonitoring() {
// Listen for messages from parent window
window.addEventListener('message', handleMessage, false);
// Send ready signal to parent
if (window.parent !== window) {
window.parent.postMessage('oidc-session-iframe-ready', '*');
}
}
// Handle incoming messages from client
function handleMessage(event) {
try {
// Validate message format
if (typeof event.data !== 'string') {
return;
}
var parts = event.data.split(' ');
if (parts.length !== 3 || parts[0] !== 'oidc-session-check') {
return;
}
var clientId = parts[1];
var newSessionState = parts[2];
var origin = event.origin;
// Store client origin for future communication
if (!clientOrigin) {
clientOrigin = origin;
}
// Check session state
checkSessionState(clientId, newSessionState, origin);
} catch (error) {
console.error('OIDC session iframe error:', error);
}
}
// Check current session state
function checkSessionState(clientId, newSessionState, origin) {
try {
// Simple session state comparison
// In a full implementation, this would check against server session
var currentState = getCurrentSessionState(clientId);
var result;
if (currentState === newSessionState) {
result = 'unchanged';
} else {
result = 'changed';
sessionState = newSessionState;
}
// Send result back to client
event.source.postMessage(result, origin);
} catch (error) {
console.error('Session state check error:', error);
event.source.postMessage('error', origin);
}
}
// Get current session state (simplified implementation)
function getCurrentSessionState(clientId) {
// In a full implementation, this would:
// 1. Check server-side session status
// 2. Validate client authentication
// 3. Return proper session state hash
// For now, return stored session state
return sessionState || 'logged-out';
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initSessionMonitoring);
} else {
initSessionMonitoring();
}
})();
</script>
</body>
</html>
"""
logger.info("OIDC session iframe served successfully")
return HTMLResponse(
content=iframe_html,
status_code=status.HTTP_200_OK,
headers={
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
"X-Frame-Options": "SAMEORIGIN", # Allow iframe embedding
},
)
except Exception as e:
logger.error(f"Error serving OIDC session iframe: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to serve session management iframe"
) from None
@oidc_router.get(
"/oidc/session/check",
summary="OIDC Session Status Check",
description="""
Session status check endpoint for OIDC Session Management 1.0.
This endpoint enables client applications to check the current session status
without requiring full re-authentication. Used by SPAs and browser clients
for silent session validation.
**Features:**
- Session status validation without authentication prompts
- Support for silent re-authentication flows
- Client-side session monitoring integration
- OIDC-compliant session state responses
**Parameters:**
- No authentication required for basic status checks
- Optional client_id for client-specific session validation
- Session state validation via secure methods
**Responses:**
- Session status indicators (active, expired, invalid)
- Client-specific session information
- Silent re-authentication triggers when needed
**Security:**
- No sensitive data exposure in responses
- Rate limiting applied for abuse prevention
- Secure session state validation methods
""",
responses={
200: {"description": "Session status information"},
400: {"description": "Invalid request parameters"},
500: {"description": "Internal server error"},
},
)
async def oidc_session_check(
request: Request,
client_id: str | None = Query(None, description="Client ID for session validation"),
):
"""
OIDC Session Status Check endpoint.
Provides session status information for client applications without
requiring full authentication according to OIDC Session Management 1.0.
Args:
request: FastAPI request object
client_id: Optional client ID for validation
Returns:
dict: Session status information
Raises:
HTTPException: If session check fails
"""
try:
# Extract session information from request
# This is a simplified implementation - in production, you'd check:
# 1. Browser cookies for session tokens
# 2. Server-side session storage
# 3. Client-specific session state
session_status = {
"session_state": "unknown",
"authenticated": False,
"client_id": client_id,
"check_time": datetime.now(UTC).isoformat(),
}
# Check for session indicators (cookies, headers, etc.)
# This is simplified - in a full implementation:
# - Check session cookies
# - Validate session tokens
# - Check server-side session storage
# - Return proper session state
# Simple session detection based on common patterns
has_session_cookie = "session" in request.cookies or "sessionid" in request.cookies
has_auth_header = "authorization" in request.headers
if has_session_cookie or has_auth_header:
session_status.update(
{
"session_state": "active",
"authenticated": True,
}
)
else:
session_status.update(
{
"session_state": "logged_out",
"authenticated": False,
}
)
logger.info(f"OIDC session check for client: {client_id}, status: {session_status['session_state']}")
return session_status
except Exception as e:
logger.error(f"Error during OIDC session check: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to check session status"
) from None
@oidc_router.get(
"/oidc/frontchannel/logout",
summary="OIDC Front-Channel Logout",
description="""
Front-channel logout endpoint as defined in OIDC Front-Channel Logout 1.0.
This endpoint enables coordinated logout across multiple OIDC clients
using front-channel (browser-based) communication. When a user logs out
from one client, all participating clients can be notified.
**Features:**
- Cross-client logout coordination
- Browser-based logout notification
- Support for multiple simultaneous client sessions
- OIDC Front-Channel Logout specification compliance
**Parameters:**
- `iss`: Issuer identifier for validation
- `sid`: Session ID for logout coordination
**Behavior:**
- Validates logout request parameters
- Coordinates logout across registered clients
- Provides iframe-based logout notifications
- Maintains security during cross-client communication
**Security:**
- Validates issuer and session parameters
- Prevents unauthorized logout requests
- Secure cross-origin communication
- Rate limiting for abuse prevention
""",
responses={
200: {"description": "Front-channel logout response"},
400: {"description": "Invalid request parameters"},
500: {"description": "Internal server error"},
},
)
async def oidc_frontchannel_logout(
request: Request,
iss: str | None = Query(None, description="Issuer identifier"),
sid: str | None = Query(None, description="Session identifier"),
):
"""
OIDC Front-Channel Logout endpoint.
Handles front-channel logout requests to coordinate logout across
multiple OIDC clients according to the Front-Channel Logout specification.
Args:
request: FastAPI request object
iss: Issuer identifier for validation
sid: Session identifier for logout coordination
Returns:
HTMLResponse: Front-channel logout response
Raises:
HTTPException: If logout coordination fails
"""
try:
# Get base URL for issuer validation
base_url = get_base_url(request)
# Validate issuer parameter
if iss and iss != base_url:
logger.warning(f"Invalid issuer in front-channel logout: {iss}, expected: {base_url}")
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid issuer parameter")
# Process front-channel logout
# In a full implementation, this would:
# 1. Validate session ID
# 2. Identify all clients for this session
# 3. Generate logout notifications for each client
# 4. Return appropriate response
# Sanitize user inputs to prevent XSS
import html
import json
safe_iss = html.escape(iss or base_url, quote=True)
safe_sid = html.escape(sid or "unknown", quote=True)
# Use JSON encoding for safe JavaScript variable injection
js_iss = json.dumps(safe_iss)
js_sid = json.dumps(safe_sid)
logout_response = f"""<!DOCTYPE html>
<html>
<head>
<title>Front-Channel Logout</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'unsafe-inline';">
</head>
<body>
<h1>Logout Processing</h1>
<p>Processing logout request...</p>
<script type="text/javascript">
(function() {{
'use strict';
// Safely injected variables (JSON encoded to prevent XSS)
var issuer = {js_iss};
var sessionId = {js_sid};
// Front-channel logout processing
var logoutComplete = false;
function processLogout() {{
try {{
// In a full implementation, this would:
// 1. Send logout notifications to all registered clients
// 2. Clear local session state
// 3. Coordinate cross-client logout
console.log('Processing front-channel logout');
console.log('Issuer:', issuer);
console.log('Session ID:', sessionId);
// Simulate logout processing
setTimeout(function() {{
logoutComplete = true;
updateStatus('Logout completed successfully');
}}, 1000);
}} catch (error) {{
console.error('Front-channel logout error:', error);
updateStatus('Logout processing error');
}}
}}
function updateStatus(message) {{
var statusElement = document.querySelector('p');
if (statusElement) {{
statusElement.textContent = message;
}}
}}
// Start logout processing when DOM is ready
if (document.readyState === 'loading') {{
document.addEventListener('DOMContentLoaded', processLogout);
}} else {{
processLogout();
}}
}})();
</script>
</body>
</html>"""
logger.info(f"OIDC front-channel logout processed for issuer: {iss}, session: {sid}")
return HTMLResponse(
content=logout_response,
status_code=status.HTTP_200_OK,
headers={
"Cache-Control": "no-cache, no-store, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
},
)
except HTTPException:
# Re-raise HTTP exceptions without modification
raise
except Exception as e:
logger.error(f"Error during OIDC front-channel logout: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Unable to process front-channel logout"
) from None
"""
OpenID Connect (OIDC) API Router.
Provides OpenID Connect 1.0 endpoints including discovery, UserInfo, and JWKS.
"""
import logging
from datetime import UTC, datetime
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
from fastapi.responses import HTMLResponse, RedirectResponse
from authly.api.auth_dependencies import get_client_repository
from authly.api.oauth_router import get_discovery_service
from authly.api.users_dependencies import get_current_user, get_token_scopes, get_user_service, get_userinfo_service
from authly.oauth.client_repository import ClientRepository
from authly.oauth.discovery_service import DiscoveryService
from authly.oidc.discovery import OIDCDiscoveryService, OIDCServerMetadata
from authly.oidc.id_token import IDTokenGenerator
from authly.oidc.userinfo import UserInfoResponse, UserInfoService, UserInfoUpdateRequest
from authly.tokens import TokenService, get_token_service
from authly.users.models import UserModel
logger = logging.getLogger(__name__)
# Create OIDC router
oidc_router = APIRouter(tags=["OpenID Connect"])
def get_base_url(request: Request) -> str:
"""
Extract base URL from request.
Args:
request: FastAPI request object
Returns:
Base URL as string
"""
# Handle both direct access and reverse proxy scenarios
if request.headers.get("x-forwarded-proto"):
scheme = request.headers.get("x-forwarded-proto", "https")
else:
scheme = request.url.scheme
if request.headers.get("x-forwarded-host"):
host = request.headers.get("x-forwarded-host")
else:
host = request.headers.get("host", request.url.netloc)
return f"{scheme}://{host}"
Xet Storage Details
- Size:
- 43.2 kB
- Xet hash:
- 9300cd49c3007b65887bde13611d592930ebfb2d3ad356a81a40e51558feff14
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.