An OAuth 2.x client library for Python, with requests integration.
Project description
A Python OAuth 2.x client, able to obtain, refresh and revoke tokens from any OAuth2.x/OIDC compliant Authorization Server.
It can act as an OAuth 2.0/2.1 client, to automatically get and renew access tokens, based on the Client Credentials, Authorization Code, Refresh token, or the Device Authorization grants.
It comes with a requests add-on to handle OAuth 2.0 Bearer Token based authorization when accessing APIs.
It also supports OpenID Connect, PKCE, Client Assertions, Token Revocation, Exchange, and Introspection, as well as using custom params to any endpoint, and other important features that are often overlooked in other client libraries.
And it also includes a wrapper around requests.Session that makes it super easy to use REST-style APIs.
Installation
As easy as:
pip install requests_oauth2client
Usage
Import it like this:
from requests_oauth2client import *
You usually also have to use requests for your actual API calls:
import requests
That is unless you use the ApiClient wrapper from requests_oauth2client, as described below.
Calling APIs with an access token
If you already managed to obtain an access token, you can simply use the BearerAuth Auth Handler for requests:
token = "an_access_token" resp = requests.get("https://my.protected.api/endpoint", auth=BearerAuth(token))
This authentication handler will add an Authorization header in the request, with your access token according to RFC6750.
Using an OAuth2Client
OAuth2Client offers several methods that implement the communication to the various endpoints that are standardised by OAuth 2.0 and its extension. Those endpoints includes the Token Endpoint, and also the Revocation, Introspection and UserInfo Endpoint.
To initialize an OAuth2Client, you only need a Token Endpoint URI, and the credentials for your application:
oauth2client = OAuth2Client("https://myas.local/token_endpoint", ("client_id", "client_secret"))
The Token Endpoint is the only Endpoint that is mandatory to obtain tokens. Credentials are used to authenticate the client everytime it sends a request to its Authorization Server. Usually, those are a static Client ID and Secret, which are the direct equivalent of an username and a password, but for an application instead of for an human user. The default authentication method used by OAuth2Client is Client Secret Post, but other standardised methods such as Private Key JWT are supported as well. See below.
Obtaining tokens
OAuth2Client has methods to send requests to the Token Endpoint using the different standardised (and/or custom) grants. Since the token endpoint and authentication method are already declared for the client, the only required parameters are those that will be sent in the request to the Token Endpoint.
Those methods directly return a BearerToken if the request is successful, or raise an exception if it fails. BearerToken will manage the token expiration, will contain the eventual refresh token that matches the access token, and will keep track of other associated metadata as well. You can create such a BearerToken yourself if you need:
bearer_token = BearerToken(access_token="an_access_token", expires_in=60) print(bearer_token) > {'access_token': 'an_access_token', 'expires_in': 55, 'token_type': 'Bearer'} print(bearer_token.expires_at) > datetime.datetime(2021, 8, 20, 9, 56, 59, 498793)
Note that the “expires_in” indicator here is not static. It keeps track of the token lifetime and is calculated as the time flies. You can check if a token is expired with bearer_token.is_expired().
You can use a BearerToken instance everywhere you can supply an access_token as string.
Using OAuth2Client as a requests Auth Handler
While using OAuth2Client directly is great for testing or debugging OAuth2.0 flows, it is not a viable option for actual applications where tokens must be obtained, used during their lifetime then obtained again or refreshed. requests_oauth2client contains requests compatible Auth Handler (subclasses of requests.auth.AuthBase), that will take care of obtaining tokens when required, then will cache those tokens until they are expired, and will obtain new ones (or refresh them, when possible), once the initial token is expired. Those are best used with a requests.Session, or an ApiClient which is a Session Subclass with a few enhancements as described below.
Client Credentials grant
To send a request using the Client Credentials grant, use the aptly named .client_credentials() method:
token = oauth2client.client_credentials( scope="myscope", resource="https://myapi.local" # you may pass additional kw params such as audience, or whatever your AS needs )
As Auth Handler
You can use the OAuth2ClientCredentials auth handler. It takes an OAuth2Client as parameter, and the additional kwargs to pass to the token endpoint:
api_client = ApiClient( 'https://myapi.local/resource', auth=OAuth2ClientCredentials(oauth2client, scope='myscope', resource="https://myapi.local") ) resp = api_client.get() # when you send your first request to the API, it will fetch an access token first.
Supported Client Authentication Methods
requests_oauth2client supports multiple client authentication methods, as defined in multiple OAuth2.x standards. You select the appropriate method to use when initializing your OAuth2Client, with the auth parameter. Once initialised, a client will automatically use the configured authentication method every time it sends a requested to an endpoint that requires client authentication. You don’t have anything else to do afterwards.
client_secret_basic: client_id and client_secret are included in clear-text in the Authorization header. To use it, just pass a ClientSecretBasic(client_id, client_secret) as auth parameter:
client = OAuth2Client(token_endpoint, auth=ClientSecretBasic(client_id, client_secret))
client_secret_post: client_id and client_secret are included as part of the body form data. To use it, pass a ClientSecretPost(client_id, client_secret) as auth parameter. This also what is being used as default when you pass a tuple (client_id, client_secret) as auth:
client = OAuth2Client(token_endpoint, auth=ClientSecretPost(client_id, client_secret)) # or client = OAuth2Client(token_endpoint, auth=(client_id, client_secret))
client_secret_jwt: client generates an ephemeral JWT assertion including information about itself (client_id), the AS (url of the endpoint), and expiration date. To use it, pass a ClientSecretJWT(client_id, client_secret) as auth parameter. Assertion generation is entirely automatic, you don’t have anything to do:
client = OAuth2Client(token_endpoint, auth=ClientSecretJWT(client_id, client_secret))
private_key_jwt: client uses a JWT assertion like client_secret_jwt, but it is signed with an asymetric key. To use it, you need a private signing key, in a dict that matches the JWK format. The matching public key must be registered for your client on AS side. Once you have that, using this auth method is as simple with the PrivateKeyJWT auth handler:
private_jwk = { "kid": "mykid", "kty": "RSA", "e": "AQAB", "n": "...", "d": "...", "p": "...", "q": "...", "dp": "...", "dq": "...", "qi": "...", } client = OAuth2Client( "https://myas.local/token", auth=PrivateKeyJWT(client_id, private_jwk) )
none: client only presents its client_id in body form data to the AS, without any authentication credentials. Use PublicApp(client_id):
client = OAuth2Client(token_endpoint, auth=PublicApp(client_id, client_secret))
Token Exchange
To send a token exchange request, use the OAuth2Client.token_exchange() method:
client = OAuth2Client(token_endpoint, auth=...) token = client.token_exchange( subject_token='your_token_value', subject_token_type="urn:ietf:params:oauth:token-type:access_token" )
As with the other grant-type specific methods, you may specify additional keyword parameters, that will be passed to the token endpoint, including any standardised attribute like actor_token or actor_token_type, or any custom parameter. There are short names for token_types, that will be automatically translated to standardised types:
token = client.token_exchange( subject_token='your_token_value', subject_token_type="access_token", # will be automatically replaced by "urn:ietf:params:oauth:token-type:access_token" actor_token='your_actor_token', actor_token_type='id_token', # will be automatically replaced by "urn:ietf:params:oauth:token-type:id_token" )
Or to make it even easier, types can be guessed based on the supplied subject or actor token:
token = client.token_exchange( subject_token=BearerToken('your_token_value'), # subject_token_type will be "urn:ietf:params:oauth:token-type:access_token" actor_token=IdToken('your_actor_token'), # actor_token_type will be "urn:ietf:params:oauth:token-type:id_token" )
Token Revocation
OAuth2Client can send revocation requests to a Revocation Endpoint. You need to provide a Revocation Endpoint URI when creating the OAuth2Client:
oauth2client = OAuth2Client( token_endpoint, revocation_endpoint=revocation_endpoint, auth=ClientSecretJWT("client_id", "client_secret"))
The .revoke_token() method and its specialized aliases .revoke_access_token() and .revoke_refresh_token() are then available:
oauth2client.revoke_token("mytoken", token_type_hint="access_token") oauth2client.revoke_access_token("mytoken") # will automatically add token_type_hint=access_token oauth2client.revoke_refresh_token("mytoken") # will automatically add token_type_hint=refresh_token
Because Revocation Endpoints usually don’t return meaningful responses, those methods return a boolean. This boolean indicates that a request was successfully sent and no error was returned. If the Authorization Server actually returns a standardised error, an exception will be raised instead.
Token Introspection
OAuth2Client can send requests to a Token Introspection Endpoint. You need to provide an Introspection Endpoint URI when creating the OAuth2Client:
oauth2client = OAuth2Client( token_endpoint, introspection_endpoint=introspection_endpoint, auth=ClientSecretJWT("client_id", "client_secret"))
The .introspect_token() method is then available:
resp = oauth2client.introspect_token("mytoken", token_type_hint="access_token")
It returns whatever data is returned by the introspection endpoint (if it is a JSON, it’s content is returned decoded).
UserInfo Requests
OAuth2Client can send requests to an UserInfo Endpoint. You need to provide an UserInfo Endpoint URI when creating the OAuth2Client:
oauth2client = OAuth2Client( token_endpoint, userinfo_endpoint=userinfo_endpoint, auth=ClientSecretJWT("client_id", "client_secret"))
The .userinfo() method is then available:
resp = oauth2client.userinfo("mytoken")
It returns whatever data is returned by the userinfo endpoint (if it is a JSON, it’s content is returned decoded).
Initializing an OAuth2Client from a discovery document
You can initialize an OAuth2Client with the endpoint URIs mentionned in a standardised discovery document:
oauth2client = OAuth2Client.from_discovery_endpoint("https://myas.local/.well-known/openid-configuration")
This will fetch the document from the specified URI, then will decode it and initialize an OAuth2Client pointing to the appropriate endpoint URIs.
Specialized API Client
Using APIs usually involves multiple endpoints under the same root url, with a common authentication method. To make it easier, requests_oauth2client includes a specialized requests.Session subclass called ApiClient, which takes a root url as parameter on initialization. You can then send requests to different endpoints by passing their relative path instead of the full url. ApiClient also accepts an auth parameter with an AuthHandler. You can pass any of the OAuth2 Auth Handler from this module, or any requests-compatible AuthHandler. Which makes it very easy to call APIs that are protected with an OAuth2 Client Credentials Grant:
oauth2client = OAuth2Client("https://myas.local/token", (client_id, client_secret)) api = ApiClient("https://myapi.local/root", auth=OAuth2ClientCredentialsAuth(oauth2client)) resp = api.get("/resource/foo") # will actually send a GET to https://myapi.local/root/resource/foo
Note that ApiClient will never send requests “outside” its configured root url, unless you specifically give it full url at request time. The leading / in /resource above is optional. A leading / will not “reset” the url path to root, which means that you can also write the relative path without the / and it will automatically be included:
api.get("resource/foo") # will actually send a GET to https://myapi.local/root/resource/foo
You may also pass the path as an iterable of strings (or string-able objects), in which case they will be joined with a / and appended to the url path:
api.get(["resource", "foo"]) # will actually send a GET to https://myapi.local/root/resource/foo api.get(["users", 1234, "details"]) # will actually send a GET to https://myapi.local/root/users/1234/details
ApiClient will, by default, raise exceptions whenever a request returns an error status. You can disable that by passing raise_for_status=False when initializing your ApiClient:
api = ApiClient( "http://httpstat.us", raise_for_status=False # this defaults to True ) resp = api.get("500") # without raise_for_status=False, this would raise a requests.exceptions.HTTPError
You may override this at request time:
resp = api.get("500", raise_for_status=True) # raise_for_status at request-time overrides raise_for_status defined at init-time
Vendor-Specific clients
requests_oauth2client being flexible enough to handle most use cases, you should be able to use any AS by any vendor as long as it supports OAuth 2.0.
You can however subclass OAuth2Client or ApiClient to make it easier to use with specific Authorization Servers or APIs. requests_oauth2client.vendor_specific includes such classes for Auth0:
from requests_oauth2client.vendor_specific import Auth0Client a0client = Auth0Client("mytenant.eu", (client_id, client_secret)) # this will automatically initialize the token endpoint to https://mytenant.eu.auth0.com/oauth/token # so you can use it directly token = a0client.client_credentials(audience="audience") # this is a wrapper around Auth0 Management API a0mgmt = Auth0ManagementApiClient("mytenant.eu", (client_id, client_secret)) myusers = a0mgmt.get("users")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for requests_oauth2client-0.18.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ffedee6b3b325b13ec00c0a640e2d062663bd49faeabc9096f734d4455be8df |
|
MD5 | 2fc6b43b78ec15f8b1c04e5d7eef91f0 |
|
BLAKE2b-256 | 0de96c035dbd14f605ec7354bce84f7ce0ca61ee04d606ac45d8999586114126 |