Skip to content

Commit

Permalink
feat: allow dev to pass in code_verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Nov 1, 2023
1 parent 72ee18d commit f33bf3e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
10 changes: 6 additions & 4 deletions gotrue/_async/gotrue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,16 +869,18 @@ def _decode_jwt(self, jwt: str) -> DecodedJWTDict:
"""
return decode_jwt_payload(jwt)

def exchange_code_for_session(auth_code: str):
code_verifier = get_item(f"{self._storage_key}-code-verifier")
def exchange_code_for_session(self, params: CodeExchangeParams):
code_verifier = params.get("code_verifier") or get_item(

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

View check run for this annotation

Codecov / codecov/patch

gotrue/_async/gotrue_client.py#L873

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

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
"POST",
"token?grant_type=pkce",
body={
"auth_code": auth_code,
"auth_code": params.get("auth_code"),
"code_verifier": code_verifier,
},
redirect_to=redirect_to,
redirect_to=params.get("redirect_to"),
xform=parse_auth_response,
)
self._storage.remove_item(f"{self._storage_key}-code-verifier")
Expand Down
12 changes: 7 additions & 5 deletions gotrue/_sync/gotrue_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(
persist_session: bool = True,
storage: Union[SyncSupportedStorage, None] = None,
http_client: Union[SyncClient, None] = None,
flow_type: AuthFlowType = "implicit",
flow_type: AuthFlowType = "pkce",
) -> None:
SyncGoTrueBaseAPI.__init__(
self,
Expand Down Expand Up @@ -867,16 +867,18 @@ def _decode_jwt(self, jwt: str) -> DecodedJWTDict:
"""
return decode_jwt_payload(jwt)

def exchange_code_for_session(auth_code: str):
code_verifier = get_item(f"{self._storage_key}-code-verifier")
def exchange_code_for_session(self, params: CodeExchangeParams):
code_verifier = params.get("code_verifier") or get_item(

Check warning on line 871 in gotrue/_sync/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_sync/gotrue_client.py#L871

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

Check warning on line 874 in gotrue/_sync/gotrue_client.py

View check run for this annotation

Codecov / codecov/patch

gotrue/_sync/gotrue_client.py#L874

Added line #L874 was not covered by tests
"POST",
"token?grant_type=pkce",
body={
"auth_code": auth_code,
"auth_code": params.get("auth_code"),
"code_verifier": code_verifier,
},
redirect_to=redirect_to,
redirect_to=params.get("redirect_to"),
xform=parse_auth_response,
)
self._storage.remove_item(f"{self._storage_key}-code-verifier")
Expand Down
15 changes: 15 additions & 0 deletions gotrue/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,21 @@ class MFAUnenrollParams(TypedDict):
"""


class CodeExchangeParams(TypedDict):
code_verifier: str
"""
Randomly generated string
"""
auth_code: str
"""
Code returned after completing one of the authorization flows
"""
redirect_to: str
"""
The URL to route to after a session is successfully obtained
"""


class MFAVerifyParams(TypedDict):
factor_id: str
"""
Expand Down

0 comments on commit f33bf3e

Please sign in to comment.