22
33import time
44from contextlib import suppress
5- from functools import partial
6- from json import loads
7- from typing import Callable , Dict , List , Mapping , Optional , Tuple , Union
8- from urllib .parse import parse_qs , urlencode , urlparse
5+ from typing import Callable , Dict , List , Optional , Tuple
6+ from urllib .parse import parse_qs , urlparse
97from uuid import uuid4
108
11- from httpx import QueryParams
9+ from httpx import QueryParams , Response
1210from jwt import get_algorithm_by_name
1311from typing_extensions import cast
1412
3331 decode_jwt ,
3432 generate_pkce_challenge ,
3533 generate_pkce_verifier ,
36- model_dump ,
3734 model_dump_json ,
3835 model_validate ,
3936 parse_auth_otp_response ,
5047 JWK ,
5148 AMREntry ,
5249 AuthChangeEvent ,
53- AuthenticatorAssuranceLevels ,
5450 AuthFlowType ,
5551 AuthMFAChallengeResponse ,
5652 AuthMFAEnrollResponse ,
8682 SignUpWithEmailAndPasswordCredentialsOptions ,
8783 SignUpWithPasswordCredentials ,
8884 SignUpWithPhoneAndPasswordCredentialsOptions ,
85+ SSOResponse ,
8986 Subscription ,
9087 UpdateUserOptions ,
9188 UserAttributes ,
@@ -361,7 +358,9 @@ async def sign_in_with_id_token(
361358 self ._notify_all_subscribers ("SIGNED_IN" , auth_response .session )
362359 return auth_response
363360
364- async def sign_in_with_sso (self , credentials : SignInWithSSOCredentials ):
361+ async def sign_in_with_sso (
362+ self , credentials : SignInWithSSOCredentials
363+ ) -> SSOResponse :
365364 """
366365 Attempts a single-sign on using an enterprise Identity Provider. A
367366 successful SSO attempt will redirect the current page to the identity
@@ -476,7 +475,7 @@ async def get_user_identities(self) -> IdentitiesResponse:
476475 return IdentitiesResponse (identities = response .user .identities or [])
477476 raise AuthSessionMissingError ()
478477
479- async def unlink_identity (self , identity : UserIdentity ):
478+ async def unlink_identity (self , identity : UserIdentity ) -> Response :
480479 session = await self .get_session ()
481480 if not session :
482481 raise AuthSessionMissingError ()
@@ -621,7 +620,7 @@ async def reauthenticate(self) -> AuthResponse:
621620 if not session :
622621 raise AuthSessionMissingError ()
623622
624- response = await self ._request (
623+ await self ._request (
625624 "GET" ,
626625 "reauthenticate" ,
627626 jwt = session .access_token ,
@@ -674,19 +673,20 @@ async def get_user(self, jwt: Optional[str] = None) -> Optional[UserResponse]:
674673 return parse_user_response (await self ._request ("GET" , "user" , jwt = jwt ))
675674
676675 async def update_user (
677- self , attributes : UserAttributes , options : UpdateUserOptions = {}
676+ self , attributes : UserAttributes , options : Optional [ UpdateUserOptions ] = None
678677 ) -> UserResponse :
679678 """
680679 Updates user data, if there is a logged in user.
681680 """
682681 session = await self .get_session ()
683682 if not session :
684683 raise AuthSessionMissingError ()
684+ update_options = options or {}
685685 response = await self ._request (
686686 "PUT" ,
687687 "user" ,
688688 body = attributes ,
689- redirect_to = options .get ("email_redirect_to" ),
689+ redirect_to = update_options .get ("email_redirect_to" ),
690690 jwt = session .access_token ,
691691 )
692692 user_response = parse_user_response (response )
@@ -762,7 +762,7 @@ async def refresh_session(
762762 session = await self ._call_refresh_token (refresh_token )
763763 return AuthResponse (session = session , user = session .user )
764764
765- async def sign_out (self , options : SignOutOptions = { "scope" : "global" } ) -> None :
765+ async def sign_out (self , options : Optional [ SignOutOptions ] = None ) -> None :
766766 """
767767 `sign_out` will remove the logged in user from the
768768 current session and log them out - removing all items from storage and then trigger a `"SIGNED_OUT"` event.
@@ -772,13 +772,14 @@ async def sign_out(self, options: SignOutOptions = {"scope": "global"}) -> None:
772772 There is no way to revoke a user's access token jwt until it expires.
773773 It is recommended to set a shorter expiry on the jwt for this reason.
774774 """
775+ signout_options = options or {"scope" : "global" }
775776 with suppress (AuthApiError ):
776777 session = await self .get_session ()
777778 access_token = session .access_token if session else None
778779 if access_token :
779- await self .admin .sign_out (access_token , options ["scope" ])
780+ await self .admin .sign_out (access_token , signout_options ["scope" ])
780781
781- if options ["scope" ] != "others" :
782+ if signout_options ["scope" ] != "others" :
782783 await self ._remove_session ()
783784 self ._notify_all_subscribers ("SIGNED_OUT" , None )
784785
@@ -802,31 +803,35 @@ def _unsubscribe() -> None:
802803 self ._state_change_emitters [unique_id ] = subscription
803804 return subscription
804805
805- async def reset_password_for_email (self , email : str , options : Options = {}) -> None :
806+ async def reset_password_for_email (
807+ self , email : str , options : Optional [Options ] = None
808+ ) -> None :
806809 """
807810 Sends a password reset request to an email address.
808811 """
812+ reset_options = options or {}
809813 await self ._request (
810814 "POST" ,
811815 "recover" ,
812816 body = {
813817 "email" : email ,
814818 "gotrue_meta_security" : {
815- "captcha_token" : options .get ("captcha_token" ),
819+ "captcha_token" : reset_options .get ("captcha_token" ),
816820 },
817821 },
818- redirect_to = options .get ("redirect_to" ),
822+ redirect_to = reset_options .get ("redirect_to" ),
819823 )
820824
821825 async def reset_password_email (
822826 self ,
823827 email : str ,
824- options : Options = {} ,
828+ options : Optional [ Options ] = None ,
825829 ) -> None :
826830 """
827831 Sends a password reset request to an email address.
828832 """
829- await self .reset_password_for_email (email , options )
833+
834+ await self .reset_password_for_email (email , options or {})
830835
831836 # MFA methods
832837
@@ -1090,7 +1095,7 @@ async def _start_auto_refresh_token(self, value: float) -> None:
10901095 if value <= 0 or not self ._auto_refresh_token :
10911096 return
10921097
1093- async def refresh_token_function ():
1098+ async def refresh_token_function () -> None :
10941099 self ._network_retries += 1
10951100 try :
10961101 session = await self .get_session ()
@@ -1275,7 +1280,7 @@ def __del__(self) -> None:
12751280 try :
12761281 # Try to cancel the timer
12771282 self ._refresh_token_timer .cancel ()
1278- except :
1283+ except Exception :
12791284 # Ignore errors if event loop is closed or selector is not registered
12801285 pass
12811286 finally :
0 commit comments