Microsoft Authentication Library extensions (MSAL EX) provides a persistence API that can save your data on disk, encrypted on Windows, macOS and Linux. Concurrent data access will be coordinated by a file lock mechanism.
Project description
Microsoft Authentication Extensions for Python
The Microsoft Authentication Extensions for Python offers secure mechanisms for client applications to perform cross-platform token cache serialization and persistence. It gives additional support to the Microsoft Authentication Library for Python (MSAL).
MSAL Python supports an in-memory cache by default and provides the SerializableTokenCache to perform cache serialization. You can read more about this in the MSAL Python documentation. Developers are required to implement their own cache persistance across multiple platforms and Microsoft Authentication Extensions makes this simpler.
The supported platforms are Windows, Mac and Linux.
- Windows - DPAPI is used for encryption.
- MAC - The MAC KeyChain is used.
- Linux - LibSecret is used for encryption.
Note: It is recommended to use this library for cache persistance support for Public client applications such as Desktop apps only. In web applications, this may lead to scale and performance issues. Web applications are recommended to persist the cache in session. Take a look at this webapp sample.
Installation
You can find Microsoft Authentication Extensions for Python on Pypi.
- 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.
- Run
pip install msal-extensions
.
Versions
This library follows Semantic Versioning.
You can find the changes for each version under Releases.
Usage
Creating an encrypted token cache file to be used by MSAL
The Microsoft Authentication Extensions library provides the PersistedTokenCache
which accepts a platform-dependent persistence instance. This token cache can then be used to instantiate the PublicClientApplication
in MSAL Python.
The token cache includes a file lock, and auto-reload behavior under the hood.
Here is an example of this pattern for multiple platforms (taken from the complete sample here):
def build_persistence(location, fallback_to_plaintext=False):
"""Build a suitable persistence instance based your current OS"""
try:
return build_encrypted_persistence(location)
except:
if not fallback_to_plaintext:
raise
logging.warning("Encryption unavailable. Opting in to plain text.")
return FilePersistence(location)
persistence = build_persistence("token_cache.bin")
print("Type of persistence: {}".format(persistence.__class__.__name__))
print("Is this persistence encrypted?", persistence.is_encrypted)
cache = PersistedTokenCache(persistence)
Now you can use it in an MSAL application like this:
app = msal.PublicClientApplication("my_client_id", token_cache=cache)
Creating an encrypted persistence file to store your own data
Here is an example of this pattern for multiple platforms (taken from the complete sample here):
def build_persistence(location, fallback_to_plaintext=False):
"""Build a suitable persistence instance based your current OS"""
try:
return build_encrypted_persistence(location)
except: # pylint: disable=bare-except
if not fallback_to_plaintext:
raise
logging.warning("Encryption unavailable. Opting in to plain text.")
return FilePersistence(location)
persistence = build_persistence("storage.bin", fallback_to_plaintext=False)
print("Type of persistence: {}".format(persistence.__class__.__name__))
print("Is this persistence encrypted?", persistence.is_encrypted)
data = { # It can be anything, here we demonstrate an arbitrary json object
"foo": "hello world",
"bar": "",
"service_principle_1": "blah blah...",
}
persistence.save(json.dumps(data))
assert json.loads(persistence.load()) == data
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 browse 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
Contributing
All code is licensed under the MIT license and we triage actively on GitHub.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
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
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-extensions-1.0.0.tar.gz
.
File metadata
- Download URL: msal-extensions-1.0.0.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c676aba56b0cce3783de1b5c5ecfe828db998167875126ca4b47dc6436451354 |
|
MD5 | 93f47277871a06956432af604c543cb2 |
|
BLAKE2b-256 | 335e2e23593c67df0b21ffb141c485ca0ae955569203d7ff5064040af968cb81 |
File details
Details for the file msal_extensions-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: msal_extensions-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 19.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91e3db9620b822d0ed2b4d1850056a0f133cba04455e62f11612e40f5502f2ee |
|
MD5 | df511df585ccd5ea52e2916a45e6cd5b |
|
BLAKE2b-256 | 5234a8995d6f0fa626ff6b28dbd9c90f6c2a46bd484bc7ab343d078b0c6ff1a7 |