From 8d64ca450b37f7063b7fc701ee3156d1677d4fc3 Mon Sep 17 00:00:00 2001 From: Aziz Berkay Yesilyurt Date: Tue, 6 Feb 2024 18:43:23 +0100 Subject: [PATCH 1/4] fix: add AuthOtpResponse --- gotrue/_sync/gotrue_client.py | 8 +++++--- gotrue/helpers.py | 20 ++++++-------------- gotrue/types.py | 6 ++++++ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/gotrue/_sync/gotrue_client.py b/gotrue/_sync/gotrue_client.py index c495f857..2799a6b2 100644 --- a/gotrue/_sync/gotrue_client.py +++ b/gotrue/_sync/gotrue_client.py @@ -31,6 +31,7 @@ model_dump_json, model_validate, parse_auth_response, + parse_auth_otp_response, parse_sso_response, parse_user_response, ) @@ -46,6 +47,7 @@ AuthMFAListFactorsResponse, AuthMFAUnenrollResponse, AuthMFAVerifyResponse, + AuthOtpResponse, AuthResponse, CodeExchangeParams, DecodedJWTDict, @@ -365,7 +367,7 @@ def unlink_identity(self, identity): def sign_in_with_otp( self, credentials: SignInWithPasswordlessCredentials, - ) -> AuthResponse: + ) -> AuthOtpResponse: """ Log in a user using magiclink or a one-time password (OTP). @@ -399,7 +401,7 @@ def sign_in_with_otp( }, }, redirect_to=email_redirect_to, - xform=parse_auth_response, + xform=parse_auth_otp_response, ) if phone: return self._request( @@ -413,7 +415,7 @@ def sign_in_with_otp( "captcha_token": captcha_token, }, }, - xform=parse_auth_response, + xform=parse_auth_otp_response, ) raise AuthInvalidCredentialsError( "You must provide either an email or phone number" diff --git a/gotrue/helpers.py b/gotrue/helpers.py index ba61a1af..fa482a38 100644 --- a/gotrue/helpers.py +++ b/gotrue/helpers.py @@ -14,9 +14,9 @@ from .errors import AuthApiError, AuthError, AuthRetryableError, AuthUnknownError from .types import ( AuthResponse, + AuthOtpResponse, GenerateLinkProperties, GenerateLinkResponse, - Session, SSOResponse, User, UserResponse, @@ -57,19 +57,11 @@ def model_dump_json(model: BaseModel) -> str: def parse_auth_response(data: Any) -> AuthResponse: - session: Union[Session, None] = None - if ( - "access_token" in data - and "refresh_token" in data - and "expires_in" in data - and data["access_token"] - and data["refresh_token"] - and data["expires_in"] - ): - session = model_validate(Session, data) - user_data = data.get("user", data) - user = model_validate(User, user_data) if user_data else None - return AuthResponse(session=session, user=user) + return model_validate(AuthResponse, data) + + +def parse_auth_otp_response(data: Any) -> AuthOtpResponse: + return model_validate(AuthOtpResponse, data) def parse_link_response(data: Any) -> GenerateLinkResponse: diff --git a/gotrue/types.py b/gotrue/types.py index 609f1011..ee260b04 100644 --- a/gotrue/types.py +++ b/gotrue/types.py @@ -89,6 +89,12 @@ class AuthResponse(BaseModel): session: Union[Session, None] = None +class AuthOtpResponse(BaseModel): + user: None = None + session: None = None + message_id: Union[str, None] = None + + class OAuthResponse(BaseModel): provider: Provider url: str From 7810bbd868f202adf2bef1e0a8573973f8b415be Mon Sep 17 00:00:00 2001 From: Aziz Berkay Yesilyurt Date: Wed, 7 Feb 2024 14:13:43 +0100 Subject: [PATCH 2/4] fix: move changes to _async --- gotrue/_async/gotrue_client.py | 8 +++++--- gotrue/_sync/gotrue_client.py | 10 +++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/gotrue/_async/gotrue_client.py b/gotrue/_async/gotrue_client.py index a29785bc..0d16e9ef 100644 --- a/gotrue/_async/gotrue_client.py +++ b/gotrue/_async/gotrue_client.py @@ -31,6 +31,7 @@ model_dump_json, model_validate, parse_auth_response, + parse_auth_otp_response, parse_sso_response, parse_user_response, ) @@ -47,6 +48,7 @@ AuthMFAUnenrollResponse, AuthMFAVerifyResponse, AuthResponse, + AuthOtpResponse, CodeExchangeParams, DecodedJWTDict, IdentitiesResponse, @@ -365,7 +367,7 @@ async def unlink_identity(self, identity): async def sign_in_with_otp( self, credentials: SignInWithPasswordlessCredentials, - ) -> AuthResponse: + ) -> AuthOtpResponse: """ Log in a user using magiclink or a one-time password (OTP). @@ -399,7 +401,7 @@ async def sign_in_with_otp( }, }, redirect_to=email_redirect_to, - xform=parse_auth_response, + xform=parse_auth_otp_response, ) if phone: return await self._request( @@ -413,7 +415,7 @@ async def sign_in_with_otp( "captcha_token": captcha_token, }, }, - xform=parse_auth_response, + xform=parse_auth_otp_response, ) raise AuthInvalidCredentialsError( "You must provide either an email or phone number" diff --git a/gotrue/_sync/gotrue_client.py b/gotrue/_sync/gotrue_client.py index 2799a6b2..563d5a53 100644 --- a/gotrue/_sync/gotrue_client.py +++ b/gotrue/_sync/gotrue_client.py @@ -47,8 +47,8 @@ AuthMFAListFactorsResponse, AuthMFAUnenrollResponse, AuthMFAVerifyResponse, - AuthOtpResponse, AuthResponse, + AuthOtpResponse, CodeExchangeParams, DecodedJWTDict, IdentitiesResponse, @@ -552,7 +552,9 @@ def set_session(self, access_token: str, refresh_token: str) -> AuthResponse: self._notify_all_subscribers("TOKEN_REFRESHED", session) return AuthResponse(session=session, user=response.user) - def refresh_session(self, refresh_token: Union[str, None] = None) -> AuthResponse: + def refresh_session( + self, refresh_token: Union[str, None] = None + ) -> AuthResponse: """ Returns a new session, regardless of expiry status. @@ -943,7 +945,9 @@ def _get_url_for_provider( if self._flow_type == "pkce": code_verifier = generate_pkce_verifier() code_challenge = generate_pkce_challenge(code_verifier) - self._storage.set_item(f"{self._storage_key}-code-verifier", code_verifier) + self._storage.set_item( + f"{self._storage_key}-code-verifier", code_verifier + ) code_challenge_method = ( "plain" if code_verifier == code_challenge else "s256" ) From face52175ce75bcb074680c9a464fbb0ee320ae5 Mon Sep 17 00:00:00 2001 From: Aziz Berkay Yesilyurt Date: Thu, 8 Feb 2024 09:39:59 +0100 Subject: [PATCH 3/4] revert changes to parse_auth_response --- gotrue/helpers.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gotrue/helpers.py b/gotrue/helpers.py index fa482a38..de0d319d 100644 --- a/gotrue/helpers.py +++ b/gotrue/helpers.py @@ -18,6 +18,7 @@ GenerateLinkProperties, GenerateLinkResponse, SSOResponse, + Session, User, UserResponse, ) @@ -57,7 +58,19 @@ def model_dump_json(model: BaseModel) -> str: def parse_auth_response(data: Any) -> AuthResponse: - return model_validate(AuthResponse, data) + session: Union[Session, None] = None + if ( + "access_token" in data + and "refresh_token" in data + and "expires_in" in data + and data["access_token"] + and data["refresh_token"] + and data["expires_in"] + ): + session = model_validate(Session, data) + user_data = data.get("user", data) + user = model_validate(User, user_data) if user_data else None + return AuthResponse(session=session, user=user) def parse_auth_otp_response(data: Any) -> AuthOtpResponse: From 97c47138215ee04af14478d913300032964f7a8e Mon Sep 17 00:00:00 2001 From: Aziz Berkay Yesilyurt Date: Thu, 8 Feb 2024 14:51:34 +0100 Subject: [PATCH 4/4] formatting --- gotrue/_async/gotrue_client.py | 4 ++-- gotrue/_sync/gotrue_client.py | 12 ++++-------- gotrue/helpers.py | 4 ++-- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/gotrue/_async/gotrue_client.py b/gotrue/_async/gotrue_client.py index 0d16e9ef..f34361ab 100644 --- a/gotrue/_async/gotrue_client.py +++ b/gotrue/_async/gotrue_client.py @@ -30,8 +30,8 @@ model_dump, model_dump_json, model_validate, - parse_auth_response, parse_auth_otp_response, + parse_auth_response, parse_sso_response, parse_user_response, ) @@ -47,8 +47,8 @@ AuthMFAListFactorsResponse, AuthMFAUnenrollResponse, AuthMFAVerifyResponse, - AuthResponse, AuthOtpResponse, + AuthResponse, CodeExchangeParams, DecodedJWTDict, IdentitiesResponse, diff --git a/gotrue/_sync/gotrue_client.py b/gotrue/_sync/gotrue_client.py index 563d5a53..7399d0f1 100644 --- a/gotrue/_sync/gotrue_client.py +++ b/gotrue/_sync/gotrue_client.py @@ -30,8 +30,8 @@ model_dump, model_dump_json, model_validate, - parse_auth_response, parse_auth_otp_response, + parse_auth_response, parse_sso_response, parse_user_response, ) @@ -47,8 +47,8 @@ AuthMFAListFactorsResponse, AuthMFAUnenrollResponse, AuthMFAVerifyResponse, - AuthResponse, AuthOtpResponse, + AuthResponse, CodeExchangeParams, DecodedJWTDict, IdentitiesResponse, @@ -552,9 +552,7 @@ def set_session(self, access_token: str, refresh_token: str) -> AuthResponse: self._notify_all_subscribers("TOKEN_REFRESHED", session) return AuthResponse(session=session, user=response.user) - def refresh_session( - self, refresh_token: Union[str, None] = None - ) -> AuthResponse: + def refresh_session(self, refresh_token: Union[str, None] = None) -> AuthResponse: """ Returns a new session, regardless of expiry status. @@ -945,9 +943,7 @@ def _get_url_for_provider( if self._flow_type == "pkce": code_verifier = generate_pkce_verifier() code_challenge = generate_pkce_challenge(code_verifier) - self._storage.set_item( - f"{self._storage_key}-code-verifier", code_verifier - ) + self._storage.set_item(f"{self._storage_key}-code-verifier", code_verifier) code_challenge_method = ( "plain" if code_verifier == code_challenge else "s256" ) diff --git a/gotrue/helpers.py b/gotrue/helpers.py index de0d319d..0c9a0613 100644 --- a/gotrue/helpers.py +++ b/gotrue/helpers.py @@ -13,12 +13,12 @@ from .errors import AuthApiError, AuthError, AuthRetryableError, AuthUnknownError from .types import ( - AuthResponse, AuthOtpResponse, + AuthResponse, GenerateLinkProperties, GenerateLinkResponse, - SSOResponse, Session, + SSOResponse, User, UserResponse, )