Skip to content

Commit

Permalink
feat: exchange code for session now fully async
Browse files Browse the repository at this point in the history
BREAKING CHANGE: change async method on_auth_state_change to sync only.
  • Loading branch information
silentworks committed Nov 29, 2023
1 parent abe3e2a commit b5fca51
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
21 changes: 12 additions & 9 deletions gotrue/_async/gotrue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
AuthMFAUnenrollResponse,
AuthMFAVerifyResponse,
AuthResponse,
CodeExchangeParams,
DecodedJWTDict,
MFAChallengeAndVerifyParams,
MFAChallengeParams,
Expand Down Expand Up @@ -269,7 +270,7 @@ async def sign_in_with_oauth(
params["redirect_to"] = redirect_to
if scopes:
params["scopes"] = scopes
url = self._get_url_for_provider(provider, params)
url = await self._get_url_for_provider(provider, params)

Check warning on line 273 in gotrue/_async/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L273

Added line #L273 was not covered by tests
return OAuthResponse(provider=provider, url=url)

async def sign_in_with_otp(
Expand Down Expand Up @@ -500,7 +501,7 @@ async def sign_out(self) -> None:
await self._remove_session()
self._notify_all_subscribers("SIGNED_OUT", None)

async def on_auth_state_change(
def on_auth_state_change(
self,
callback: Callable[[AuthChangeEvent, Union[Session, None]], None],
) -> Subscription:
Expand Down Expand Up @@ -844,15 +845,17 @@ def _is_implicit_grant_flow(self, url: str) -> bool:
params = parse_qs(result.query)
return "access_token" in params or "error_description" in params

def _get_url_for_provider(
async def _get_url_for_provider(
self,
provider: Provider,
params: Dict[str, str],
) -> str:
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)
await self._storage.set_item(

Check warning on line 856 in gotrue/_async/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L856

Added line #L856 was not covered by tests
f"{self._storage_key}-code-verifier", code_verifier
)
code_challenge_method = (
"plain" if code_verifier == code_challenge else "s256"
)
Expand All @@ -869,11 +872,11 @@ def _decode_jwt(self, jwt: str) -> DecodedJWTDict:
"""
return decode_jwt_payload(jwt)

def exchange_code_for_session(self, params: CodeExchangeParams):
code_verifier = params.get("code_verifier") or self._storage.get_item(
async def exchange_code_for_session(self, params: CodeExchangeParams):
code_verifier = params.get("code_verifier") or await self._storage.get_item(

Check warning on line 876 in gotrue/_async/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L876

Added line #L876 was not covered by tests
f"{self._storage_key}-code-verifier"
)
response = self._request(
response = await self._request(

Check warning on line 879 in gotrue/_async/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L879

Added line #L879 was not covered by tests
"POST",
"token?grant_type=pkce",
body={
Expand All @@ -883,8 +886,8 @@ def exchange_code_for_session(self, params: CodeExchangeParams):
redirect_to=params.get("redirect_to"),
xform=parse_auth_response,
)
self._storage.remove_item(f"{self._storage_key}-code-verifier")
await self._storage.remove_item(f"{self._storage_key}-code-verifier")

Check warning on line 889 in gotrue/_async/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L889

Added line #L889 was not covered by tests
if response.session:
self._save_session(response.session)
await self._save_session(response.session)

Check warning on line 891 in gotrue/_async/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L891

Added line #L891 was not covered by tests
self._notify_all_subscribers("SIGNED_IN", response.session)
return response
1 change: 1 addition & 0 deletions gotrue/_sync/gotrue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
AuthMFAUnenrollResponse,
AuthMFAVerifyResponse,
AuthResponse,
CodeExchangeParams,
DecodedJWTDict,
MFAChallengeAndVerifyParams,
MFAChallengeParams,
Expand Down

0 comments on commit b5fca51

Please sign in to comment.