UMA 2.0 session and client support for requests-oauthlib
Project description
UMA 2.0 Session Support for Requests-OAuthlib
Provides OAuth2 Sessions that support UMA 2.0 flows.
The UMA2Session class extends requests_oauthlib.OAuth2Session, and so supports any OAuth2 clients for initial authentication. Once authenticated, if a Resource Server requires UMA authorization, the session will automatically attempt to obtain the required Requesting Party Token (RPT) with the necessary permissions, then retry the request.
See User-Managed Access (UMA) 2.0 Grant for OAuth 2.0 Authorization for more information.
NOTE: This project is not affiliated with Requests-OAuthlib or any of its dependent libraries.
Install
pip install requests-oauthlib-uma
Example
from oauthlib.oauth2 import LegacyApplicationClient
from requests_oauthlib_uma import UMA2Session
# This example uses Legacy flow for initial authentication, but any OAuth2 Client can be used.
client_id = "your_client_id"
client_secret = "your_client_secret"
username = "your_username"
password = "your_password"
session = UMA2Session(client=LegacyApplicationClient(client_id=client_id))
token = session.fetch_token(
token_url="https://somesite.com/oauth2/token",
username=username,
password=password,
client_id=client_id,
client_secret=client_secret,
)
print(token)
# Attempt to access a UMA-protected resource:
response = session.get("https://somesite.com/secure/resource")
# The session now uses the newly issued RPT as the auth token going forward
print(session.token)
If the resource raises a 401 Unauthorized response with a WWW-Authenticate challenge header for a UMA scheme, the Session will automatically:
- Determine the Authorization Server from the challenge header
- Obtain the RPT endpoint URL from the Authorization Server's UMA 2.0 Well-Known Configuration endpoint
- Request a token with the requested permission claims from the Authorization Server's RPT endpoint
- Attempt the request again with the RPT
Subsequent requests will continue to use the issued RPT as the Session token.
If a subsequent request requires additional UMA authorization permissions not yet available in the RPT claims, the Session will repeat the above flow and attempt to upgrade the last-issued RPT with the added permission claims. This allows clients to incrementally obtain permission claims as needed.
Refreshing Tokens
The UMA2Session supports refreshing expired tokens via the same mechanism as OAuth2Session; see Refreshing tokens for more details. Automatic token refresh always uses the refresh_token from the last-issued RPT if provided.
Extra RPT Endpoint Parameters
If the Authorization Server is known to accept additional parameters (e.g., Keycloak), they can be configured when initializing the UMA2Session:
session = UMA2Session(
client=LegacyApplicationClient(client_id=client_id),
fetch_rpt_kwargs={"audience": "other-client"},
)
Handling Sequential Flows
By default, UMA2Session will only attempt a UMA flow once per request. If a Resource requires multiple UMA flows in order to grant authorization, you can increase the maximum number of attempts permitted per request: If the Resource is still requesting UMA authorization after reaching this limit, requests_oauthlib_uma.exceptions.MaxUMAFlowsReachedError is raised.
from requests_oauthlib_uma.exceptions import MaxUMAFlowsReachedError
session = UMA2Session(
client=LegacyApplicationClient(client_id=client_id),
max_flows_per_request=2,
)
try:
response = session.get("https://somesite.com/secure/resource")
except MaxUMAFlowsReachedError as err:
print(f"Giving up after {err.last_attempt.attempt_number} UMA authorization attempts.")
# Get the last response before the session gave up
last_response = err.last_attempt.result()
print(f"Last response was {last_response.status_code} - {last_response.text}")
Default Headers
You can also configure the Session to always provide a set of default headers that will be provided with all requests:
session = UMA2Session(
client=LegacyApplicationClient(client_id=client_id),
headers={"Content-Type": "application/json"},
)
Contributing
This package utilizes Poetry for dependency management and pre-commit for ensuring code formatting is automatically done and code style checks are performed.
git clone https://github.com/Daveography/requests-oauthlib-uma.git requests-oauthlib-uma
cd requests-oauthlib-uma
pip install poetry
poetry install
poetry run pre-commit install
poetry run pre-commit autoupdate
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
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 requests_oauthlib_uma-0.1.3.tar.gz.
File metadata
- Download URL: requests_oauthlib_uma-0.1.3.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91d2e62f3622aceef817d1085fbf260954fb712e493e8634ff69d1531c11bcb7
|
|
| MD5 |
8fa7eeaae9a2bbe029e14fb3efe381fa
|
|
| BLAKE2b-256 |
f8abd89c1c233290b94056e1df1059c982187ab166a77a928b1790d742f5f747
|
Provenance
The following attestation bundles were made for requests_oauthlib_uma-0.1.3.tar.gz:
Publisher:
publish.yaml on Daveography/requests-oauthlib-uma
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
requests_oauthlib_uma-0.1.3.tar.gz -
Subject digest:
91d2e62f3622aceef817d1085fbf260954fb712e493e8634ff69d1531c11bcb7 - Sigstore transparency entry: 229058689
- Sigstore integration time:
-
Permalink:
Daveography/requests-oauthlib-uma@e5dad946811d3e06bbfb85ad553e5649057adadd -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/Daveography
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@e5dad946811d3e06bbfb85ad553e5649057adadd -
Trigger Event:
release
-
Statement type:
File details
Details for the file requests_oauthlib_uma-0.1.3-py3-none-any.whl.
File metadata
- Download URL: requests_oauthlib_uma-0.1.3-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bbe281a181c014a1480b5d24dce0bf4d56d58d9e0afdcfecf704f3fd44947063
|
|
| MD5 |
a92fff57d452bc56c460a292ba1d1be9
|
|
| BLAKE2b-256 |
662e1cc97c75827bd6a213ffa1bd6c4fe6e636e4208b8b91c703ccc1cf58281c
|
Provenance
The following attestation bundles were made for requests_oauthlib_uma-0.1.3-py3-none-any.whl:
Publisher:
publish.yaml on Daveography/requests-oauthlib-uma
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
requests_oauthlib_uma-0.1.3-py3-none-any.whl -
Subject digest:
bbe281a181c014a1480b5d24dce0bf4d56d58d9e0afdcfecf704f3fd44947063 - Sigstore transparency entry: 229058693
- Sigstore integration time:
-
Permalink:
Daveography/requests-oauthlib-uma@e5dad946811d3e06bbfb85ad553e5649057adadd -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/Daveography
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@e5dad946811d3e06bbfb85ad553e5649057adadd -
Trigger Event:
release
-
Statement type: