Skip to main content

Python package to get auth token for a msal client application using msal, msal-extensions and azure-identity.

Project description

msal-bearer SNYK dependency check

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

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 2: 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 poetry 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).

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

msal_bearer-1.4.5.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

msal_bearer-1.4.5-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file msal_bearer-1.4.5.tar.gz.

File metadata

  • Download URL: msal_bearer-1.4.5.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Darwin/25.5.0

File hashes

Hashes for msal_bearer-1.4.5.tar.gz
Algorithm Hash digest
SHA256 83b39d646567532847739e49cc69f10cd44bab38726721ccbe0bae557d48b31a
MD5 cebbeab63e01e325a4f8575c758d3edb
BLAKE2b-256 7af2cf3227e930ec932294f05cf519c429a68eeee532cf66eefbd92a2b3cde70

See more details on using hashes here.

File details

Details for the file msal_bearer-1.4.5-py3-none-any.whl.

File metadata

  • Download URL: msal_bearer-1.4.5-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.5 Darwin/25.5.0

File hashes

Hashes for msal_bearer-1.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fd22aa80fb9cbddb1196454f03b8e416af44db9442fc0b1cb509eefac283c210
MD5 5a89eeb27af941f84bcd9fd52ca89bbe
BLAKE2b-256 e12be43a04b1f177d397e672bb6ed46715d0d11e05264ea6e28f63249aa025f4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page