Authentication using python requests and MSAL
Project description
Authentication using python requests and MSAL. This uses the MSAL cache for repeated requests.
Bugs/Questions
Report bugs/feature requests: https://github.com/corteva/msal-requests-auth/issues
Ask questions: https://github.com/corteva/msal-requests-auth/discussions
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file msal_requests_auth-0.8.1.tar.gz
.
File metadata
- Download URL: msal_requests_auth-0.8.1.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67609d6591b2842430b4def56a4ceff512a3ef495c7ab678180603345947f55d |
|
MD5 | ad8c6d6d3118c57871fbc7076c2745f9 |
|
BLAKE2b-256 | 6f62b80be77015c7d28c322666a3e9986fc919e94c84ff2f50b29412f4666bba |
File details
Details for the file msal_requests_auth-0.8.1-py3-none-any.whl
.
File metadata
- Download URL: msal_requests_auth-0.8.1-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6965d5973ecdf4ddfd5c723413b42792a257d6f8600fb29bfd48a5c613055eb |
|
MD5 | 546cc222e54d2989488b358aa65d3d1a |
|
BLAKE2b-256 | 8d4cbe6a7bb039b91d12aaeb0098d1b00f77df589436adefe58b81ebe70e1558 |