44from uuid import UUID , uuid4
55import asyncio
66from datetime import timedelta
7- from typing import Optional , Tuple , List , Callable
7+ from typing import Optional , Tuple , List
88from passlib import pwd
99from passlib .context import CryptContext
1010
4242PWD_CONTEXT = CryptContext (schemes = ["bcrypt" ], deprecated = "auto" )
4343
4444# Audiences
45+ CUSTOM_AUTH_AUD = "btrix:custom-auth"
4546AUTH_AUD = "btrix:auth"
4647RESET_AUD = "btrix:reset"
4748VERIFY_AUD = "btrix:verify"
@@ -121,14 +122,22 @@ def create_access_token(user: User) -> str:
121122
122123
123124# ============================================================================
124- def create_internal_crawler_access_token (sub : str , role : str ) -> str :
125+ def create_custom_jwt_token (sub : str , data : dict [ str , str ] ) -> str :
125126 """create jwt token for internal crawler access"""
126127 return generate_jwt (
127- {"sub" : sub , "internal_role" : role , " aud" : AUTH_AUD },
128+ {** data , "sub" : sub , "aud" : CUSTOM_AUTH_AUD },
128129 INTERNAL_JWT_TOKEN_LIFETIME ,
129130 )
130131
131132
133+ # ============================================================================
134+ def get_custom_jwt_token (request : Request ) -> dict [str , str ]:
135+ """return data from custom jwt token"""
136+ token = request .query_params .get ("auth_bearer" ) or ""
137+ payload = decode_jwt (token , [CUSTOM_AUTH_AUD ])
138+ return payload
139+
140+
132141# ============================================================================
133142def verify_password (plain_password : str , hashed_password : str ) -> bool :
134143 """verify password by hash"""
@@ -157,7 +166,7 @@ def generate_password() -> str:
157166
158167# ============================================================================
159168# pylint: disable=raise-missing-from
160- def init_jwt_auth (user_manager ) -> tuple [ Callable , Callable , Callable , Callable ] :
169+ def init_jwt_auth (user_manager ):
161170 """init jwt auth router + current_active_user dependency"""
162171 oauth2_scheme = OA2BearerOrQuery (tokenUrl = "/api/auth/jwt/login" , auto_error = False )
163172
@@ -167,8 +176,6 @@ async def get_current_user(
167176 try :
168177 payload = decode_jwt (token , AUTH_ALLOW_AUD )
169178 uid : Optional [str ] = payload .get ("sub" ) or payload .get ("user_id" )
170- # insure not an internal token
171- assert not payload .get ("internal_role" )
172179 user = await user_manager .get_by_id (UUID (uid ))
173180 assert user
174181 return user
@@ -194,17 +201,6 @@ async def shared_secret_or_superuser(
194201
195202 return user
196203
197- def get_custom_access (role : str ) -> Callable [[str ], str ]:
198- def get_access_dep (token : str = Depends (oauth2_scheme )) -> str :
199- payload = decode_jwt (token , AUTH_ALLOW_AUD )
200- sub = payload .get ("sub" )
201- if not sub or payload .get ("internal_role" ) != role :
202- raise HTTPException (status_code = 401 , detail = "invalid_credentials" )
203-
204- return sub
205-
206- return get_access_dep
207-
208204 current_active_user = get_current_user
209205
210206 auth_jwt_router = APIRouter ()
@@ -284,9 +280,4 @@ async def refresh_jwt(user=Depends(current_active_user)):
284280 user_info = await user_manager .get_user_info_with_orgs (user )
285281 return get_bearer_response (user , user_info )
286282
287- return (
288- auth_jwt_router ,
289- current_active_user ,
290- shared_secret_or_superuser ,
291- get_custom_access ,
292- )
283+ return auth_jwt_router , current_active_user , shared_secret_or_superuser
0 commit comments