Skip to content

Commit a88fa67

Browse files
committed
Merge branch 'release-1.0.0'
2 parents 6f77b1e + bd5b407 commit a88fa67

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

README.md

+44-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
# Microsoft Authentication Extensions for Python
33

4-
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)](https://github.com/AzureAD/microsoft-authentication-library-for-python).
4+
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)](https://github.com/AzureAD/microsoft-authentication-library-for-python).
55

66
MSAL Python supports an in-memory cache by default and provides the [SerializableTokenCache](https://msal-python.readthedocs.io/en/latest/#msal.SerializableTokenCache) to perform cache serialization. You can read more about this in the MSAL Python [documentation](https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-python-token-cache-serialization). Developers are required to implement their own cache persistance across multiple platforms and Microsoft Authentication Extensions makes this simpler.
77

8-
The supported platforms are Windows, Mac and Linux.
8+
The supported platforms are Windows, Mac and Linux.
99
- Windows - [DPAPI](https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-use-data-protection) is used for encryption.
1010
- MAC - The MAC KeyChain is used.
1111
- Linux - [LibSecret](https://wiki.gnome.org/Projects/Libsecret) is used for encryption.
@@ -28,7 +28,9 @@ You can find the changes for each version under
2828

2929
## Usage
3030

31-
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.
31+
### Creating an encrypted token cache file to be used by MSAL
32+
33+
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.
3234

3335
The token cache includes a file lock, and auto-reload behavior under the hood.
3436

@@ -39,24 +41,16 @@ Here is an example of this pattern for multiple platforms (taken from the comple
3941
```python
4042
def build_persistence(location, fallback_to_plaintext=False):
4143
"""Build a suitable persistence instance based your current OS"""
42-
if sys.platform.startswith('win'):
43-
return FilePersistenceWithDataProtection(location)
44-
if sys.platform.startswith('darwin'):
45-
return KeychainPersistence(location, "my_service_name", "my_account_name")
46-
if sys.platform.startswith('linux'):
47-
try:
48-
return LibsecretPersistence(
49-
location,
50-
schema_name="my_schema_name",
51-
attributes={"my_attr1": "foo", "my_attr2": "bar"},
52-
)
53-
except: # pylint: disable=bare-except
54-
if not fallback_to_plaintext:
55-
raise
56-
logging.exception("Encryption unavailable. Opting in to plain text.")
57-
return FilePersistence(location)
44+
try:
45+
return build_encrypted_persistence(location)
46+
except:
47+
if not fallback_to_plaintext:
48+
raise
49+
logging.warning("Encryption unavailable. Opting in to plain text.")
50+
return FilePersistence(location)
5851

5952
persistence = build_persistence("token_cache.bin")
53+
print("Type of persistence: {}".format(persistence.__class__.__name__))
6054
print("Is this persistence encrypted?", persistence.is_encrypted)
6155

6256
cache = PersistedTokenCache(persistence)
@@ -66,6 +60,36 @@ Now you can use it in an MSAL application like this:
6660
app = msal.PublicClientApplication("my_client_id", token_cache=cache)
6761
```
6862

63+
### Creating an encrypted persistence file to store your own data
64+
65+
Here is an example of this pattern for multiple platforms (taken from the complete [sample here](https://github.com/AzureAD/microsoft-authentication-extensions-for-python/blob/dev/sample/persistence_sample.py)):
66+
67+
```python
68+
def build_persistence(location, fallback_to_plaintext=False):
69+
"""Build a suitable persistence instance based your current OS"""
70+
try:
71+
return build_encrypted_persistence(location)
72+
except: # pylint: disable=bare-except
73+
if not fallback_to_plaintext:
74+
raise
75+
logging.warning("Encryption unavailable. Opting in to plain text.")
76+
return FilePersistence(location)
77+
78+
persistence = build_persistence("storage.bin", fallback_to_plaintext=False)
79+
print("Type of persistence: {}".format(persistence.__class__.__name__))
80+
print("Is this persistence encrypted?", persistence.is_encrypted)
81+
82+
data = { # It can be anything, here we demonstrate an arbitrary json object
83+
"foo": "hello world",
84+
"bar": "",
85+
"service_principle_1": "blah blah...",
86+
}
87+
88+
persistence.save(json.dumps(data))
89+
assert json.loads(persistence.load()) == data
90+
```
91+
92+
6993
## Community Help and Support
7094

7195
We leverage Stack Overflow to work with the community on supporting Azure Active Directory and its SDKs, including this one!
@@ -92,4 +116,4 @@ provided by the bot. You will only need to do this once across all repos using o
92116

93117
## We value and adhere to the Microsoft Open Source Code of Conduct
94118

95-
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
119+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

sample/persistence_sample.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ def build_persistence(location, fallback_to_plaintext=False):
1111
try:
1212
return build_encrypted_persistence(location)
1313
except: # pylint: disable=bare-except
14-
# Known issue: Currently, only Linux
14+
# On Linux, encryption exception will be raised during initialization.
15+
# On Windows and macOS, they won't be detected here,
16+
# but will be raised during their load() or save().
1517
if not fallback_to_plaintext:
1618
raise
1719
logging.warning("Encryption unavailable. Opting in to plain text.")

sample/token_cache_sample.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def build_persistence(location, fallback_to_plaintext=False):
1212
try:
1313
return build_encrypted_persistence(location)
1414
except: # pylint: disable=bare-except
15-
# Known issue: Currently, only Linux
15+
# On Linux, encryption exception will be raised during initialization.
16+
# On Windows and macOS, they won't be detected here,
17+
# but will be raised during their load() or save().
1618
if not fallback_to_plaintext:
1719
raise
1820
logging.warning("Encryption unavailable. Opting in to plain text.")

0 commit comments

Comments
 (0)