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

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.7.1.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

msal_requests_auth-0.7.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file msal_requests_auth-0.7.1.tar.gz.

File metadata

  • Download URL: msal_requests_auth-0.7.1.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for msal_requests_auth-0.7.1.tar.gz
Algorithm Hash digest
SHA256 2f70b9f3e94ea6e460c4f891181ffe5e4d1a9bb5a83a99c44823d37dc4209896
MD5 ac560a777498c64d23227c7ff57575c4
BLAKE2b-256 5bbaf0a28ffc8516bbed2c7cb79eceb3604a67e506c434843c520ae1d012147d

See more details on using hashes here.

Provenance

File details

Details for the file msal_requests_auth-0.7.1-py3-none-any.whl.

File metadata

File hashes

Hashes for msal_requests_auth-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 523830f385d3097c83355a2c554061b8c20f5180ef77ab76499e1fbee3958f20
MD5 15afd98b778c72f7bf151f460837b679
BLAKE2b-256 90a4348d55d81b2492118e52df88d0e2ce110f4cb6d6e7bd1da615895c5e012c

See more details on using hashes here.

Provenance

Supported by

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