==================
msal-requests-auth
==================
Authentication using python requests and MSAL. This uses the MSAL cache
for repeated requests.
.. image:: https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square
:alt: All Contributors
:target: https://github.com/corteva/msal-requests-auth/blob/master/AUTHORS.rst
.. image:: https://img.shields.io/badge/License-BSD%203--Clause-yellow.svg
:target: https://github.com/corteva/msal-requests-auth/blob/master/LICENSE
.. image:: https://img.shields.io/pypi/v/msal_requests_auth.svg
:target: https://pypi.python.org/pypi/msal_requests_auth
.. image:: https://pepy.tech/badge/msal_requests_auth
:target: https://pepy.tech/project/msal_requests_auth
.. image:: https://img.shields.io/conda/vn/conda-forge/msal_requests_auth.svg
:target: https://anaconda.org/conda-forge/msal_requests_auth
.. image:: https://github.com/corteva/msal-requests-auth/workflows/Tests/badge.svg
:target: https://github.com/corteva/msal-requests-auth/actions?query=workflow%3ATests
.. image:: https://codecov.io/gh/corteva/msal-requests-auth/branch/master/graph/badge.svg
:target: https://codecov.io/gh/corteva/msal-requests-auth
.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white
:target: https://github.com/pre-commit/pre-commit
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:target: https://github.com/python/black
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:
- `requests <https://requests.readthedocs.io/en/latest/>`_
- `httpx <https://www.python-httpx.org/>`_
Device Code Flow
~~~~~~~~~~~~~~~~
.. note:: By default, `DeviceCodeAuth` copys the code to your clipboard and opens a webbrowser.
To disable, either set `headless=True` when initializing `DeviceCodeAuth`
or set the environment variable `MSAL_REQUESTS_AUTH_HEADLESS` to `true`.
- 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
.. code-block:: python
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
~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
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:
.. code-block:: console
$ python -m pip install msal_requests_auth
If you use conda:
.. code-block:: console
$ 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:
.. code-block:: console
python -m pip install keyrings.alt
Here is an example of how to set an alternative backend for ``keyring``:
.. code-block:: python
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.
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage
Raw data
{
"_id": null,
"home_page": "https://github.com/corteva/msal-requests-auth",
"name": "msal-requests-auth",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "msal, requests",
"author": "msal_requests_auth Contributors",
"author_email": "alansnow21@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/6f/62/b80be77015c7d28c322666a3e9986fc919e94c84ff2f50b29412f4666bba/msal_requests_auth-0.8.1.tar.gz",
"platform": "any",
"description": "==================\nmsal-requests-auth\n==================\n\nAuthentication using python requests and MSAL. This uses the MSAL cache\nfor repeated requests.\n\n.. image:: https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square\n :alt: All Contributors\n :target: https://github.com/corteva/msal-requests-auth/blob/master/AUTHORS.rst\n\n.. image:: https://img.shields.io/badge/License-BSD%203--Clause-yellow.svg\n :target: https://github.com/corteva/msal-requests-auth/blob/master/LICENSE\n\n.. image:: https://img.shields.io/pypi/v/msal_requests_auth.svg\n :target: https://pypi.python.org/pypi/msal_requests_auth\n\n.. image:: https://pepy.tech/badge/msal_requests_auth\n :target: https://pepy.tech/project/msal_requests_auth\n\n.. image:: https://img.shields.io/conda/vn/conda-forge/msal_requests_auth.svg\n :target: https://anaconda.org/conda-forge/msal_requests_auth\n\n.. image:: https://github.com/corteva/msal-requests-auth/workflows/Tests/badge.svg\n :target: https://github.com/corteva/msal-requests-auth/actions?query=workflow%3ATests\n\n.. image:: https://codecov.io/gh/corteva/msal-requests-auth/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/corteva/msal-requests-auth\n\n.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white\n :target: https://github.com/pre-commit/pre-commit\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n :target: https://github.com/python/black\n\n\nBugs/Questions\n--------------\n\n- Report bugs/feature requests: https://github.com/corteva/msal-requests-auth/issues\n- Ask questions: https://github.com/corteva/msal-requests-auth/discussions\n\n\nUsage\n-----\n\nCompatible with:\n\n- `requests <https://requests.readthedocs.io/en/latest/>`_\n- `httpx <https://www.python-httpx.org/>`_\n\n\nDevice Code Flow\n~~~~~~~~~~~~~~~~\n\n.. note:: By default, `DeviceCodeAuth` copys the code to your clipboard and opens a webbrowser.\n To disable, either set `headless=True` when initializing `DeviceCodeAuth`\n or set the environment variable `MSAL_REQUESTS_AUTH_HEADLESS` to `true`.\n\n- New in version 0.2.0: headless\n- New in version 0.6.0: MSAL_REQUESTS_AUTH_HEADLESS environment variable\n- New in version 0.7.0: KeyringTokenCache\n\n.. code-block:: python\n\n import requests\n import msal\n from msal_requests_auth.auth import DeviceCodeAuth\n from msal_requests_auth.cache import KeyringTokenCache\n\n client_id = \"<client ID from Azure AD>\"\n tenant_id = \"<tenant ID from Azure AD>\"\n application_id = \"<client ID of application you want to get a token for from Azure AD>\"\n\n with KeyringTokenCache() as token_cache:\n app = msal.PublicClientApplication(\n client_id,\n authority=f\"https://login.microsoftonline.com/{tenant_id}/\",\n token_cache=token_cache,\n )\n auth = DeviceCodeAuth(\n client=app,\n scopes=[f\"{application_id}/.default\"],\n )\n response = requests.get(\n endpoint,\n auth=auth,\n )\n\n\nClient Credentials Flow\n~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code-block:: python\n\n import requests\n import msal\n from msal_requests_auth.auth import ClientCredentialAuth\n\n client_id = \"<client ID from Azure AD>\"\n client_secret = \"<client secret for client in Azure AD>\"\n tenant_id = \"<tenant ID from Azure AD>\"\n application_id = \"<client ID of application you want to get a token for from Azure AD>\"\n app = msal.ConfidentialClientApplication(\n client_id,\n authority=(f\"https://login.microsoftonline.com/{tenant_id}/\"),\n client_credential=client_secret,\n )\n auth = ClientCredentialAuth(\n client=app,\n scopes=[f\"{application_id}/.default\"],\n )\n response = requests.get(\n endpoint,\n auth=auth,\n )\n\n\nInstallation\n------------\n\nTo install msal-requests-auth, run this command in your terminal:\n\n.. code-block:: console\n\n $ python -m pip install msal_requests_auth\n\n\nIf you use conda:\n\n.. code-block:: console\n\n $ conda install -c conda-forge msal_requests_auth\n\n\nWindows keyring backend\n~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe Windows Credential Locker is used by default by ``keyring``.\nHowever, its password length limitations often prevent\nstoring tokens. An alternative backend may resolve this limitation.\nWhen choosing a backend, be sure you are aware of its limitations.\n\n``keyrings.alt`` is an alternative ``keyring`` backend to consider:\n\n.. code-block:: console\n\n python -m pip install keyrings.alt\n\n\nHere is an example of how to set an alternative backend for ``keyring``:\n\n.. code-block:: python\n\n import keyring\n\n keyring.core._config_path().parent.mkdir(parents=True, exist_ok=True)\n keyring.core._config_path().write_text(\n \"[backend]\\ndefault-keyring=keyrings.alt.Windows.EncryptedKeyring\"\n )\n\n\nCredits\n-------\n\nThis package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.\n\n.. _Cookiecutter: https://github.com/audreyr/cookiecutter\n.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage\n",
"bugtrack_url": null,
"license": "BSD License",
"summary": "Authentication using python requests and MSAL",
"version": "0.8.1",
"project_urls": {
"Download": "http://python.org/pypi/msal-requests-auth",
"Homepage": "https://github.com/corteva/msal-requests-auth"
},
"split_keywords": [
"msal",
" requests"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8d4cbe6a7bb039b91d12aaeb0098d1b00f77df589436adefe58b81ebe70e1558",
"md5": "546cc222e54d2989488b358aa65d3d1a",
"sha256": "c6965d5973ecdf4ddfd5c723413b42792a257d6f8600fb29bfd48a5c613055eb"
},
"downloads": -1,
"filename": "msal_requests_auth-0.8.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "546cc222e54d2989488b358aa65d3d1a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 9768,
"upload_time": "2024-07-22T17:07:19",
"upload_time_iso_8601": "2024-07-22T17:07:19.824822Z",
"url": "https://files.pythonhosted.org/packages/8d/4c/be6a7bb039b91d12aaeb0098d1b00f77df589436adefe58b81ebe70e1558/msal_requests_auth-0.8.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6f62b80be77015c7d28c322666a3e9986fc919e94c84ff2f50b29412f4666bba",
"md5": "ad8c6d6d3118c57871fbc7076c2745f9",
"sha256": "67609d6591b2842430b4def56a4ceff512a3ef495c7ab678180603345947f55d"
},
"downloads": -1,
"filename": "msal_requests_auth-0.8.1.tar.gz",
"has_sig": false,
"md5_digest": "ad8c6d6d3118c57871fbc7076c2745f9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 14793,
"upload_time": "2024-07-22T17:07:21",
"upload_time_iso_8601": "2024-07-22T17:07:21.131930Z",
"url": "https://files.pythonhosted.org/packages/6f/62/b80be77015c7d28c322666a3e9986fc919e94c84ff2f50b29412f4666bba/msal_requests_auth-0.8.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-22 17:07:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "corteva",
"github_project": "msal-requests-auth",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "msal-requests-auth"
}