question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AccessTokenResponse Cache is cleared with every new access token request

See original GitHub issue

Describe the bug At every new access toke request the cache is reinitialized. So the cache will never contain more than one cached authorization token.

auth.access_token_client.py class AccessTokenClient

def get_auth(self) -> AccessTokenResponse:
        """
        Get's the access token
        :return:AccessTokenResponse
        """
        global cache

        cache_key = self._get_cache_key()
        try:
            access_token = cache[cache_key]
        except KeyError:
            cache_ttl = 3600
            access_token = None
            if not access_token:
                request_url = self.scheme + self.host + self.path
                access_token = self._request(request_url, self.data, self.headers)
            else:
                cache_ttl = access_token.get('expires_in')
            cache = TTLCache(maxsize=10, ttl=cache_ttl - 15)  # <---- THE CACHE IS CLEARED HERE
            cache[cache_key] = access_token
        return AccessTokenResponse(**access_token)

Expected behavior The cache should be initialized only once and be updated when a new access token is requested.

Additional context In addition to this issue, it would also be very useful to be able to set the TTLCache maxsize to a value greater than 10 since we usually cycle through more than 10 authorizations. This applies to :

auth.access_token_client.py

cache = TTLCache(maxsize=10, ttl=3600)
grantless_cache = TTLCache(maxsize=10, ttl=3600)

base.client.py role_cache = TTLCache(maxsize=1000, ttl=3600)

A good solution in my opinion would be to have the maxsize as an environmental variable or 10 by default.

Thanks

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
saleweavercommented, Jun 2, 2022

Hey, yes - I’ll rework the cache for both when I have more time, I want to add a way that works between restarts as well

0reactions
zonwizardcommented, Jun 6, 2022

Hey Michael, if you are going to improve the cache system, it might be a good idea also to reduce the TTL. Since 3600 seconds is the exact expiration time for the tokens it might happen that after finding a token in the cache it expires before he actual API request is made. {‘message’: ‘The security token included in the request is expired’, ‘code’: ‘Unauthorized’}

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access Token Response - OAuth 2.0 Simplified
A simple implementation of Bearer Tokens is to generate a random string and store it in a database along with the associated user...
Read more >
B2C Token Caching for multiple resources not working #904
Hi, I'm trying to use the Android MSAL with a B2C authority. Basically I have to fetch multiple Access Token for multiple resources....
Read more >
google api - Unable to refresh access token - Stack Overflow
Java code which sends the request: RefreshTokenRequest req = new RefreshTokenRequest(new NetHttpTransport(), new JacksonFactory(), new ...
Read more >
OAuth authentication using POST returns "400 bad request"
I am trying to make it work with DotNetOpenAuth and so far no luck. It appears that your implementation expects all parameters in...
Read more >
Acquire and cache tokens with Microsoft Authentication ...
Your application code should first try to get a token silently ... You can also clear the token cache, which is achieved by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found