1- import logging
21import secrets
32from typing import List , NoReturn
43
98from fastapi .security import HTTPAuthorizationCredentials
109from fastapi .websockets import WebSocket
1110
12- from uiauth import enums , models , secure
13-
14- LOGGER = logging .getLogger ("uvicorn.default" )
11+ from uiauth import enums , logger , models , secure
1512
1613
1714def failed_auth_counter (request : Request ) -> None :
@@ -62,7 +59,7 @@ def raise_error(request: Request) -> NoReturn:
6259 request: Request object containing client information.
6360 """
6461 failed_auth_counter (request )
65- LOGGER .error (
62+ logger . CUSTOM_LOGGER .error (
6663 "Incorrect username or password: %d" ,
6764 models .ws_session .invalid [request .client .host ],
6865 )
@@ -88,16 +85,12 @@ def extract_credentials(authorization: HTTPAuthorizationCredentials) -> List[str
8885def verify_login (
8986 authorization : HTTPAuthorizationCredentials ,
9087 request : Request ,
91- env_username : str ,
92- env_password : str ,
9388) -> str | NoReturn :
9489 """Verifies authentication and generates session token for each user.
9590
9691 Args:
9792 authorization: Authorization header from the request.
9893 request: Request object containing client information.
99- env_username: Environment variable for the username.
100- env_password: Environment variable for the password.
10194
10295 Returns:
10396 str:
@@ -107,11 +100,11 @@ def verify_login(
107100 username , signature , timestamp = extract_credentials (authorization )
108101 else :
109102 raise_error (request )
110- if secrets .compare_digest (username , env_username ):
111- hex_user = secure .hex_encode (env_username )
112- hex_pass = secure .hex_encode (env_password )
103+ if secrets .compare_digest (username , models . env . username ):
104+ hex_user = secure .hex_encode (models . env . username )
105+ hex_pass = secure .hex_encode (models . env . password )
113106 else :
114- LOGGER . warning ("User '%s' not allowed" , username )
107+ logger . CUSTOM_LOGGER . warning ("User '%s' not allowed" , models . env . username )
115108 raise_error (request )
116109 message = f"{ hex_user } { hex_pass } { timestamp } "
117110 expected_signature = secure .calculate_hash (message )
@@ -151,18 +144,18 @@ def verify_session(
151144 and session_token
152145 and secrets .compare_digest (session_token , stored_token )
153146 ):
154- LOGGER .info ("Session is valid for host: %s" , request .client .host )
147+ logger . CUSTOM_LOGGER .info ("Session is valid for host: %s" , request .client .host )
155148 return
156149 elif not session_token :
157- LOGGER .warning (
150+ logger . CUSTOM_LOGGER .warning (
158151 "Session is invalid or expired for host: %s" , request .client .host
159152 )
160153 raise models .RedirectException (
161154 source = request .url .path ,
162155 destination = enums .APIEndpoints .fastapi_login ,
163156 )
164157 else :
165- LOGGER .warning (
158+ logger . CUSTOM_LOGGER .warning (
166159 "Session token mismatch for host: %s. Expected: %s, Received: %s" ,
167160 request .client .host ,
168161 stored_token ,
@@ -198,6 +191,6 @@ def clear_session(host: str) -> None:
198191 """
199192 if models .ws_session .client_auth .get (host ):
200193 models .ws_session .client_auth .pop (host )
201- LOGGER .info ("Session cleared for host: %s" , host )
194+ logger . CUSTOM_LOGGER .info ("Session cleared for host: %s" , host )
202195 else :
203- LOGGER .warning ("No session found for host: %s" , host )
196+ logger . CUSTOM_LOGGER .warning ("No session found for host: %s" , host )
0 commit comments