From 99c9409ef2398b10bb7563ca3cbde0b0204ec88d Mon Sep 17 00:00:00 2001 From: "joel@joellee.org" Date: Sun, 29 Oct 2023 13:27:07 +0800 Subject: [PATCH] feat: add sso --- gotrue/_async/gotrue_base_api.py | 1 + gotrue/_async/gotrue_client.py | 26 ++++++++++++++++++++++++++ gotrue/_sync/gotrue_base_api.py | 1 + gotrue/_sync/gotrue_client.py | 26 ++++++++++++++++++++++++++ gotrue/types.py | 11 +++++++++++ 5 files changed, 65 insertions(+) diff --git a/gotrue/_async/gotrue_base_api.py b/gotrue/_async/gotrue_base_api.py index f6ce7f0c..3aac9dd1 100644 --- a/gotrue/_async/gotrue_base_api.py +++ b/gotrue/_async/gotrue_base_api.py @@ -112,6 +112,7 @@ async def _request( ) response.raise_for_status() result = response if no_resolve_json else response.json() + print(response) if xform: return xform(result) except Exception as e: diff --git a/gotrue/_async/gotrue_client.py b/gotrue/_async/gotrue_client.py index 06be518d..e0a8421f 100644 --- a/gotrue/_async/gotrue_client.py +++ b/gotrue/_async/gotrue_client.py @@ -253,6 +253,32 @@ async def sign_in_with_password( self._notify_all_subscribers("SIGNED_IN", response.session) return response + async def sign_in_with_sso(self, credentials: SignInWithSSOCredentials): + await self._remove_session() + provider_id = credentials.get("provider_id") + domain = credentials.get("domain") + options = credentials.get("options", {}) + redirect_to = options.get("redirect_to") + captcha_token = options.get("captcha_token") + if domain: + return await self._request( + "POST", + "sso", + body={"domain": domain}, + redirect_to=redirect_to, + xform=parse_auth_response, + ) + if provider_id: + return await self._request( + "POST" "sso", + body={"provider_id": provider_id}, + redirect_to=redirect_to, + xform=parse_auth_response, + ) + raise AuthInvalidCredentialsError( + "You must provide either a domain or provider_id" + ) + async def sign_in_with_oauth( self, credentials: SignInWithOAuthCredentials, diff --git a/gotrue/_sync/gotrue_base_api.py b/gotrue/_sync/gotrue_base_api.py index 2a9b97fc..9aa504fd 100644 --- a/gotrue/_sync/gotrue_base_api.py +++ b/gotrue/_sync/gotrue_base_api.py @@ -112,6 +112,7 @@ def _request( ) response.raise_for_status() result = response if no_resolve_json else response.json() + print(response) if xform: return xform(result) except Exception as e: diff --git a/gotrue/_sync/gotrue_client.py b/gotrue/_sync/gotrue_client.py index 4c270bc4..46136a69 100644 --- a/gotrue/_sync/gotrue_client.py +++ b/gotrue/_sync/gotrue_client.py @@ -253,6 +253,32 @@ def sign_in_with_password( self._notify_all_subscribers("SIGNED_IN", response.session) return response + def sign_in_with_sso(self, credentials: SignInWithSSOCredentials): + self._remove_session() + provider_id = credentials.get("provider_id") + domain = credentials.get("domain") + options = credentials.get("options", {}) + redirect_to = options.get("redirect_to") + captcha_token = options.get("captcha_token") + if domain: + return self._request( + "POST", + "sso", + body={"domain": domain}, + redirect_to=redirect_to, + xform=parse_auth_response, + ) + if provider_id: + return self._request( + "POST" "sso", + body={"provider_id": provider_id}, + redirect_to=redirect_to, + xform=parse_auth_response, + ) + raise AuthInvalidCredentialsError( + "You must provide either a domain or provider_id" + ) + def sign_in_with_oauth( self, credentials: SignInWithOAuthCredentials, diff --git a/gotrue/types.py b/gotrue/types.py index b47c2084..2bd515c0 100644 --- a/gotrue/types.py +++ b/gotrue/types.py @@ -323,6 +323,17 @@ class SignInWithOAuthCredentials(TypedDict): options: NotRequired[SignInWithOAuthCredentialsOptions] +class SignInWithSSOCredentials(TypedDict): + provider_id: NotRequired[str] + domain: NotRequired[str] + options: NotRequired[SignInWithSSOOptions] + + +class SignInWithSSOOptions(TypedDict): + redirect_to: NotRequired[str] + captcha_token: NotRequired[str] + + class VerifyOtpParamsOptions(TypedDict): redirect_to: NotRequired[str] captcha_token: NotRequired[str]