Skip to main content

The Microsoft Authentication Library (MSAL) for Python library enables your app to access the Microsoft Cloud by supporting authentication of users with Microsoft Azure Active Directory accounts (AAD) and Microsoft Accounts (MSA) using industry standard OAuth2 and OpenID Connect.

Project description

Microsoft Authentication Library (MSAL) for Python Preview

The MSAL library for Python enables your app to access the Microsoft Cloud by supporting authentication of users with Microsoft Azure Active Directory accounts and Microsoft Accounts using industry standard OAuth2 and OpenID Connect. Soon MSAL Python will also support Azure AD B2C.

Not sure whether this is the SDK you are looking for? There are other Microsoft Identity SDKs here.

Important Note about the MSAL Preview

This library is suitable for use in a production environment. We provide the same production level support for this library as we do our current production libraries. During the preview we may make changes to the API, internal cache format, and other mechanisms of this library, which you will be required to take along with bug fixes or feature improvements. This may impact your application. For instance, a change to the cache format may impact your users, such as requiring them to sign in again. An API change may require you to update your code. When we provide the General Availability release we will require you to update to the General Availability version within six months, as applications written using a preview version of library may no longer work.

Installation

  1. If you haven't already, install and/or upgrade the pip of your Python environment to a recent version. We tested with pip 18.1.
  2. As usual, just run pip install msal.

Usage and Samples

Before using MSAL Python (or any MSAL SDKs, for that matter), you will have to register your application with the AAD 2.0 endpoint.

Acquiring tokens with MSAL Python need to follow this 3-step pattern.

  1. MSAL proposes a clean separation between public client applications, and confidential client applications. So you will first create either a PublicClientApplication or a ConfidentialClientApplication instance, and ideally reuse it during the lifecycle of your app. The following example shows a PublicClientApplication:

    from msal import PublicClientApplication
    app = PublicClientApplication("your_client_id", authority="...")
    

    Later, each time you would want an access token, you start by:

    result = None  # It is just an initial value. Please follow instructions below.
    
  2. The API model in MSAL provides you explicit control on how to utilize token cache. This cache part is technically optional, but we highly recommend you to harness the power of MSAL cache. It will automatically handle the token refresh for you.

    # We now check the cache to see
    # whether we already have some accounts that the end user already used to sign in before.
    accounts = app.get_accounts()
    if accounts:
        # If so, you could then somehow display these accounts and let end user choose
        print("Pick the account you want to use to proceed:")
        for a in accounts:
            print(a["username"])
        # Assuming the end user chose this one
        chosen = accounts[0]
        # Now let's try to find a token in cache for this account
        result = app.acquire_token_silent(config["scope"], account=chosen)
    
  3. Either there is no suitable token in the cache, or you chose to skip the previous step, now it is time to actually send a request to AAD to obtain a token. There are different methods based on your client type and scenario. Here we demonstrate a placeholder flow.

    if not result:
        # So no suitable token exists in cache. Let's get a new one from AAD.
        result = app.acquire_token_by_one_of_the_actual_method(..., scopes=["User.Read"])
    if "access_token" in result:
        print(result["access_token"])  # Yay!
    else:
        print(result.get("error"))
        print(result.get("error_description"))
        print(result.get("correlation_id"))  # You may need this when reporting a bug
    

That is the high level pattern. There will be some variations for different flows. They are demonstrated in samples hosted right in this repo.

Documentation

The generic documents on Auth Scenarios and Auth protocols are recommended reading.

There is the API reference of MSAL Python which documents every parameter of each public method.

More and more detail about MSAL Python functionality and usage will be documented in the Wiki.

Versions

This library follows Semantic Versioning.

You can find the changes for each version under Releases.

Community Help and Support

We leverage Stack Overflow to work with the community on supporting Azure Active Directory and its SDKs, including this one! We highly recommend you ask your questions on Stack Overflow (we're all on there!) Also browser existing issues to see if someone has had your question before.

We recommend you use the "msal" tag so we can see it! Here is the latest Q&A on Stack Overflow for MSAL: http://stackoverflow.com/questions/tagged/msal

Security Reporting

If you find a security issue with our libraries or services please report it to secure@microsoft.com with as much detail as possible. Your submission may be eligible for a bounty through the Microsoft Bounty program. Please do not post security issues to GitHub Issues or any other public site. We will contact you shortly upon receiving the information. We encourage you to get notifications of when security incidents occur by visiting this page and subscribing to Security Advisory Alerts.

Contributing

All code is licensed under the MIT license and we triage actively on GitHub. We enthusiastically welcome contributions and feedback. Please read the contributing guide before starting.

We Value and Adhere to the Microsoft Open Source Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

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

Uploaded Source

Built Distributions

msal-0.8.0-py2.py3-none-any.whl (43.2 kB view details)

Uploaded Python 2 Python 3

msal-0.8.0-py2-none-any.whl (43.2 kB view details)

Uploaded Python 2

File details

Details for the file msal-0.8.0.tar.gz.

File metadata

  • Download URL: msal-0.8.0.tar.gz
  • Upload date:
  • Size: 39.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.15

File hashes

Hashes for msal-0.8.0.tar.gz
Algorithm Hash digest
SHA256 11aa3bf98753d7e6825bde7e1998bc4af501ba8c547cc08198aeb7fa533762bd
MD5 0d3c4fc4ab0444c4671ea946a57c8dcd
BLAKE2b-256 74f511b9c56c54943e2102be129e71bcbf7ae9292cec541e5e3b34ede446c3dc

See more details on using hashes here.

File details

Details for the file msal-0.8.0-py2.py3-none-any.whl.

File metadata

  • Download URL: msal-0.8.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 43.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.12

File hashes

Hashes for msal-0.8.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 63ce9a5ae630add03388fabdb30ad6bf4984d982ce1c1f062a980b1850ffaf52
MD5 d176fef260ea75c1b473ae314030b468
BLAKE2b-256 5b41f73c640b462908e4afef0e99ecf8d68b76c2db2375e29ca6f202e1141627

See more details on using hashes here.

File details

Details for the file msal-0.8.0-py2-none-any.whl.

File metadata

  • Download URL: msal-0.8.0-py2-none-any.whl
  • Upload date:
  • Size: 43.2 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/2.7.15

File hashes

Hashes for msal-0.8.0-py2-none-any.whl
Algorithm Hash digest
SHA256 77bf648fecd3552c56d8f57e1839d398bec5deb63c87ab8233d4ad86981c4fd8
MD5 a3629911a6461a41cd2ebf076a2678ee
BLAKE2b-256 4df1e80a6a8966d74bd5d2233af364769261613d92af2b35278087ade07dd43a

See more details on using hashes here.

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