@@ -489,37 +489,37 @@ async def cached_fief_user_validation(token: str) -> Optional[FiefUserInfo]:
489489 return None
490490
491491
492- class CachedFiefAuth ( FiefAuth ):
493- """Fief authentication with caching to reduce server load"""
494-
495- async def current_user ( self , optional : bool = False ):
496- """Override to use cached validation"""
497-
498- async def _current_user ( request : Request ):
499- # Extract token from request
500- authorization = request . headers . get ( "Authorization" )
501- if not authorization or not authorization . startswith ( "Bearer " ) :
502- if optional :
503- return None
504- raise HTTPException (
505- status_code = 401 , detail = "Missing or invalid authorization header"
506- )
492+ async def cached_fief_current_user (
493+ request : Request , optional : bool = False
494+ ) -> Optional [ FiefUserInfo ]:
495+ """
496+ Cached Fief user validation dependency function
497+ """
498+ # Extract token from request
499+ authorization = request . headers . get ( "Authorization" )
500+ if not authorization or not authorization . startswith ( "Bearer " ):
501+ if optional :
502+ return None
503+ raise HTTPException ( status_code = 401 , detail = "Missing or invalid authorization header" )
504+
505+ token = authorization . split ( " " , 1 )[ 1 ]
506+ user_info = await cached_fief_user_validation ( token )
507507
508- token = authorization .split (" " , 1 )[1 ]
509- user_info = await cached_fief_user_validation (token )
508+ if not user_info :
509+ if optional :
510+ return None
511+ raise HTTPException (status_code = 401 , detail = "Invalid or expired token" )
510512
511- if not user_info :
512- if optional :
513- return None
514- raise HTTPException (status_code = 401 , detail = "Invalid or expired token" )
513+ return user_info
515514
516- return user_info
517515
518- return _current_user
516+ def get_cached_fief_user_optional ():
517+ """Dependency factory for optional cached Fief user"""
519518
519+ async def _get_user (request : Request ) -> Optional [FiefUserInfo ]:
520+ return await cached_fief_current_user (request , optional = True )
520521
521- # Create cached auth instance
522- cached_auth = CachedFiefAuth (fief , oauth2_scheme )
522+ return _get_user
523523
524524
525525class AuthInfo (BaseModel ):
@@ -532,7 +532,7 @@ class AuthInfo(BaseModel):
532532
533533
534534async def flexible_auth (
535- fief_user : Optional [FiefUserInfo ] = Depends (cached_auth . current_user ( optional = True )),
535+ fief_user : Optional [FiefUserInfo ] = Depends (get_cached_fief_user_optional ( )),
536536 api_key : Optional [str ] = Header (None , alias = "X-API-Key" ),
537537) -> AuthInfo :
538538 """
0 commit comments