1818from app .schemas .user import UserResponse
1919
2020# OAuth2 scheme for JWT token in Authorization header
21- reusable_oauth2 = OAuth2PasswordBearer (
22- tokenUrl = f"{ settings .api_v1_str } /admin/auth/login"
23- )
21+ reusable_oauth2 = OAuth2PasswordBearer (tokenUrl = f"{ settings .api_v1_str } /admin/auth/login" )
2422api_key_header = APIKeyHeader (name = "X-API-Key" , auto_error = False )
2523
2624
@@ -41,14 +39,14 @@ async def get_db() -> AsyncGenerator[AsyncSession, None]:
4139async def get_current_user (session : SessionDep , token : TokenDep ) -> User :
4240 """
4341 Dependency to get the current authenticated user from JWT.
44-
42+
4543 Raises:
4644 HTTPException: If token is invalid or user not found.
4745 """
4846 try :
4947 payload = security .verify_token (token )
5048 if payload is None :
51- raise HTTPException (
49+ raise HTTPException (
5250 status_code = status .HTTP_401_UNAUTHORIZED ,
5351 detail = "Could not validate credentials" ,
5452 headers = {"WWW-Authenticate" : "Bearer" },
@@ -59,20 +57,19 @@ async def get_current_user(session: SessionDep, token: TokenDep) -> User:
5957 status_code = status .HTTP_403_FORBIDDEN ,
6058 detail = "Could not validate credentials" ,
6159 )
62-
60+
6361 user = await user_repo .get (session , token_data .sub ) # type: ignore # sub is UUID in string
6462 if not user :
6563 raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = "User not found" )
66-
64+
6765 if not user .is_active :
6866 raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = "Inactive user" )
69-
67+
7068 return user
7169
7270
7371async def get_current_client (
74- session : SessionDep ,
75- api_key : str = Security (api_key_header )
72+ session : SessionDep , api_key : str = Security (api_key_header )
7673) -> Client :
7774 """
7875 Dependency to authenticate a B2B client via API Key.
@@ -82,28 +79,28 @@ async def get_current_client(
8279 status_code = status .HTTP_401_UNAUTHORIZED ,
8380 detail = "Missing X-API-Key header" ,
8481 )
85-
82+
8683 # verify key format
8784 key_hash = auth_client .hash_key (api_key )
88-
85+
8986 # Check DB
9087 db_key = await api_key_repo .get_by_hash (session , key_hash = key_hash )
9188 if not db_key or not db_key .is_valid :
92- raise HTTPException (
89+ raise HTTPException (
9390 status_code = status .HTTP_401_UNAUTHORIZED ,
9491 detail = "Invalid or expired API Key" ,
9592 )
96-
93+
9794 # Update last used (fire and forget / async task in real app)
9895 # db_key.last_used_at = datetime.utcnow()
99- # await session.commit()
100-
96+ # await session.commit()
97+
10198 if not db_key .client .is_license_valid :
10299 raise HTTPException (
103100 status_code = status .HTTP_403_FORBIDDEN ,
104101 detail = "Client license expired or inactive" ,
105102 )
106-
103+
107104 return db_key .client
108105
109106
@@ -113,9 +110,9 @@ def get_content_service() -> "ContentService":
113110 In a real app with more complex deps, this could initialize the service.
114111 """
115112 from app .services .content import ContentService , content_service as _content_service
113+
116114 return _content_service
117115
118116
119117CurrentUser = Annotated [User , Depends (get_current_user )]
120118CurrentClient = Annotated [Client , Depends (get_current_client )]
121-
0 commit comments