Skip to content

Commit

Permalink
Add force refresh and get refresh token options in get provider token (
Browse files Browse the repository at this point in the history
…#389)

* Add force refresh and get refresh token options in get provider token

* enhance test
  • Loading branch information
talaharoni authored Jul 5, 2024
1 parent ec89316 commit 662f46a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion descope/management/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ def get_provider_token(
self,
login_id: str,
provider: str,
withRefreshToken: Optional[bool] = False,
forceRefresh: Optional[bool] = False,
) -> dict:
"""
Get the provider token for the given login ID.
Expand All @@ -615,6 +617,8 @@ def get_provider_token(
Args:
login_id (str): The login ID of the user.
provider (str): The provider name (google, facebook, etc').
withRefreshToken (bool): Optional, set to true to get also the refresh token.
forceRefresh (bool): Optional, set to true to force refresh the token.
Return value (dict):
Return dict in the format
Expand All @@ -626,7 +630,7 @@ def get_provider_token(
"""
response = self._auth.do_get(
MgmtV1.user_get_provider_token,
{"loginId": login_id, "provider": provider},
{"loginId": login_id, "provider": provider, "withRefreshToken": withRefreshToken, "forceRefresh": forceRefresh},
pswd=self._auth.management_key,
)
return response.json()
Expand Down
7 changes: 4 additions & 3 deletions tests/management/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,13 +880,14 @@ def test_get_provider_token(self):
network_resp = mock.Mock()
network_resp.ok = True
network_resp.json.return_value = json.loads(
"""{"provider": "p1", "providerUserId": "puid", "accessToken": "access123", "expiration": "123123123", "scopes": ["s1", "s2"]}"""
"""{"provider": "p1", "providerUserId": "puid", "accessToken": "access123", "refreshToken": "refresh456", "expiration": "123123123", "scopes": ["s1", "s2"]}"""
)
mock_get.return_value = network_resp
resp = self.client.mgmt.user.get_provider_token("valid-id", "p1")
resp = self.client.mgmt.user.get_provider_token("valid-id", "p1", True, True)
self.assertEqual(resp["provider"], "p1")
self.assertEqual(resp["providerUserId"], "puid")
self.assertEqual(resp["accessToken"], "access123")
self.assertEqual(resp["refreshToken"], "refresh456")
self.assertEqual(resp["expiration"], "123123123")
self.assertEqual(resp["scopes"], ["s1", "s2"])
mock_get.assert_called_with(
Expand All @@ -895,7 +896,7 @@ def test_get_provider_token(self):
**common.default_headers,
"Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}",
},
params={"loginId": "valid-id", "provider": "p1"},
params={"loginId": "valid-id", "provider": "p1", "withRefreshToken": True, "forceRefresh": True},
allow_redirects=None,
verify=True,
timeout=DEFAULT_TIMEOUT_SECONDS,
Expand Down

0 comments on commit 662f46a

Please sign in to comment.