Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nikolaigulatz/oauth-token-cache
Easily obtain and cache OAuth 2.0 JWT tokens from Auth0.
https://github.com/nikolaigulatz/oauth-token-cache
auth0 oauth oauth2 python redis
Last synced: about 5 hours ago
JSON representation
Easily obtain and cache OAuth 2.0 JWT tokens from Auth0.
- Host: GitHub
- URL: https://github.com/nikolaigulatz/oauth-token-cache
- Owner: NikolaiGulatz
- License: mit
- Created: 2019-10-27T00:21:36.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T07:42:33.000Z (almost 2 years ago)
- Last Synced: 2024-11-15T18:39:40.117Z (about 8 hours ago)
- Topics: auth0, oauth, oauth2, python, redis
- Language: Python
- Size: 90.8 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# oauth-token-cache
[![Build Status](https://travis-ci.org/NikolaiGulatz/oauth-token-cache.svg?branch=master)](https://travis-ci.org/NikolaiGulatz/oauth-token-cache) [![codecov](https://codecov.io/gh/NikolaiGulatz/oauth-token-cache/branch/master/graph/badge.svg)](https://codecov.io/gh/NikolaiGulatz/oauth-token-cache) [![Maintainability](https://api.codeclimate.com/v1/badges/0c5868af680f364adafa/maintainability)](https://codeclimate.com/github/NikolaiGulatz/oauth-token-cache/maintainability) [![PyPI version](https://badge.fury.io/py/oauth-token-cache.svg)](https://badge.fury.io/py/oauth-token-cache)
Easily obtain and cache OAuth 2.0 JWT tokens from Auth0.
When using external auth providers for obtaining OAuth 2.0 machine-to-machine tokens you may want to share one access
token across several instances (e.g. processes, threads, containers, pods ...) of your application in order to avoid
having to issue new tokens too often.oauth-token-cache makes it easy to obtain, refresh and cache OAuth 2.0 tokens. Obtained tokens are stored both in
memory and in Redis with a TTL which corresponds to the time to expire of your token.```shell
pip install oauth-token-cache
```## Quickstart
```python
from oauth_token_cache import OAuthTokenCachetoken_provider = OAuthTokenCache(
client_id="XXX",
client_secret="XXX",
token_url="https://example.com/oauth/token"
)"""
The token will be cached in Redis. A fresh token will automatically be
fetched when calling `token` the next time in case the old token has expired.
"""
my_token = token_provider.token(audience="test")my_token.access_token
>> eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlFrVkJOemN6TjBNeFJr...my_token.audience
>> testmy_token.token_type
>> Bearermy_token.expires_at
>> 1572169916my_token.expired
>> False
```## Configuring Redis
The `redis_options` argument will be passed on to the redis client. See the [documentation of the redis package](https://pypi.org/project/redis/) on how to configure the client.
```python
OAuthTokenCache(
client_id="XXX",
client_secret="XXX",
token_url="https://example.com/oauth/token",
redis_options={
"host": "example.com",
"port": 1234,
}
)
```### Using your own Redis client
You can pass your own Redis client at which `redis_options` will be ignored. Make sure to configure the redis client to
automatically decode responses using `decode_response=True`.```python
redis_client = redis.Redis(decode_response=True)OAuthTokenCache(
client_id="XXX",
client_secret="XXX",
token_url="https://example.com/oauth/token",
redis_client=redis_client,
)
```## Overwriting the returned token
For CI and testing it is sometimes helpful to overwrite the returned access token and thus bypass the refreshing and caching functionality of OAuthTokenCache.
You can do so by setting the `OAUTH_TOKEN` environment variable. OAuthTokenCache will then always return the access token given in the environment variable.
## Development
1. Install the dependencies:
```shell
poetry install
```2. Run linters:
```shell
make black
make pylint
```3. Run pytest:
```shell
make pytest
```