OAuth2-lib
This project contains a number of classes to perform authentication (AuthN) and authorization (AuthZ) in a FastAPI application.
They can be found in oauth2_lib/fastapi.py. Most notable are:
OIDCAuth: AuthN implementation that authenticates a user against a OIDC backend. You can subclass and implementdef userinfo()as needed.- To use a different AuthN method, subclass the
Authenticationbase class.
- To use a different AuthN method, subclass the
OIDCUserModel: model of the data returned byOIDCAuth. You can subclass this to rename and/or add fields.OPAAuthorization: AuthZ implementation that authorizes a user's HTTP request against an Open Policy Agent (OPA) instance.- To use a different AuthZ method, subclass the
Authorizationbase class.
- To use a different AuthZ method, subclass the
GraphQLOPAAuthorization: AuthZ implementation that authorizes a user's GraphQL query against an Open Policy Agent (OPA) instance.- To use a different AuthZ method, subclass the
GraphqlAuthorizationbase class.
- To use a different AuthZ method, subclass the
OPAResult: model of the data returned byOPAAuthorizationandGraphQLOPAAuthorization.
The orchestrator-core documentation has a section on Authentication and Authorization that describes how to use/override these classes.
Upgrading to 3.0.0
3.0.0 replaces httpx with httpx2. The client passed to
OIDCAuth.userinfo(), OIDCAuth.check_openid_config() and OPAMixin.get_decision() is now an
httpx2.AsyncClient.
The annotation only affects type checking, but any value you pass back into that client must come
from httpx2 too — an httpx.BasicAuth, Timeout or Limits raises TypeError at runtime:
# Before
from httpx import AsyncClient, BasicAuth
class MyOIDCAuth(OIDCAuth):
async def userinfo(self, async_request: AsyncClient, token: str) -> OIDCUserModel:
response = await async_request.post(url, auth=BasicAuth(id, secret))
return OIDCUserModel(response.json())
# After
from httpx2 import AsyncClient
class MyOIDCAuth(OIDCAuth):
async def userinfo(self, async_request: AsyncClient, token: str) -> OIDCUserModel:
response = await async_request.post(url, auth=(id, secret))
return OIDCUserModel(response.json())
A plain (id, secret) tuple is accepted by both stacks, so that part can land before you upgrade.
The httpx[http2] extra is gone as well; the library only ever made HTTP/1.1 requests, so h2 is no
longer installed transitively. If you relied on oauth2-lib to pull httpx into your environment,
declare it yourself.
Installation
To install the package from PyPI:
pip install oauth2-lib
Development
Virtual Environment
Steps to setup a virtual environment.
Install uv and create the development environment:
uv sync --locked --group test --group dev
Run project commands through that environment with uv run, for example:
uv run pytest
Unit tests
Run the unit tests through the uv-managed environment:
uv run pytest
Pre-commit
This project uses pre-commit to automatically run a number of checks before making a git commit. The same checks will be performed in the CI pipeline so this can save you some time.
First ensure you have pre-commit installed. It is recommended to install it outside the virtualenv. On Linux and Mac, pre-commit is available in most package managers. Alternatively you can install it globally with pipx.
Once pre-commit is installed, go into the project root and enable it:
pre-commit install
This should output pre-commit installed at .git/hooks/pre-commit. The next time you run git commit the pre-commit hooks will validate your changes.
Set the package version
When a release version has been assigned, update the package metadata on a clean branch with uv version:
uv version 2.7.1
Specify the full version explicitly so release candidates can be represented, for example uv version 2.7.1rc1.
Supported Python versions
oauth2-lib must support the same python versions as orchestrator-core.
Exceptions to this rule are:
- A new python version is released: oauth2-lib should support the new version before orchestrator-core does
- Support for an old python version is dropped: oauth2-lib should drop the python version after orchestrator-core does
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file oauth2_lib-3.0.0.tar.gz.
File metadata
- Download URL: oauth2_lib-3.0.0.tar.gz
- Upload date:
- Size: 27.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b1dc793e6327b499265d5a5f0637f472bbe3ee10b687f189769844993cf3c62c
|
|
| MD5 |
e9c8b63ddb1916ad84ffd8aefb8d349f
|
|
| BLAKE2b-256 |
ba7c17b86d3b232f23f2571356644cc7d486eeadc508578c67692b7379bfae10
|
Provenance
The following attestation bundles were made for oauth2_lib-3.0.0.tar.gz:
Publisher:
publish-release.yml on workfloworchestrator/oauth2-lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
oauth2_lib-3.0.0.tar.gz -
Subject digest:
b1dc793e6327b499265d5a5f0637f472bbe3ee10b687f189769844993cf3c62c - Sigstore transparency entry: 2448851740
- Sigstore integration time:
-
Permalink:
workfloworchestrator/oauth2-lib@a4966877a3783a151fa32d71109901b022d1779b -
Branch / Tag:
refs/tags/3.0.0 - Owner: https://github.com/workfloworchestrator
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-release.yml@a4966877a3783a151fa32d71109901b022d1779b -
Trigger Event:
release
-
Statement type:
File details
Details for the file oauth2_lib-3.0.0-py3-none-any.whl.
File metadata
- Download URL: oauth2_lib-3.0.0-py3-none-any.whl
- Upload date:
- Size: 19.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
031c33884367d4702450fde081b9c23625460a7d2f8adbf06e7db49098cde50b
|
|
| MD5 |
1cd79176df5c2bac7f1228940e6efa5e
|
|
| BLAKE2b-256 |
35eccf32e8c0163629f3bfdd54114f396e765289db12c31f46560a795b546cbd
|
Provenance
The following attestation bundles were made for oauth2_lib-3.0.0-py3-none-any.whl:
Publisher:
publish-release.yml on workfloworchestrator/oauth2-lib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
oauth2_lib-3.0.0-py3-none-any.whl -
Subject digest:
031c33884367d4702450fde081b9c23625460a7d2f8adbf06e7db49098cde50b - Sigstore transparency entry: 2448851771
- Sigstore integration time:
-
Permalink:
workfloworchestrator/oauth2-lib@a4966877a3783a151fa32d71109901b022d1779b -
Branch / Tag:
refs/tags/3.0.0 - Owner: https://github.com/workfloworchestrator
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-release.yml@a4966877a3783a151fa32d71109901b022d1779b -
Trigger Event:
release
-
Statement type: