99import abc
1010import os
1111import errno
12+ import hashlib
1213import logging
1314import sys
1415try :
@@ -50,6 +51,9 @@ def _mkdir_p(path):
5051 else :
5152 raise
5253
54+ def _auto_hash (input_string ):
55+ return hashlib .sha256 (input_string .encode ('utf-8' )).hexdigest ()
56+
5357
5458# We do not aim to wrap every os-specific exception.
5559# Here we define only the most common one,
@@ -197,19 +201,18 @@ class KeychainPersistence(BasePersistence):
197201 and protected by native Keychain libraries on OSX"""
198202 is_encrypted = True
199203
200- def __init__ (self , signal_location , service_name , account_name ):
204+ def __init__ (self , signal_location , service_name = None , account_name = None ):
201205 """Initialization could fail due to unsatisfied dependency.
202206
203207 :param signal_location: See :func:`persistence.LibsecretPersistence.__init__`
204208 """
205- if not (service_name and account_name ): # It would hang on OSX
206- raise ValueError ("service_name and account_name are required" )
207209 from .osx import Keychain , KeychainError # pylint: disable=import-outside-toplevel
208210 self ._file_persistence = FilePersistence (signal_location ) # Favor composition
209211 self ._Keychain = Keychain # pylint: disable=invalid-name
210212 self ._KeychainError = KeychainError # pylint: disable=invalid-name
211- self ._service_name = service_name
212- self ._account_name = account_name
213+ default_service_name = "msal-extensions" # This is also our package name
214+ self ._service_name = service_name or default_service_name
215+ self ._account_name = account_name or _auto_hash (signal_location )
213216
214217 def save (self , content ):
215218 with self ._Keychain () as locker :
@@ -247,7 +250,7 @@ class LibsecretPersistence(BasePersistence):
247250 and protected by native libsecret libraries on Linux"""
248251 is_encrypted = True
249252
250- def __init__ (self , signal_location , schema_name , attributes , ** kwargs ):
253+ def __init__ (self , signal_location , schema_name = None , attributes = None , ** kwargs ):
251254 """Initialization could fail due to unsatisfied dependency.
252255
253256 :param string signal_location:
@@ -262,7 +265,8 @@ def __init__(self, signal_location, schema_name, attributes, **kwargs):
262265 from .libsecret import ( # This uncertain import is deferred till runtime
263266 LibSecretAgent , trial_run )
264267 trial_run ()
265- self ._agent = LibSecretAgent (schema_name , attributes , ** kwargs )
268+ self ._agent = LibSecretAgent (
269+ schema_name or _auto_hash (signal_location ), attributes or {}, ** kwargs )
266270 self ._file_persistence = FilePersistence (signal_location ) # Favor composition
267271
268272 def save (self , content ):
0 commit comments