Skip to main content

Add your description here

Project description

HTTPX-OAuth2

My implementation of an httpx.BaseTransport that negotiates an access token and puts it in the request headers before sending it.

Installation

You can't install it yet :(

Usage

The library only needs to be setup. Once it is done, the authentication will happen behind the usage of httpx.Client, meaning you shouldn't need to change existing code.

Imports

import httpx
from httpx_oauth2 import (
	OAuthAuthorityClient,
	TokenProviderFactory,
	ClientAuthenticationTransport,
	ClientCredentials,
	ResourceOwnerCredentials
)

Client Credentials

api_client = httpx.Client(base_url='http://example')

# ============== ADD THIS ==============

token_providers = TokenProviderFactory(
	OAuthAuthorityClient(httpx.Client(base_url='http://localhost:8080/realms/master'))
)

credentials = ClientCredentials(CLIENT_ID, CLIENT_SECRET, ('scope-1', 'scope-2'))

api_client._transport = ClientAuthenticationTransport(
	api_client._transport,
	token_providers.client_credentials(credentials)
)

# ===== JUST THIS. NOW USE A USUAL =====

api_client.get('/users')

Resource Owner

api_client = httpx.Client(base_url='http://example')

# ============== ADD THIS ==============

token_providers = TokenProviderFactory(
	OAuthAuthorityClient(httpx.Client(base_url='http://localhost:8080/realms/master'))
)

credentials = ResourceOwnerCredentials(USERNAME, PASSWORD, CLIENT_ID, ('scope-1', 'scope-2')) # <<<

api_client._transport = ClientAuthenticationTransport(
	api_client._transport,
	token_providers.resource_owner(credentials) # <<<
)

# ===== JUST THIS. NOW USE A USUAL =====

api_client.get('/users')

Token Exchange

api_client = httpx.Client(base_url='http://example')

# ============== ADD THIS ==============

token_providers = TokenProviderFactory(
	OAuthAuthorityClient(httpx.Client(base_url='http://localhost:8080/realms/master'))
)

credentials = ClientCredentials(CLIENT_ID, CLIENT_SECRET, ('scope-1', 'scope-2'))

api_client._transport = TokenExchangeAuthenticationTransport( # <<<
	api_client._transport,
	token_providers.token_exchange(credentials) # <<<
)

# ===== JUST THIS. NOW USE A USUAL =====

# * Put the token to be exchanged in the authorization headers

api_client.get('/users', headers={'Authorization': 'token_to_be_exchanged'})

Cache and Automatic retry

Access token are cached. Exchanged tokens too.

If the AuthenticationTransport see that the response is 401 (meaning the token wasn't valid anymore), it will:

  • Try to refresh the token with the refresh_token if supported.
  • Request a new token.
  • Re-send the request.

But '_' means its protected?

Yes. But I haven't found an easier way to let httpx build the base transport but still be able to wrap it with custom behavior.

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

httpx_oauth2-0.1.0.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

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

httpx_oauth2-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file httpx_oauth2-0.1.0.tar.gz.

File metadata

  • Download URL: httpx_oauth2-0.1.0.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for httpx_oauth2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8f476cfcd8d8b5172e2fa8ade063f486b8c02cdf8af93cb6ca701a0835fa3d05
MD5 d37209c0c21a542b2d8e36d6c32846b5
BLAKE2b-256 1ff4acb663966bd8925aacdf09a8672077950126f15f398348bb3379c10565bf

See more details on using hashes here.

File details

Details for the file httpx_oauth2-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: httpx_oauth2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for httpx_oauth2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 577982d321dbeeb1ba03ace441a5708423c3a92dd016724b826e84edf51b0a10
MD5 3672bf2f8be00836045e9321b066f827
BLAKE2b-256 8c7e0b908020cfe65ffff9811af6c460f49c8a744e8fc703cff69c58120be4ce

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