Package: alf-auth0
alf-auth0
===Python OAuth 2 Clientalfis an OAuth 2 Client based onrequests.Sessionwith seamless support for theClient Credentials Flow.FeaturesAutomatic token retrieving and renewingToken expiration controlAutomatic token storageAutomatic retry on status 401 (UNAUTHORIZED)Works with Auth0 Client Credentials FlowUsageInitialize the client and use it as arequests.Sessionobject.fromalf.clientimportClientalf=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 storageNow 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.fromalf.clientimportClientfromredisimportStrictRedisredis=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 Auth0For the Client to work with Auth0 you need to initialize it withaudienceandtoken_default_expire_in.Auth0 is not returningexpires_inwhen you call authentication endpoint. As a result you should
settoken_default_expire_inas 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) fieldAudienceshould be set as your API Identifier in Auth0.fromalf.clientimportClientalf=Client(token_endpoint='http://example.com/token',audience='http://api.example.com/my-api/',token_default_expire_in=86400client_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 theaccess_tokenandexpires_inkeys will be
expected.Multiple attempts will be issued after an error response from the endpoint if
thetoken_retriesargument is used. Checktoken-retryingfor more info.alfkeeps the token until it is expired according to theexpires_invalue.The token will be used on aBearer authorization
headerfor
the original request.GET /resource/1 HTTP/1.1
Host: example.com
Authorization: Bearer token-12312If 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 RetryingThe client supports theretry interface from urllib3to 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 asbackoff).fromrequests.packages.urllib3.utilimportRetryfromalf.clientimportClientalf=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))WorkflowTroubleshootingIn case of an error retrieving a token, the error response will be returned,
the real request won’t happen.Related projectsdjalfAn extended client that uses Django’s cache backend to share tokens between
server instances.tornado-alfA port of thealfclient using tornado’sAsyncHTTPClient.