Client library for the OIDC Auth server.
Project description
Typed client library for the authentication routes exposed by py-oidc-auth.
py-oidc-auth-client is the counterpart of the server-side library py-oidc-auth.
While py-oidc-auth helps you add OpenID Connect login, token, and device endpoints to web frameworks, py-oidc-auth-client consumes those routes and gives you ready-to-use bearer tokens for calling protected APIs.
Features
- One high level helper:
authenticate() - Device flow for headless sessions
- Authorization code flow for interactive logins
- Token caching and refresh token support via a token file
- Fully typed public API
Install
python -m pip install py-oidc-auth-client
Import name is py_oidc_auth_client:
from py_oidc_auth_client import authenticate
Relationship to py-oidc-auth
A typical py-oidc-auth server exposes endpoints similar to:
GET /auth/v2/loginGET /auth/v2/callbackPOST /auth/v2/tokenPOST /auth/v2/deviceGET /auth/v2/logoutGET /auth/v2/userinfo
This client calls the relevant routes (token and device, and possibly login/callback) and returns a Token
object that contains a ready-made Authorization header.
Quick start
from py_oidc_auth_client import authenticate
token = authenticate(host="https://auth.example.org")
# Use with any HTTP client
headers = token["headers"]
print(headers["Authorization"])
Use with httpx
import httpx
from py_oidc_auth_client import authenticate
token = authenticate(host="https://auth.example.org")
with httpx.Client() as client:
r = client.get("https://service.example.org/protected", headers=token["headers"])
r.raise_for_status()
print(r.json())
Token persistence
By default, the client stores tokens in a cache file so you do not have to re-authenticate on every run.
You can control where tokens are stored with token_file:
from py_oidc_auth_client import authenticate, TokenStore
token = authenticate(
host="https://auth.example.org",
store=TokenStore(path="~/.cache/py-oidc-auth-client/token.json"),
)
You can also point to a token file via environment variable:
OIDC_TOKEN_FILE
Interactive and non-interactive environments
The client tries to select a suitable strategy:
- Use a valid cached access token.
- Refresh using the refresh token.
- If interactive authentication is possible, fall back to an interactive login.
- If running in a non-interactive session without a usable token, raise an error telling you how to provide a token file.
For headless sessions, the device flow is the recommended approach.
Advanced usage
If you need more control than authenticate(), use the flow helpers from py_oidc_auth_client.auth.
Device flow
import asyncio
from py_oidc_auth_client import Config, DeviceFlow
async def main() -> None:
cfg = Config(host="https://auth.example.org")
flow = DeviceFlow(config=cfg, token=None, timeout=600)
device = await flow.get_device_code()
print("Open:", device.uri)
print("Code:", device.user_code)
await flow.poll(device["device_code"], int(device["interval"]))
print(flow.token["headers"])
asyncio.run(main())
Authorization code flow
import asyncio
from py_oidc_auth_client import Config, CodeFlow
async def main() -> None:
cfg = Config(host="https://auth.example.org")
flow = CodeFlow(config=cfg, token=None, timeout=120)
await flow.login()
print(flow.token["headers"])
asyncio.run(main())
Contributing
Contributions are welcome. Please open an issue to discuss larger changes before submitting a pull request.
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
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 py_oidc_auth_client-2602.0.2.tar.gz.
File metadata
- Download URL: py_oidc_auth_client-2602.0.2.tar.gz
- Upload date:
- Size: 110.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b46384587197595ae1b83b0b0760e694bcaf16197c95bd584eb0c36e0cb913f
|
|
| MD5 |
e393eb4e6745016004e597fdaa3166f4
|
|
| BLAKE2b-256 |
df51ab8a6f2d2b9fbfcc825c12c191af5a9f8141dc7a305f7e6080f28988a9c7
|
File details
Details for the file py_oidc_auth_client-2602.0.2-py3-none-any.whl.
File metadata
- Download URL: py_oidc_auth_client-2602.0.2-py3-none-any.whl
- Upload date:
- Size: 25.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dea784bbcf198295df457ca1d39bd149a7a24d0d0cfcd4ed2dc15080a67ff35a
|
|
| MD5 |
95efeef2ab0f0893bf8a8a159c50b6f2
|
|
| BLAKE2b-256 |
eb10035064ca64f093db79b3da184d79926c278c9d98426b7962615f0eb45d68
|