def test_refresh_user_token_refresh_replaced_if_returned(self): c = Credentials('id', 'secret', 'uri') token = make_token_dict() response = mock_response(content=token) send = MagicMock(return_value=response) with patch('tekore.auth.Credentials._send', send): refreshed = c.refresh_user_token('refresh') self.assertEqual(refreshed.refresh_token, token['refresh_token'])
class RefreshingCredentials: """ Client for retrieving automatically refreshing access tokens. Delegates to an underlying :class:`Credentials <tekore.auth.expiring.Credentials>` manager and parses tokens it returns into :class:`RefreshingToken`. Parameters ---------- client_id client id client_secret client secret redirect_uri whitelisted redirect URI sender request sender """ def __init__(self, client_id: str, client_secret: str, redirect_uri: str = None, sender: Sender = None): self._client = Credentials(client_id, client_secret, redirect_uri, sender) def request_client_token(self) -> RefreshingToken: """ Request a refreshing client token. Returns ------- RefreshingToken automatically refreshing client token """ token = self._client.request_client_token() return RefreshingToken(token, self._client) def user_authorisation_url(self, scope=None, state: str = None, show_dialog: bool = False) -> str: """ Construct an authorisation URL. Step 1/2 in authorisation code flow. User should be redirected to the resulting URL for authorisation. Parameters ---------- scope access rights as a space-separated list state additional state show_dialog force login dialog even if previously authorised Returns ------- str login URL """ return self._client.user_authorisation_url(scope, state, show_dialog) def request_user_token(self, code: str) -> RefreshingToken: """ Request a new refreshing user token. Step 2/2 in authorisation code flow. Code is provided as a URL parameter in the redirect URI after login in step 1. Parameters ---------- code code from redirect parameters Returns ------- RefreshingToken automatically refreshing user token """ token = self._client.request_user_token(code) return RefreshingToken(token, self._client) def refresh_user_token(self, refresh_token: str) -> RefreshingToken: """ Request an automatically refreshing user token with a refresh token. Parameters ---------- refresh_token refresh token Returns ------- RefreshingToken automatically refreshing user token """ token = self._client.refresh_user_token(refresh_token) return RefreshingToken(token, self._client)
def test_refresh_user_token(self): c = Credentials(self.client_id, self.client_secret) c.refresh_user_token(self.user_refresh)
def test_async_refresh_user_token(self): c = Credentials(self.client_id, self.client_secret, asynchronous=True) run(c.refresh_user_token(self.user_refresh))