11import warnings
2+ from collections .abc import AsyncGenerator , Iterable
23from contextlib import asynccontextmanager
34from enum import auto , Enum
45from hashlib import sha512
5- from typing import Any , AsyncGenerator , cast , Dict , Iterable , Literal , Optional , Type , Union
6+ from typing import Any , Literal
67
78from itsdangerous import BadSignature , SignatureExpired , URLSafeTimedSerializer
89from quart import (
@@ -47,7 +48,7 @@ class Action(Enum):
4748
4849class _AuthSerializer (URLSafeTimedSerializer ):
4950 def __init__ (
50- self , secret : Union [ str , bytes , Iterable [str ], Iterable [bytes ]] , salt : Union [ str , bytes ]
51+ self , secret : str | bytes | Iterable [str ] | Iterable [bytes ], salt : str | bytes
5152 ) -> None :
5253 super ().__init__ (secret , salt , signer_kwargs = {"digest_method" : sha512 })
5354
@@ -59,12 +60,12 @@ class AuthUser:
5960 inherit from this.
6061 """
6162
62- def __init__ (self , auth_id : Optional [ str ] , action : Action = Action .PASS ) -> None :
63+ def __init__ (self , auth_id : str | None , action : Action = Action .PASS ) -> None :
6364 self ._auth_id = auth_id
6465 self .action = action
6566
6667 @property
67- def auth_id (self ) -> Optional [ str ] :
68+ def auth_id (self ) -> str | None :
6869 return self ._auth_id
6970
7071 @property
@@ -81,21 +82,21 @@ class QuartAuth:
8182
8283 def __init__ (
8384 self ,
84- app : Optional [ Quart ] = None ,
85+ app : Quart | None = None ,
8586 * ,
8687 attribute_name : str = None ,
87- cookie_domain : Optional [ str ] = None ,
88- cookie_name : Optional [ str ] = None ,
89- cookie_path : Optional [ str ] = None ,
90- cookie_http_only : Optional [ bool ] = None ,
91- cookie_samesite : Optional [ Literal ["Strict" , "Lax" ]] = None ,
92- cookie_secure : Optional [ bool ] = None ,
93- duration : Optional [ int ] = None ,
94- mode : Optional [ Literal ["cookie" , "bearer" ]] = None ,
95- salt : Optional [ str ] = None ,
88+ cookie_domain : str | None = None ,
89+ cookie_name : str | None = None ,
90+ cookie_path : str | None = None ,
91+ cookie_http_only : bool | None = None ,
92+ cookie_samesite : Literal ["Strict" , "Lax" ] | None = None ,
93+ cookie_secure : bool | None = None ,
94+ duration : int | None = None ,
95+ mode : Literal ["cookie" , "bearer" ] | None = None ,
96+ salt : str | None = None ,
9697 singleton : bool = True ,
97- serializer_class : Optional [ Type [ _AuthSerializer ]] = None ,
98- user_class : Optional [ Type [ AuthUser ]] = None ,
98+ serializer_class : type [ _AuthSerializer ] | None = None ,
99+ user_class : type [ AuthUser ] | None = None ,
99100 ) -> None :
100101 self .attribute_name = attribute_name
101102 self .cookie_domain = cookie_domain
@@ -168,7 +169,7 @@ def resolve_user(self) -> AuthUser:
168169
169170 return self .user_class (auth_id )
170171
171- def load_cookie (self ) -> Optional [ str ] :
172+ def load_cookie (self ) -> str | None :
172173 try :
173174 token = ""
174175 if has_request_context ():
@@ -180,7 +181,7 @@ def load_cookie(self) -> Optional[str]:
180181 else :
181182 return self .load_token (token )
182183
183- def load_bearer (self ) -> Optional [ str ] :
184+ def load_bearer (self ) -> str | None :
184185 try :
185186 if has_request_context ():
186187 raw = request .headers ["Authorization" ]
@@ -194,14 +195,14 @@ def load_bearer(self) -> Optional[str]:
194195 token = raw [6 :].strip ()
195196 return self .load_token (token )
196197
197- def dump_token (self , auth_id : str , app : Optional [ Quart ] = None ) -> str :
198+ def dump_token (self , auth_id : str , app : Quart | None = None ) -> str :
198199 if app is None :
199200 app = current_app
200201
201202 serializer = self .serializer_class (app .secret_key , self .salt )
202203 return serializer .dumps (auth_id )
203204
204- def load_token (self , token : str , app : Optional [ Quart ] = None ) -> Optional [ str ] :
205+ def load_token (self , token : str , app : Quart | None = None ) -> str | None :
205206 if app is None :
206207 app = current_app
207208
@@ -212,7 +213,7 @@ def load_token(self, token: str, app: Optional[Quart] = None) -> Optional[str]:
212213
213214 keys .append (app .secret_key ) # itsdangerous expects current key at top
214215
215- serializer = self .serializer_class (keys , self .salt ) # type: ignore[arg-type]
216+ serializer = self .serializer_class (keys , self .salt )
216217 try :
217218 return serializer .loads (token , max_age = self .duration )
218219 except (BadSignature , SignatureExpired ):
@@ -230,9 +231,9 @@ async def after_request(self, response: Response) -> Response:
230231 response .delete_cookie (
231232 self .cookie_name ,
232233 domain = self .cookie_domain ,
233- httponly = cast ( bool , self .cookie_http_only ) ,
234+ httponly = self .cookie_http_only ,
234235 path = self .cookie_path ,
235- secure = cast ( bool , self .cookie_secure ) ,
236+ secure = self .cookie_secure ,
236237 samesite = self .cookie_samesite ,
237238 )
238239 elif user .action in {Action .WRITE , Action .WRITE_PERMANENT }:
@@ -252,14 +253,14 @@ async def after_request(self, response: Response) -> Response:
252253 token ,
253254 domain = self .cookie_domain ,
254255 max_age = max_age ,
255- httponly = cast ( bool , self .cookie_http_only ) ,
256+ httponly = self .cookie_http_only ,
256257 path = self .cookie_path ,
257- secure = cast ( bool , self .cookie_secure ) ,
258+ secure = self .cookie_secure ,
258259 samesite = self .cookie_samesite ,
259260 )
260261 return response
261262
262- async def after_websocket (self , response : Optional [ Response ] ) -> Optional [ Response ] :
263+ async def after_websocket (self , response : Response | None ) -> Response | None :
263264 user = self .load_user ()
264265 if self .mode == "bearer" :
265266 if user .action != Action .PASS :
@@ -330,8 +331,8 @@ async def authenticated_client(
330331 token ,
331332 path = self .cookie_path ,
332333 domain = self .cookie_domain ,
333- secure = cast ( bool , self .cookie_secure ) ,
334- httponly = cast ( bool , self .cookie_http_only ) ,
334+ secure = self .cookie_secure ,
335+ httponly = self .cookie_http_only ,
335336 samesite = self .cookie_samesite ,
336337 )
337338 yield
@@ -342,7 +343,7 @@ async def authenticated_client(
342343 domain = self .cookie_domain ,
343344 )
344345
345- def _template_context (self ) -> Dict [str , AuthUser ]:
346+ def _template_context (self ) -> dict [str , AuthUser ]:
346347 return {"current_user" : self .load_user ()}
347348
348349
0 commit comments