-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_cache.py
More file actions
89 lines (72 loc) · 3.62 KB
/
token_cache.py
File metadata and controls
89 lines (72 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Generic functions and types for working with a TokenCache that is not platform specific."""
import os
import warnings
import time
import errno
import logging
import msal
from .cache_lock import CrossPlatLock
from .persistence import (
_mkdir_p, FilePersistence,
FilePersistenceWithDataProtection, KeychainPersistence)
logger = logging.getLogger(__name__)
class PersistedTokenCache(msal.SerializableTokenCache):
"""A token cache using given persistence layer, coordinated by a file lock."""
def __init__(self, persistence, lock_location=None):
super(PersistedTokenCache, self).__init__()
self._lock_location = (
os.path.expanduser(lock_location) if lock_location
else persistence.get_location() + ".lockfile")
_mkdir_p(os.path.dirname(self._lock_location))
self._persistence = persistence
self._last_sync = 0 # _last_sync is a Unixtime
self.is_encrypted = persistence.is_encrypted
def _reload_if_necessary(self):
# type: () -> None
"""Reload cache from persistence layer, if necessary"""
try:
if self._last_sync < self._persistence.time_last_modified():
self.deserialize(self._persistence.load())
self._last_sync = time.time()
except EnvironmentError as exp:
if exp.errno != errno.ENOENT:
raise
# Otherwise, from cache's perspective, a nonexistent file is a NO-OP
def modify(self, credential_type, old_entry, new_key_value_pairs=None):
with CrossPlatLock(self._lock_location):
self._reload_if_necessary()
super(PersistedTokenCache, self).modify(
credential_type,
old_entry,
new_key_value_pairs=new_key_value_pairs)
self._persistence.save(self.serialize())
self._last_sync = time.time()
def find(self, credential_type, **kwargs): # pylint: disable=arguments-differ
with CrossPlatLock(self._lock_location):
self._reload_if_necessary()
return super(PersistedTokenCache, self).find(credential_type, **kwargs)
class FileTokenCache(PersistedTokenCache):
"""A token cache which uses plain text file to store your tokens."""
def __init__(self, cache_location, **ignored): # pylint: disable=unused-argument
warnings.warn("You are using an unprotected token cache", RuntimeWarning)
warnings.warn("Use PersistedTokenCache(...) instead", DeprecationWarning)
super(FileTokenCache, self).__init__(FilePersistence(cache_location))
UnencryptedTokenCache = FileTokenCache # For backward compatibility
class WindowsTokenCache(PersistedTokenCache):
"""A token cache which uses Windows DPAPI to encrypt your tokens."""
def __init__(
self, cache_location, entropy='',
**ignored): # pylint: disable=unused-argument
warnings.warn("Use PersistedTokenCache(...) instead", DeprecationWarning)
super(WindowsTokenCache, self).__init__(
FilePersistenceWithDataProtection(cache_location, entropy=entropy))
class OSXTokenCache(PersistedTokenCache):
"""A token cache which uses native Keychain libraries to encrypt your tokens."""
def __init__(self,
cache_location,
service_name='Microsoft.Developer.IdentityService',
account_name='MSALCache',
**ignored): # pylint: disable=unused-argument
warnings.warn("Use PersistedTokenCache(...) instead", DeprecationWarning)
super(OSXTokenCache, self).__init__(
KeychainPersistence(cache_location, service_name, account_name))