@@ -489,37 +489,37 @@ async def cached_fief_user_validation(token: str) -> Optional[FiefUserInfo]:
489
489
return None
490
490
491
491
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 )
507
507
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" )
510
512
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
515
514
516
- return user_info
517
515
518
- return _current_user
516
+ def get_cached_fief_user_optional ():
517
+ """Dependency factory for optional cached Fief user"""
519
518
519
+ async def _get_user (request : Request ) -> Optional [FiefUserInfo ]:
520
+ return await cached_fief_current_user (request , optional = True )
520
521
521
- # Create cached auth instance
522
- cached_auth = CachedFiefAuth (fief , oauth2_scheme )
522
+ return _get_user
523
523
524
524
525
525
class AuthInfo (BaseModel ):
@@ -532,7 +532,7 @@ class AuthInfo(BaseModel):
532
532
533
533
534
534
async 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 ( )),
536
536
api_key : Optional [str ] = Header (None , alias = "X-API-Key" ),
537
537
) -> AuthInfo :
538
538
"""
0 commit comments