Skip to main content

azure-ad-verify-token

Verify JWT issued by Azure Active Directory B2C in Python 🐍.

Build Status PyPI version

Validation steps this library makes:

1. Accepts an Azure AD B2C JWT.
2. Extracts `kid` from unverified headers.
3. Finds `kid` within Azure JWKS.
4. Obtains RSA key from JWK.
5. Calls `jwt.decode` with nessary parameters, which inturn validates:

    - Signature
    - Expiration
    - Audience
    - Issuer
    - Key
    - Algorithm

License

https://creativecommons.org/licenses/by-nc-nd/4.0/

This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

For commercial use licenses contact us.

Installation

pip install azure-ad-verify-token

Usage

First you'll need to get your azure_ad_app_id, azure_ad_issuer and azure_ad_jwks_uri. See below steps to obtain these.

  1. For app id. Login to Azure Portal, navigation to Azure AD B2C, Click on the Applications section and your app id should be listed.

  2. For Issuer and JWKS URI:

Under the "User Flows", note down the name of yours, this will be needed shortly.

https://i.imgur.com/uYmghAZ.png

Next, under Azure AD B2C, within the Applications section.

Click on "Endpoints".

Copy the endpoint with the label "OpenID Connect configuration endpoint (v2)"

It will look something like:

https://exampletenant.b2clogin.com/exampletenant.onmicrosoft.com/<policy-name>/v2.0/.well-known/openid-configuration

https://i.imgur.com/3bQGZBn.png

Now replace <policy-name> with the name of your User Flow from earlier

https://exampletenant.b2clogin.com/exampletenant.onmicrosoft.com/B2C_1_app_sign_in/v2.0/.well-known/openid-configuration

Now visit that URL in your web browser.

You should get a JSON response, note down the values for the keys 'issuer' and 'jwks_uri'.

Now you have those values you can proceed to verify a Azure generated JWT Token.

from azure_ad_verify_token import verify_jwt

azure_ad_app_id = 'b74cd13f-8f79-4c98-b748-7789ecb1111d5'
azure_ad_issuer = 'https://exampletenant.b2clogin.com/0867afa-24e7-40e9-9d27-74bb598zzzzc/v2.0/'
azure_ad_jwks_uri = 'https://exampletenant.b2clogin.com/exampletenant.onmicrosoft.com/B2C_1_app_sign_in/discovery/v2.0/keys'
payload = verify_jwt(
    token='<AZURE_JWT_TO_VERIFY_HERE>',
    valid_audiences=[azure_ad_app_id],
    issuer=azure_ad_issuer,
    jwks_uri=azure_ad_jwks_uri,
    verify=True,
)

print(payload)
{'aud': 'b74cd13f-8f79-4c98-b748-7789ecb1111d5',
 'auth_time': 1591800638,
 'emails': ['bob@example.com'],
 'exp': 1591804238,
 'family_name': 'Exp Admin',
 'given_name': 'Richard',
 'iat': 1591800638,
 'iss': 'https://exampletenant.b2clogin.com/90867afa-24e7-40e9-9d27-74bb598zzzzc/v2.0/',
 'nbf': 1591800638,
 'sub': 'e07bbc53-b812-4572-9edc-4b5d4ac88447',
 'tfp': 'B2C_1_app_sign_in',
 'ver': '1.0'}

If something goes wrong, one of the below exceptions will be raised:

# If the token is found to be invalid.
azure_ad_verify_token.InvalidAuthorizationToken

# Base exception, raised if the checks which call the Azure server recieve an unhappy response.
azure_ad_verify_token.AzureVerifyTokenError

Release History

0.4.0 (2022-12-29)

  • Adds optional kwargs to verify_jwt function.

0.3.0 (2022-11-03)

  • Adds optional 'options' param to verify_jwt function.

0.2.1 (2021-12-23)

  • Widen permitted dependency versions.

0.2.0 (2021-10-19)

  • Adds optional arg verify to verify_jwt function.

0.1.3 (2021-03-11)

  • Allow wider versions of cryptography dep.

0.1.2 (2020-06-30)

  • Corrects required dependency version range.

0.1.1 (2020-06-29)

  • Documentation updates.

0.1.0 (2020-06-29)

  • Initial release.

Release files for azure-ad-verify-token 0.4.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 azure-ad-verify-token 0.4.0
File Size Uploaded
azure-ad-verify-token-0.4.0.tar.gz 12.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for azure-ad-verify-token 0.4.0
File Interpreter ABI Platform
azure_ad_verify_token-0.4.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 22.9 kB

Release files / azure-ad-verify-token-0.4.0.tar.gz

Download URL azure-ad-verify-token-0.4.0.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
87248c2822e0d885b7c49083391dceb772fa565b521485359b9b1df57abe9ba5
BLAKE2b-256 checksum
How to use checksums
b0eefd464f4e2a13e5f1c998a2cefb0108c51f2e9e1d558943e49ef6bf0b60da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.3.dev21+g89fba32 CPython/3.8.10

Release files / azure_ad_verify_token-0.4.0-py2.py3-none-any.whl

Download URL azure_ad_verify_token-0.4.0-py2.py3-none-any.whl
Size 10.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5b2c92646433e91aaf123240d3b3d59133a56c910766a4c434dfd617c0e85f3f
BLAKE2b-256 checksum
How to use checksums
1f1642acc7900e87c42095d226bb99fa030a79a6f13d539522bd795d0405c122
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.3.dev21+g89fba32 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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