Skip to main content

OAuth2 support for aiohttp client

Project description

aiohttp-oauth2-client: OAuth2 support for aiohttp client

This package adds support for OAuth 2.0 authorization to the ClientSession class of the aiohttp library. It handles retrieving access tokens and injects them in the Authorization header of HTTP requests as a Bearer token.

Features:

Installation

The pacakge is available on PyPi and can be installed using pip:

pip install aiohttp-oauth2-client

Usage

Begin by importing the relevant modules, like the OAuth2 client middleware and grant. Also import asyncio for running async code:

import asyncio
from aiohttp import ClientSession
from aiohttp_oauth2_client.middleware import OAuth2Middleware
from aiohttp_oauth2_client.grant.device_code import DeviceCodeGrant

Then create an OAuth2Grant and OAuth2Middleware object and perform a HTTP request to a protected resource. We use the Device Code grant in this example:

async def main():
    async with DeviceCodeGrant(
            token_url=TOKEN_URL,
            device_authorization_url=DEVICE_AUTHORIZATION_URL,
            client_id=CLIENT_ID,
            pkce=True
    ) as grant, ClientSession(middlewares=(OAuth2Middleware(grant),)) as client:
        async with client.get(PROTECTED_ENDPOINT) as response:
            assert response.ok
            print(await response.text())


asyncio.run(main())

The client and grant objects can be used as async context managers. This ensures the proper setup and cleanup of associated resources.

Grant configuration

This section provides an overview of the configuration options for each grant type. Extra parameters can be provided, which will then be used in the authorization process.

Authorization code grant

The authorization code grant uses a web browser login to request an authorization code, which is then used to request an access token.

Parameters
Parameter Required Description
token_url Yes OAuth 2.0 Token URL
authorization_url Yes OAuth 2.0 Authorization URL
client_id Yes client identifier
token No OAuth 2.0 Token
pkce No use PKCE
Example
from aiohttp import ClientSession
from aiohttp_oauth2_client.middleware import OAuth2Middleware
from aiohttp_oauth2_client.grant.authorization_code import AuthorizationCodeGrant

...

async with AuthorizationCodeGrant(
        token_url="https://sso.example.com/oauth2/token",
        authorization_url="https://sso.example.com/oauth2/auth",
        client_id="public",
        pkce=True
) as grant, ClientSession(middlewares=(OAuth2Middleware(grant),)) as client:
    ...

Client credentials grant

Use client credentials to obtain an access token.

Parameters
Parameter Required Description
token_url Yes OAuth 2.0 Token URL
client_id Yes client identifier
client_secret Yes client secret
token No OAuth 2.0 token
Example
from aiohttp import ClientSession
from aiohttp_oauth2_client.middleware import OAuth2Middleware
from aiohttp_oauth2_client.grant.client_credentials import ClientCredentialsGrant

...

async with ClientCredentialsGrant(
        token_url="https://sso.example.com/oauth2/token",
        client_id="my-client",
        client_secret="top-secret"
) as grant, ClientSession(middlewares=(OAuth2Middleware(grant),)) as client:
    ...

Device code grant

Obtain user authorization on devices with limited input capabilities or lack a suitable browser to handle an interactive log in procedure. The user is instructed to review the authorization request on a secondary device, which does have the requisite input and browser capabilities to complete the user interaction.

Parameters
Parameter Required Description
token_url Yes OAuth 2.0 Token URL
device_authorization_url Yes OAuth 2.0 Device Authorization URL
client_id Yes client identifier
token No OAuth 2.0 Token
pkce No use PKCE
Example
from aiohttp import ClientSession
from aiohttp_oauth2_client.middleware import OAuth2Middleware
from aiohttp_oauth2_client.grant.device_code import DeviceCodeGrant

...

async with DeviceCodeGrant(
        token_url="https://sso.example.com/oauth2/token",
        device_authorization_url="https://sso.example.com/oauth2/auth/device",
        client_id="public",
        pkce=True
) as grant, ClientSession(middlewares=(OAuth2Middleware(grant),)) as client:
    ...

Resource owner password credentials grant

Use the username and password of the resource owner to obtain an access token.

Parameters
Parameter Required Description
token_url Yes OAuth 2.0 Token URL
username Yes username of the resource owner
password Yes password of the resource owner
token No OAuth 2.0 Token
Example
from aiohttp import ClientSession
from aiohttp_oauth2_client.middleware import OAuth2Middleware
from aiohttp_oauth2_client.grant.resource_owner_password_credentials import ResourceOwnerPasswordCredentialsGrant

...

async with ResourceOwnerPasswordCredentialsGrant(
        token_url="https://sso.example.com/oauth2/token",
        username="username",
        password="password123",
        client_id="public"
) as grant, ClientSession(middlewares=(OAuth2Middleware(grant),)) as client:
    ...

Development

To start developing on this project, you should install all needed dependencies for running and testing the code:

pip install -e .[dev]

This will also install linting and formatting tools, which can be automatically executed when you commit using Git. To set up pre-commit as a Git hook, run:

pre-commit install

You can also run the pre-commit checks manually with the following command:

pre-commit run --all-files

Build the docs

This repository uses Sphinx to generate documentation for the Python package. To build the documentation, first install the required dependencies via the extra docs:

pip install -e .[docs]

Then go to the documentation directory and build the docs:

cd docs/
make html

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

aiohttp_oauth2_client-2.0.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttp_oauth2_client-2.0.1-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_oauth2_client-2.0.1.tar.gz.

File metadata

  • Download URL: aiohttp_oauth2_client-2.0.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for aiohttp_oauth2_client-2.0.1.tar.gz
Algorithm Hash digest
SHA256 cb570cf312ef833ee5e83c61c0dad059cbb8254724b8fb6ad18217a6e39d6942
MD5 fa092e7e9de948c9866383d9a62d8546
BLAKE2b-256 64d4bd691d9ccb083aa85944ab93113fe0c7cd71c9ac00ab25f798ba7790ddd8

See more details on using hashes here.

File details

Details for the file aiohttp_oauth2_client-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_oauth2_client-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8bb2911f9dd0a909f54badbe6203889e60126bc8921ab69fbc761a521f609072
MD5 bbca65cc3ce630795d5e56f725491ffa
BLAKE2b-256 7e81dcb8e77719ab363f958b5acfcb09acd03653cde95f55d30777121158d265

See more details on using hashes here.

Supported by

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