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

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.5.0.tar.gz (6.6 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.5.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: msal_bearer-1.5.0.tar.gz
  • Upload date:
  • Size: 6.6 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.5.0.tar.gz
Algorithm Hash digest
SHA256 92fb25d12e3f256e14e1951675e124b523bec9c0e2da2db008f145d43cc75de5
MD5 fc53a2626150de01709698e1ba447cfa
BLAKE2b-256 f748ea41a4c4e69f343849d343104e2a503c68a619ef8f75f74be86e51dc5a53

See more details on using hashes here.

File details

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

File metadata

  • Download URL: msal_bearer-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5a93c6537b7a055f508f2c1172ba9fe38582cac6e8075a71d435827642dd2e1
MD5 a8d484fcfed1ab7232342c379b45a61f
BLAKE2b-256 7e1a7c3dd74c754737f13fff7dff39811e935dd581841dd476f4a43aa82cf7db

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