Skip to content

feat: add client credentials token support for M2M - #138

Merged
kishore7snehil merged 5 commits into
mainfrom
feat/m2m-client-credentials
Oct 22, 2026
Merged

kishore7snehil merged 5 commits into
mainfrom
feat/m2m-client-credentials

Conversation

@kishore7snehil

@kishore7snehil kishore7snehil commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

📋 Changes

This PR adds get_client_credentials_token() to ApiClient for obtaining machine-to-machine access tokens with the OAuth 2.0 client credentials grant. When a token store is configured, the result is cached so repeat calls within the token's lifetime skip the network round-trip.

✨ Features

  • Client Credentials Token: New get_client_credentials_token(audience, scope=None) method that authenticates with HTTP Basic using the configured client_id and client_secret.
  • M2M Caching: When token_store is set, tokens are cached per tenant, client, audience, and scope set. Store read and write failures are logged and fall back to a fresh exchange.
  • Type Safety: New ClientCredentialsTokenResult TypedDict.

🔧 API Changes

  • New method: ApiClient.get_client_credentials_token()
  • New type: ClientCredentialsTokenResult (TypedDict)
  • New error: GetClientCredentialsTokenError, raised when client credentials are not configured or the token endpoint is missing from discovery metadata

📖 Documentation

  • Updated README.md with a client credentials section
  • Updated EXAMPLES.md with a client credentials example

🧪 Testing

  • This change adds test coverage
  • This change has been tested on the latest version of the platform/language

Contributor Checklist

Comment thread src/auth0_api_python/api_client.py Fixed
@kishore7snehil
kishore7snehil marked this pull request as ready for review October 6, 2026 06:50
@kishore7snehil
kishore7snehil requested a review from a team as a code owner October 6, 2026 06:50
Comment thread src/auth0_api_python/api_client.py Resolved
Comment thread src/auth0_api_python/api_client.py Resolved
Comment thread tests/test_api_client.py Resolved
Comment thread src/auth0_api_python/api_client.py Resolved
Comment thread src/auth0_api_python/api_client.py Resolved
Comment thread src/auth0_api_python/types.py Resolved
@kishore7snehil
kishore7snehil force-pushed the feat/m2m-client-credentials branch from fcb1832 to 6576276 Compare October 8, 2026 04:07
@kishore7snehil
kishore7snehil force-pushed the feat/obo-token-storage branch 2 times, most recently from d57457d to 75faa2d Compare October 8, 2026 04:22
@kishore7snehil
kishore7snehil force-pushed the feat/m2m-client-credentials branch 2 times, most recently from a9668aa to 0d427d9 Compare October 12, 2026 09:59
nandan-bhat
nandan-bhat previously approved these changes Oct 22, 2026
@kishore7snehil
kishore7snehil force-pushed the feat/m2m-client-credentials branch from 398a6bf to 9349c8f Compare October 22, 2026 06:41
Add an ApiClient method to obtain a client credentials (M2M) access token
for server-to-server calls using the OAuth 2.0 client credentials grant.
The method authenticates via HTTP Basic and caches the result in a
configured token_store keyed by audience and scope set, so a repeat call
within the token's lifetime skips the network round-trip. Add the
ClientCredentialsTokenResult type and GetClientCredentialsTokenError, and
document the method in the README and examples.
Cache the scope Auth0 returned alongside the token and include it on a cache hit, so a hit matches the original exchange.
…aching

Log the original exception through the module logger instead.
- Raise ApiError for a malformed token response instead of leaking
  ValueError or KeyError.
- Cache token_type and return scope whenever it was saved, so a cache hit
  matches a fresh exchange.
- Annotate the result as ClientCredentialsTokenResult.
- Add tests for no token store, a different scope, a missing token
  endpoint, the SDK-side expiry check, and malformed responses.
@kishore7snehil
kishore7snehil force-pushed the feat/m2m-client-credentials branch from 9349c8f to 75290da Compare October 22, 2026 06:52
Base automatically changed from feat/obo-token-storage to main October 22, 2026 07:16
@kishore7snehil
kishore7snehil dismissed nandan-bhat’s stale review October 22, 2026 07:16

The base branch was changed.

@kishore7snehil
kishore7snehil merged commit ec72b74 into main Oct 22, 2026
7 checks passed
@kishore7snehil
kishore7snehil deleted the feat/m2m-client-credentials branch October 22, 2026 07:19
@kishore7snehil kishore7snehil mentioned this pull request Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants