Skip to content

Commit d4b6699

Browse files
sindrigauvipy
authored andcommitted
Ensure expires_at is always int
As discussed in #745
1 parent 7d7fe90 commit d4b6699

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

oauthlib/oauth2/rfc6749/clients/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,11 @@ def populate_token_attributes(self, response):
589589

590590
if 'expires_in' in response:
591591
self.expires_in = response.get('expires_in')
592-
self._expires_at = time.time() + int(self.expires_in)
592+
self._expires_at = round(time.time()) + int(self.expires_in)
593593

594594
if 'expires_at' in response:
595595
try:
596-
self._expires_at = int(response.get('expires_at'))
596+
self._expires_at = round(float(response.get('expires_at')))
597597
except:
598598
self._expires_at = None
599599

oauthlib/oauth2/rfc6749/parameters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def parse_implicit_response(uri, state=None, scope=None):
345345
params['scope'] = scope_to_list(params['scope'])
346346

347347
if 'expires_in' in params:
348-
params['expires_at'] = time.time() + int(params['expires_in'])
348+
params['expires_at'] = round(time.time()) + int(params['expires_in'])
349349

350350
if state and params.get('state', None) != state:
351351
raise ValueError("Mismatching or missing state in params.")
@@ -437,6 +437,9 @@ def parse_token_response(body, scope=None):
437437
else:
438438
params['expires_at'] = time.time() + int(params['expires_in'])
439439

440+
if isinstance(params.get('expires_at'), float):
441+
params['expires_at'] = round(params['expires_at'])
442+
440443
params = OAuth2Token(params, old_scope=scope)
441444
validate_token_parameters(params)
442445
return params

tests/oauth2/rfc6749/clients/test_base.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import datetime
3+
from unittest.mock import patch
34

45
from oauthlib import common
56
from oauthlib.oauth2 import Client, InsecureTransportError, TokenExpiredError
@@ -353,3 +354,35 @@ def test_create_code_challenge_s256(self):
353354
code_verifier = client.create_code_verifier(length=128)
354355
code_challenge_s256 = client.create_code_challenge(code_verifier=code_verifier, code_challenge_method='S256')
355356
self.assertEqual(code_challenge_s256, client.code_challenge)
357+
358+
def test_parse_token_response_expires_at_is_int(self):
359+
expected_expires_at = 1661185149
360+
token_json = ('{ "access_token":"2YotnFZFEjr1zCsicMWpAA",'
361+
' "token_type":"example",'
362+
' "expires_at":1661185148.6437678,'
363+
' "scope":"/profile",'
364+
' "example_parameter":"example_value"}')
365+
366+
client = Client(self.client_id)
367+
368+
response = client.parse_request_body_response(token_json, scope=["/profile"])
369+
370+
self.assertEqual(response['expires_at'], expected_expires_at)
371+
self.assertEqual(client._expires_at, expected_expires_at)
372+
373+
@patch('time.time')
374+
def test_parse_token_response_generated_expires_at_is_int(self, t):
375+
t.return_value = 1661185148.6437678
376+
expected_expires_at = round(t.return_value) + 3600
377+
token_json = ('{ "access_token":"2YotnFZFEjr1zCsicMWpAA",'
378+
' "token_type":"example",'
379+
' "expires_in":3600,'
380+
' "scope":"/profile",'
381+
' "example_parameter":"example_value"}')
382+
383+
client = Client(self.client_id)
384+
385+
response = client.parse_request_body_response(token_json, scope=["/profile"])
386+
387+
self.assertEqual(response['expires_at'], expected_expires_at)
388+
self.assertEqual(client._expires_at, expected_expires_at)

tests/oauth2/rfc6749/clients/test_service_application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_request_body_no_initial_private_key(self, t):
166166
@patch('time.time')
167167
def test_parse_token_response(self, t):
168168
t.return_value = time()
169-
self.token['expires_at'] = self.token['expires_in'] + t.return_value
169+
self.token['expires_at'] = self.token['expires_in'] + round(t.return_value)
170170

171171
client = ServiceApplicationClient(self.client_id)
172172

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy