Skip to content

Commit

Permalink
Add test case for using auto_refresh_kwargs for authentication.
Browse files Browse the repository at this point in the history
Also fix formatting.
  • Loading branch information
fodinabor committed Apr 21, 2020
1 parent 7277d68 commit 397e87a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
17 changes: 11 additions & 6 deletions requests_oauthlib/oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,15 @@ def refresh_token(
)
kwargs.update(self.auto_refresh_kwargs)

auth = auth or kwargs.pop('auth', None)
client_id = kwargs.get('client_id')
client_secret = kwargs.get('client_secret', '')
auth = auth or kwargs.pop("auth", None)
client_id = kwargs.get("client_id")
client_secret = kwargs.get("client_secret", "")

if client_id and (auth is None):
log.debug('Encoding client_id "%s" with client_secret as Basic auth credentials.', client_id)
log.debug(
'Encoding client_id "%s" with client_secret as Basic auth credentials.',
client_id,
)
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)

body = self._client.prepare_refresh_body(
Expand Down Expand Up @@ -501,8 +504,10 @@ def request(
)

token = self.refresh_token(
self.auto_refresh_url, client_id=client_id,
client_secret=client_secret, **kwargs
self.auto_refresh_url,
client_id=client_id,
client_secret=client_secret,
**kwargs
)
if self.token_updater:
log.debug(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ def fake_refresh_with_auth(r, **kwargs):
client_secret=self.client_secret,
)

# auto refresh with auth from auto_refresh_kwargs
for client in self.clients:
sess = OAuth2Session(
client=client,
token=self.expired_token,
auto_refresh_url="https://i.b/refresh",
token_updater=token_updater,
auto_refresh_kwargs={
"client_id": self.client_id,
"client_secret": self.client_secret,
},
)
sess.send = fake_refresh_with_auth
sess.get("https://i.b")

@mock.patch("time.time", new=lambda: fake_time)
def test_token_from_fragment(self):
mobile = MobileApplicationClient(self.client_id)
Expand Down

0 comments on commit 397e87a

Please sign in to comment.