Skip to main content

Menshen API client

menshen_client is an API client distributed as a Python's library to integrate Menshen OAuth2 Token Exchange server in your autorization flow. It has been designed to add few extra-dependencies to your project.

Installation

The API client is available from PyPI and can be added to your project using:

uv add menshen_client
# or
pip install menshen_client

Usage

Configure the client

You can create your client configuration using the following snippet:

from menshen_client import Configuration, TokenExchangeClient

# Configure the client
config = Configuration(
    client_id="acme",
    client_secret="super-secret",
    server_root_url="https://menshen.example.org",
    requests_timeout=1.5,  # This is optional (default: 3 seconds, None means no timeout)
)

# Create the client instance
client = TokenExchangeClient(config=config)

In this example, the client identifier and secret are those provided by your Menshen instance administrator that should have registered a service provider for your service. The server_root_url is the root URL of your Menshen server.

The client keeps a pool of HTTP connections to your Menshen server. It is recommended to create a single long-lived client instance for your application and to close it when you no longer need it:

client.close()

Alternatively, you can use the client as a context manager to automatically close its connections:

with TokenExchangeClient(config=config) as client:
    exchange_response = client.exchange(exchange_request)

Token exchange request

To generate an exchange token given a subject token and a related token type use:

from menshen_client import (
    TokenExchangeRequest,
    TokenExchangeResponse,
    TokenType,
)

# Create the token exchange request
exchange_request = TokenExchangeRequest(
    subject_token="exampletoken",
    subject_token_type=TokenType.ACCESS_TOKEN,
    audience="https://target.example.org",
    scope="target:item:write",
)

# Get the exchanged token response
exchange_response: TokenExchangeResponse = client.exchange(exchange_request)

For a full list of available fields in the token exchange request, please check the TokenExchangeRequest dataclass in the project's repository (and the RFC 8693).

The token exchange response contains the exchanged token that can be used to query the target resource server:

import requests

resources = requests.get(
    "https://target.example.org/external_api/v1.0/resource/",
    headers={
        "Authorization": f"Bearer {exchange_response.access_token}",
        "Content-Type": "application/json",
    },
)

For a full list of available fields in the token exchange response, please check the TokenExchangeResponse dataclass in the project's repository (and the RFC 8693).

Exchanged token instrospection

If you are the target resource server, you may want to introspect exchanged tokens via your autorization server instance using:

from menshen_client import (
    IntrospectionRequest,
    IntrospectionResponse,
    TokenType,
)

# Create the token instrospection request
introspection_request = IntrospectionRequest(
    token="exampletoken",
    token_type_hint=TokenType.ACCESS_TOKEN,
)

# Get the token introspection response
introspection_response: IntrospectionResponse = client.introspect(introspection_request)

# Check if the token is still valid to access the resource and get user information
assert introspection_response.active
assert introspection_response.sub == "1234567890"
assert introspection_response.email == "jane.doe@example.org"

For a full list of available fields in the token instrospection response, please check the IntrospectionResponse dataclass in the project's repository (and the RFC 7662).

Exchanged token revocation

If for any reason, you want to revoke an exchaged token, you can query your Menshen instance dedicated endpoint:

from menshen_client import (
    RevocationRequest,
    TokenType,
)

# Create the token revocation request
revocation_request = RevocationRequest(
    token="exampletoken",
    token_type_hint=TokenType.ACCESS_TOKEN,
)

# Token revocation response is empty
client.revoke(revocation_request)

Asynchronous client

If your project is asynchronous, you can use the TokenExchangeAsyncClient instead. It shares the same configuration and the same methods as the synchronous client, that need to be awaited:

from menshen_client import TokenExchangeAsyncClient

async with TokenExchangeAsyncClient(config=config) as client:
    exchange_response = await client.exchange(exchange_request)
    introspection_response = await client.introspect(introspection_request)
    await client.revoke(revocation_request)

When the client is not used as a context manager, close its HTTP connections using:

await client.aclose()

License

This work is released under the MIT License (see LICENSE).

While this project is a public-driven initiative, our license choice is an invitation for private sector actors to use, sell and contribute to it.

Metadata

Release files for menshen-client 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for menshen-client 0.3.0
File Size Uploaded Provenance Actions
menshen_client-0.3.0.tar.gz 7.6 kB With provenance Details for menshen_client-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for menshen-client 0.3.0
File Interpreter ABI Platform Actions
menshen_client-0.3.0-py3-none-any.whl Python 3 none any Details for menshen_client-0.3.0-py3-none-any.whl

Total release size: 15.5 kB

Release files / menshen_client-0.3.0.tar.gz

Download URL menshen_client-0.3.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b1a1d12a6277988e657e24ccc8a9ecf487d170bb9b0a0b10b1214eb8ee260308
BLAKE2b-256 checksum
How to use checksums
466c71057f698c261b20bb969387494072010d4fea3029a5480942546c6d5443
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

Release files / menshen_client-0.3.0-py3-none-any.whl

Download URL menshen_client-0.3.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6cddbf3024a661cac72ea47fbe0bcfc4355ef51d39b944708a736835803be1f2
BLAKE2b-256 checksum
How to use checksums
a5c5bef9dba4e39cd05aabe74d3488c393b4b2f963587df78c29b6ecdabe4a99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

Security for menshen-client 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for menshen_client-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page