9
9
import abc
10
10
import os
11
11
import errno
12
+ import hashlib
12
13
import logging
13
14
import sys
14
15
try :
@@ -50,6 +51,9 @@ def _mkdir_p(path):
50
51
else :
51
52
raise
52
53
54
+ def _auto_hash (input_string ):
55
+ return hashlib .sha256 (input_string .encode ('utf-8' )).hexdigest ()
56
+
53
57
54
58
# We do not aim to wrap every os-specific exception.
55
59
# Here we define only the most common one,
@@ -197,19 +201,18 @@ class KeychainPersistence(BasePersistence):
197
201
and protected by native Keychain libraries on OSX"""
198
202
is_encrypted = True
199
203
200
- def __init__ (self , signal_location , service_name , account_name ):
204
+ def __init__ (self , signal_location , service_name = None , account_name = None ):
201
205
"""Initialization could fail due to unsatisfied dependency.
202
206
203
207
:param signal_location: See :func:`persistence.LibsecretPersistence.__init__`
204
208
"""
205
- if not (service_name and account_name ): # It would hang on OSX
206
- raise ValueError ("service_name and account_name are required" )
207
209
from .osx import Keychain , KeychainError # pylint: disable=import-outside-toplevel
208
210
self ._file_persistence = FilePersistence (signal_location ) # Favor composition
209
211
self ._Keychain = Keychain # pylint: disable=invalid-name
210
212
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 )
213
216
214
217
def save (self , content ):
215
218
with self ._Keychain () as locker :
@@ -247,7 +250,7 @@ class LibsecretPersistence(BasePersistence):
247
250
and protected by native libsecret libraries on Linux"""
248
251
is_encrypted = True
249
252
250
- def __init__ (self , signal_location , schema_name , attributes , ** kwargs ):
253
+ def __init__ (self , signal_location , schema_name = None , attributes = None , ** kwargs ):
251
254
"""Initialization could fail due to unsatisfied dependency.
252
255
253
256
:param string signal_location:
@@ -262,7 +265,8 @@ def __init__(self, signal_location, schema_name, attributes, **kwargs):
262
265
from .libsecret import ( # This uncertain import is deferred till runtime
263
266
LibSecretAgent , trial_run )
264
267
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 )
266
270
self ._file_persistence = FilePersistence (signal_location ) # Favor composition
267
271
268
272
def save (self , content ):
0 commit comments