Skip to main content

Authentication using python requests and MSAL

Project description

Authentication using python requests and MSAL. This uses the MSAL cache for repeated requests.

All Contributors https://img.shields.io/badge/License-BSD%203--Clause-yellow.svg https://img.shields.io/pypi/v/msal_requests_auth.svg https://pepy.tech/badge/msal_requests_auth https://img.shields.io/conda/vn/conda-forge/msal_requests_auth.svg https://github.com/corteva/msal-requests-auth/workflows/Tests/badge.svg https://codecov.io/gh/corteva/msal-requests-auth/branch/master/graph/badge.svg https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white https://img.shields.io/badge/code%20style-black-000000.svg

Bugs/Questions

Usage

Compatible with:

Device Code Flow

  • New in version 0.2.0: headless

  • New in version 0.6.0: MSAL_REQUESTS_AUTH_HEADLESS environment variable

  • New in version 0.7.0: KeyringTokenCache

import requests
import msal
from msal_requests_auth.auth import DeviceCodeAuth
from msal_requests_auth.cache import KeyringTokenCache

client_id = "<client ID from Azure AD>"
tenant_id = "<tenant ID from Azure AD>"
application_id = "<client ID of application you want to get a token for from Azure AD>"

with KeyringTokenCache() as token_cache:
    app = msal.PublicClientApplication(
        client_id,
        authority=f"https://login.microsoftonline.com/{tenant_id}/",
        token_cache=token_cache,
    )
    auth = DeviceCodeAuth(
        client=app,
        scopes=[f"{application_id}/.default"],
    )
    response = requests.get(
        endpoint,
        auth=auth,
    )

Client Credentials Flow

import requests
import msal
from msal_requests_auth.auth import ClientCredentialAuth

client_id = "<client ID from Azure AD>"
client_secret = "<client secret for client in Azure AD>"
tenant_id = "<tenant ID from Azure AD>"
application_id = "<client ID of application you want to get a token for from Azure AD>"
app = msal.ConfidentialClientApplication(
    client_id,
    authority=(f"https://login.microsoftonline.com/{tenant_id}/"),
    client_credential=client_secret,
)
auth = ClientCredentialAuth(
    client=app,
    scopes=[f"{application_id}/.default"],
)
response = requests.get(
    endpoint,
    auth=auth,
)

Installation

To install msal-requests-auth, run this command in your terminal:

$ python -m pip install msal_requests_auth

If you use conda:

$ conda install -c conda-forge msal_requests_auth

Windows keyring backend

The Windows Credential Locker is used by default by keyring. However, its password length limitations often prevent storing tokens. An alternative backend may resolve this limitation. When choosing a backend, be sure you are aware of its limitations.

keyrings.alt is an alternative keyring backend to consider:

python -m pip install keyrings.alt

Here is an example of how to set an alternative backend for keyring:

import keyring

keyring.core._config_path().parent.mkdir(parents=True, exist_ok=True)
keyring.core._config_path().write_text(
    "[backend]\ndefault-keyring=keyrings.alt.Windows.EncryptedKeyring"
)

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

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_requests_auth-0.9.3.dev0.tar.gz (15.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_requests_auth-0.9.3.dev0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file msal_requests_auth-0.9.3.dev0.tar.gz.

File metadata

  • Download URL: msal_requests_auth-0.9.3.dev0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for msal_requests_auth-0.9.3.dev0.tar.gz
Algorithm Hash digest
SHA256 77016af2d5b4fbc61ca77312f22f3a525c495d4ab2bab39bc1dd43f459191ed3
MD5 f1ab867589948a2dcfdb821f3e6a9566
BLAKE2b-256 020818fddf8876dc59d8256b69b9841cc2307f9d47b74f55470a1eb79744a241

See more details on using hashes here.

Provenance

The following attestation bundles were made for msal_requests_auth-0.9.3.dev0.tar.gz:

Publisher: release.yaml on corteva/msal-requests-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file msal_requests_auth-0.9.3.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for msal_requests_auth-0.9.3.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 b45aa1ff8b529c9e77bcf6aae832c0eb9a2b1862464d0f81267d1049f4f75c5b
MD5 f853ef76dc137d6e9f8f1410da22de15
BLAKE2b-256 36c328584f18560a3e4e22b3e0c41db468ff1b9b2fcb86ab6ea73bc74911eccb

See more details on using hashes here.

Provenance

The following attestation bundles were made for msal_requests_auth-0.9.3.dev0-py3-none-any.whl:

Publisher: release.yaml on corteva/msal-requests-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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