Oauthlib: populate_token_attributes() ValueError for float expires_at values

Created on 10 Dec 2020  ·  7Comments  ·  Source: oauthlib/oauthlib

https://github.com/oauthlib/oauthlib/blob/d54965b86ce4ede956db70baff0b3d5e9182a007/oauthlib/oauth2/rfc6749/clients/base.py#L516

Some tokens have an expires_at value as a float (example: 1612807300.1613762) which causes this line to throw a ValueError: invalid literal for int() with base 10. This could be solved using int(float(response.get('expires_at'))) (or rounding to not truncate).

Bug Contributor Friendly OAuth2-Client

Most helpful comment

The usage of expires_at is not defined in RFC, so it seems confusion is in the code as well. We can assume it is an int similarly to expires_in.

Any PR to fix its usage and add unit test to cover this edge-case is welcome.

All 7 comments

Just above that self._expires_at is made a float:

self._expires_at = time.time() + int(self.expires_in)

The usage of expires_at is not defined in RFC, so it seems confusion is in the code as well. We can assume it is an int similarly to expires_in.

Any PR to fix its usage and add unit test to cover this edge-case is welcome.

hey, can i have this issue? I'm just starting with open source, so this will be a good start ig.

hey, can i have this issue? I'm just starting with open source, so this will be a good start ig.

@default-303 go for it!

@ggiill thanks, I did the following changes

- self._expires_at = int(response.get('expires_at')) 
+ self._expires_at = round(float(response.get('expires_at')))

and passes all the tests is when i run pytest

But what kinda tests you want me to add ? like a simple typecheck test something like this -
```
import unittest
import fix ## a sample script i made up for this demo
import time

class TestFix(unittest.TestCase) :

def test_float(self) : 
    string_time = str(time.time())
    self.assertIsInstance(fix.get_time("123345.1222"), int)

def test_int(self) : 
    string_time = str(round(time.time()))
    self.assertIsInstance(fix.get_time("12333"), int)

```

or a full on mock test ?

@ggiill hey, can i get a review ?
lmao its been a month even i forgot abt it.

@ggiill hey, can i get a review ?
lmao its been a month even i forgot abt it.

@default-303 I'm not a maintainer - pinging @JonathanHuot. You probably want to put in a PR for review as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ViktorHaag picture ViktorHaag  ·  11Comments

ib-lundgren picture ib-lundgren  ·  21Comments

jcampbell05 picture jcampbell05  ·  14Comments

JonathanHuot picture JonathanHuot  ·  33Comments

ryarnyah picture ryarnyah  ·  3Comments