1+ """OIDC authentication module for validating JWTs."""
2+
13import json
24import logging
35import urllib .request
79import jwt
810from fastapi import HTTPException , Security , security , status
911from fastapi .security .base import SecurityBase
10- from pydantic import AnyHttpUrl
11- from starlette .exceptions import HTTPException
12- from starlette .status import HTTP_403_FORBIDDEN
12+ from pydantic import HttpUrl
1313
1414logger = logging .getLogger (__name__ )
1515
1616
1717@dataclass
1818class OpenIdConnectAuth :
19- openid_configuration_url : AnyHttpUrl
20- openid_configuration_internal_url : Optional [AnyHttpUrl ] = None
19+ """OIDC authentication class to generate auth handlers."""
20+
21+ openid_configuration_url : HttpUrl
22+ openid_configuration_internal_url : Optional [HttpUrl ] = None
2123 allowed_jwt_audiences : Optional [Sequence [str ]] = None
2224
2325 # Generated attributes
2426 auth_scheme : SecurityBase = field (init = False )
2527 jwks_client : jwt .PyJWKClient = field (init = False )
26- valid_token_dependency : Callable [..., Any ] = field (init = False )
28+ validated_user : Callable [..., Any ] = field (init = False )
29+ maybe_validated_user : Callable [..., Any ] = field (init = False )
2730
2831 def __post_init__ (self ):
32+ """Initialize the OIDC authentication class."""
2933 logger .debug ("Requesting OIDC config" )
30- with urllib .request .urlopen (
31- str (self .openid_configuration_internal_url or self .openid_configuration_url )
32- ) as response :
34+ origin_url = (
35+ self .openid_configuration_internal_url or self .openid_configuration_url
36+ )
37+ with urllib .request .urlopen (origin_url ) as response :
3338 if response .status != 200 :
3439 logger .error (
3540 "Received a non-200 response when fetching OIDC config: %s" ,
@@ -45,10 +50,10 @@ def __post_init__(self):
4550 openIdConnectUrl = str (self .openid_configuration_url ),
4651 auto_error = False ,
4752 )
48- self .user_or_none = self .build (auto_error = False )
49- self .valid_token_dependency = self .build (auto_error = True )
53+ self .validated_user = self ._build (auto_error = True )
54+ self .maybe_validated_user = self ._build (auto_error = False )
5055
51- def build (self , auto_error : bool = True ):
56+ def _build (self , auto_error : bool = True ):
5257 """Build a dependency for validating an OIDC token."""
5358
5459 def valid_token_dependency (
@@ -59,7 +64,8 @@ def valid_token_dependency(
5964 if not auth_header :
6065 if auto_error :
6166 raise HTTPException (
62- status_code = HTTP_403_FORBIDDEN , detail = "Not authenticated"
67+ status_code = status .HTTP_403_FORBIDDEN ,
68+ detail = "Not authenticated" ,
6369 )
6470 return None
6571
@@ -111,4 +117,6 @@ def valid_token_dependency(
111117
112118
113119class OidcFetchError (Exception ):
120+ """Error fetching OIDC configuration."""
121+
114122 pass
0 commit comments