Chain exceptions from LibsecretPersistence
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
+import logging
import os
import sys
from typing import TYPE_CHECKING
+import six
+
if TYPE_CHECKING:
from typing import Any
import msal_extensions
+_LOGGER = logging.getLogger(__name__)
+
class TokenCachePersistenceOptions(object):
"""Options for persistent token caching.
@@ -86,12 +91,16 @@ def _get_persistence(allow_unencrypted, account_name, cache_name):
return msal_extensions.LibsecretPersistence(
file_path, cache_name, {"MsalClientID": "Microsoft.Developer.IdentityService"}, label=account_name
)
- except ImportError:
+ except Exception as ex: # pylint:disable=broad-except
+ _LOGGER.debug('msal-extensions is unable to encrypt a persistent cache: "%s"', ex, exc_info=True)
if not allow_unencrypted:
- raise ValueError(
- "PyGObject is required to encrypt the persistent cache. Please install that library or "
- + 'specify "allow_unencrypted_storage=True" to store the cache without encryption.'
+ error = ValueError(
+ "Cache encryption is impossible because libsecret dependencies are not installed or are unusable,"
+ + " for example because no display is available (as in an SSH session). The chained exception has"
+ + ' more information. Specify "allow_unencrypted_storage=True" to store the cache unencrypted'
+ + " instead of raising this exception."
)
+ six.raise_from(error, ex)
return msal_extensions.FilePersistence(file_path)
raise NotImplementedError("A persistent cache is not available in this environment.")
