Skip to main content

msal-bearer

Python package to get authorization token interactively for a msal application.
For public client application using user impersonation it also handles local cache and refreshing the token.

Usage 1: Public client application user impersonation

import requests
import httpx
from msal_bearer import BearerAuth

tenant_id = "YOUR_TENANT_ID"
client_id = "YOUR_CLIENT_ID"
scope = ["YOUR_SCOPE"]

auth = BearerAuth.get_auth(
    tenantID=tenant_id,
    clientID=client_id,
    scopes=scope
)

# Supports requests
response = requests.get("https://www.example.com/", auth=auth)

# and httpx
client = httpx.Client()
response = client.get("https://www.example.com/", auth=auth)

Usage 2: Confidential client with secret

from msal_bearer import Authenticator

tenant_id = "YOUR_TENANT_ID"
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
scope = ["YOUR_SCOPE"]

a = Authenticator(
    tenant_id=tenant_id
    client_id=client_id,
    client_secret=client_secret,
)
token = a.get_token(scopes=scope)

Usage 3: Confidential client with user_assertion for OBO authentication on streamlit

from msal_bearer import Authenticator

tenant_id = "YOUR_TENANT_ID"
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
scope = ["YOUR_SCOPE"]

user_assertion = st.context.headers["X-Forwarded-Access-Token"]

a = Authenticator(
    tenant_id=tenant_id
    client_id=client_id,
    client_secret=client_secret,
    user_assertion=user_assertion,
)
token = a.get_token(scopes=scope)

Installing

Clone and install using uv or install from pypi using pip.

pip install msal_bearer

Alternatives

Other similar packages include https://pypi.org/project/msal-requests-auth/ (for confidential client applications) and https://pypi.org/project/msal-interactive-token-acquirer/ (no caching implemented).

Metadata

Release files for msal-bearer 1.6.1

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

Source distribution (sdist)

Source distribution for msal-bearer 1.6.1
File Size Uploaded Provenance Actions
msal_bearer-1.6.1.tar.gz 7.3 kB Without provenance Details for msal_bearer-1.6.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for msal-bearer 1.6.1
File Interpreter ABI Platform Actions
msal_bearer-1.6.1-py3-none-any.whl Python 3 none any Details for msal_bearer-1.6.1-py3-none-any.whl

Total release size: 15.8 kB

Release files / msal_bearer-1.6.1.tar.gz

Download URL msal_bearer-1.6.1.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ec42ac76c5b208f273dbb4776db5046f96fa935c73c860add29db38c28f95805
BLAKE2b-256 checksum
How to use checksums
f8508318f92ed3fde86d53f9918fcec7a9a3503667e47f811cc66e74ddc2720c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.3 CPython/3.13.7 Darwin/27.2.0

Release files / msal_bearer-1.6.1-py3-none-any.whl

Download URL msal_bearer-1.6.1-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
569bf061aa5b8608fd05dd22c8e8bf32a7c44894bf6e71c0d404ac0ddb490b3c
BLAKE2b-256 checksum
How to use checksums
0a7bd1fee2e09ff09b480e05136d65f00e9f0272e655f9140aa24edc20c700c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.3 CPython/3.13.7 Darwin/27.2.0

Security for msal-bearer 1.6.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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