Skip to main content

OAuth Client adapted to work with Auth0

Project description

alf-auth0 ===

https://travis-ci.org/globocom/alf.svg?branch=master

Python OAuth 2 Client

alf is an OAuth 2 Client based on requests.Session with seamless support for the Client Credentials Flow.

/assets/alf.jpeg?raw=true

Features

  • Automatic token retrieving and renewing

  • Token expiration control

  • Automatic token storage

  • Automatic retry on status 401 (UNAUTHORIZED)

  • Works with Auth0 Client Credentials Flow

Usage

Initialize the client and use it as a requests.Session object.

from alf.client import Client

alf = Client(
    token_endpoint='http://example.com/token',
    client_id='client-id',
    client_secret='secret')

resource_uri = 'http://example.com/resource'

alf.put(
    resource_uri, data='{"name": "alf"}',
    headers={'Content-Type': 'application/json'})

alf.get(resource_uri)

alf.delete(resource_uri)

Using your custom token storage

Now passing an object with get and set attributes you can store or retrieve a token.

This object can be a Redis, Memcache or your custom object.

from alf.client import Client
from redis import StrictRedis

redis = StrictRedis(host='localhost', port=6379, db=0)

alf = Client(
    token_endpoint='http://example.com/token',
    client_id='client-id',
    client_secret='secret',
    token_storage=redis)

resource_uri = 'http://example.com/resource'

alf.put(
    resource_uri, data='{"name": "alf"}',
    headers={'Content-Type': 'application/json'})

alf.get(resource_uri)

alf.delete(resource_uri)

Using alf with Auth0

For the Client to work with Auth0 you need to initialize it with audience and token_default_expire_in.

Auth0 is not returning expires_in when you call authentication endpoint. As a result you should set token_default_expire_in as the same value (or a bit smaller, to be safe) that you have set it in Auth0 management console > APIs > <your_api_name> > Settings > Token Expiration (Seconds) field

Audience should be set as your API Identifier in Auth0.

from alf.client import Client

alf = Client(
    token_endpoint='http://example.com/token',
    audience='http://api.example.com/my-api/',
    token_default_expire_in=86400
    client_id='client-id',
    client_secret='secret')

resource_uri = 'http://example.com/resource'

How does it work?

Before the request, a token will be requested on the authentication endpoint and a JSON response with the access_token and expires_in keys will be expected.

Multiple attempts will be issued after an error response from the endpoint if the token_retries argument is used. Check token-retrying for more info.

alf keeps the token until it is expired according to the expires_in value.

The token will be used on a Bearer authorization header for the original request.

GET /resource/1 HTTP/1.1
Host: example.com
Authorization: Bearer token-12312

If the request fails with a 401 (UNAUTHORIZED) status, a new token is retrieved from the endpoint and the request is retried. This happens only once, if it fails again the error response is returned.

The token will be reused for every following request until it is expired.

Token Retrying

The client supports the retry interface from urllib3 to repeat attempts to retrieve the token from the endpoint.

The following code will retry the token request 5 times when the response status is 500 and it will wait 0.3 seconds longer after each error (known as backoff).

from requests.packages.urllib3.util import Retry
from alf.client import Client

alf = Client(
    token_endpoint='http://example.com/token',
    client_id='client-id',
    client_secret='secret',
    token_retry=Retry(total=5, status_forcelist=[500], backoff_factor=0.3))

Workflow

/assets/workflow.png?raw=true

Troubleshooting

In case of an error retrieving a token, the error response will be returned, the real request won’t happen.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alf-auth0-0.8.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

alf_auth0-0.8.1-py2-none-any.whl (11.6 kB view details)

Uploaded Python 2

File details

Details for the file alf-auth0-0.8.1.tar.gz.

File metadata

  • Download URL: alf-auth0-0.8.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for alf-auth0-0.8.1.tar.gz
Algorithm Hash digest
SHA256 c6fdd807f3789d6002e9956f72058394484cd6dd9fd80e47c5c15d4ace339a04
MD5 c28ba716b52d9802a3516f9ee3f29efe
BLAKE2b-256 845257451728225f480bd1de17a4d1eec2130fe3a4a1c0e9f02cc1647c17b764

See more details on using hashes here.

File details

Details for the file alf_auth0-0.8.1-py2-none-any.whl.

File metadata

File hashes

Hashes for alf_auth0-0.8.1-py2-none-any.whl
Algorithm Hash digest
SHA256 0107c7d0e03a59560b96373af624d5965d98ddb5e87ebce0dc481e1e178da10b
MD5 e1da9c852df7a962022dc8e8ae688042
BLAKE2b-256 f7092322e080f4d6ecac713b95813ef417e91d6d3e9b6a534cb227f6da5b7cab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page