Verify OIDC JWT identity tokens using OIDC discovery
Project description
Python library to verify id tokens using OIDC discovery
OpenID connect identity tokens are a popular choice for federating identity between different systems without the need to share secrets. For example Trusted publishing on PyPI allows use of OIDC tokens created by GitHub or GitLab CI jobs to be used to authenticate when uploading new Python packages. Similarly, OIDC tokens can be used to authenticate to Google Cloud, AWS and Azure from any OIDC identity provider.
The jwt.io and jwt.ms tools allow validating OIDC id tokens without first configuring public keys by means of the OpenID connect discovery protocol.
This library implements the OpenID Connect discovery standard in Python to allow verification of OpenID Connect id tokens without previous configuration of public keys, etc.
Both synchronous and asynchronous (asyncio
) implementations are provided.
Example
Suppose you created a GitLab OIDC token as part of a CI job to make an authenticated HTTP GET request to some service:
# .gitlab-ci.yml within https://gitlab.com/my-group/my-project
job_with_id_token:
id_tokens:
ID_TOKEN:
aud: https://my-service.example.com
script:
- curl -X GET -H "Authorization: Bearer $ID_TOKEN" https://my-service.example.com
The following example shows how to verify the OIDC token came from a specific project within a backend implementation:
from typing import Any
from federatedidentity import Issuer, verifiers, verify_id_token
# Use OIDC discovery to fetch public keys for verifying GitLab tokens.
GITLAB_ISSUER = Issuer.from_discovery("https://gitlab.com")
# Expected project path for id token
EXPECTED_PROJECT_PATH = "my-group/my-project"
# Expected audience claim for id token.
EXPECTED_AUDIENCE_CLAIM = "https://my-service.example.com"
def verify_gitlab_token(token: str) -> dict[str, Any]:
"""
Verify an OIDC token from GitLab and return the dictionary of claims. Raises
federatedidentity.FederatedIdentityError if the token failed verification.
"""
return verify_id_token(
token,
valid_issuers=[GITLAB_ISSUER],
valid_audiences=[EXPECTED_AUDIENCE_CLAIM],
required_claims=[
# The "project_path" claim must match the expected project.
{"project_path": EXPECTED_PROJECT_PATH},
],
)
See the full documentation for more examples.
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
File details
Details for the file verify_oidc_identity-0.3.6.tar.gz
.
File metadata
- Download URL: verify_oidc_identity-0.3.6.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a55276a4c139098833298d4091a88d3a4e572907e63585e2a5f69ec8515fb00f |
|
MD5 | b5a013ecae1d0e86e22c5444225f9ee8 |
|
BLAKE2b-256 | 2e2524ada0f0c7ab5342614d7a2221e87a2e3fa275ac2c620b9d3afe6024a8e4 |
Provenance
The following attestation bundles were made for verify_oidc_identity-0.3.6.tar.gz
:
Publisher:
main.yml
on rjw57/verify-oidc-identity
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
verify_oidc_identity-0.3.6.tar.gz
- Subject digest:
a55276a4c139098833298d4091a88d3a4e572907e63585e2a5f69ec8515fb00f
- Sigstore transparency entry: 150132143
- Sigstore integration time:
- Predicate type:
File details
Details for the file verify_oidc_identity-0.3.6-py3-none-any.whl
.
File metadata
- Download URL: verify_oidc_identity-0.3.6-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78f25acdc73d94fc54b05dbd9dace345106d20043562b64e3bd914f56fd3d033 |
|
MD5 | 1a45a2b09798960dc0202756ebd6a3f9 |
|
BLAKE2b-256 | 60a02ee69b3799e739e472eea7dc7e913065906c00b7e407e7013971a71a37d9 |
Provenance
The following attestation bundles were made for verify_oidc_identity-0.3.6-py3-none-any.whl
:
Publisher:
main.yml
on rjw57/verify-oidc-identity
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
verify_oidc_identity-0.3.6-py3-none-any.whl
- Subject digest:
78f25acdc73d94fc54b05dbd9dace345106d20043562b64e3bd914f56fd3d033
- Sigstore transparency entry: 150132144
- Sigstore integration time:
- Predicate type: