1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """A keyring based Storage.
16
17 A Storage for Credentials that uses the keyring module.
18 """
19
20 __author__ = 'jcgregorio@google.com (Joe Gregorio)'
21
22 import keyring
23 import threading
24
25 from client import Storage as BaseStorage
26 from client import Credentials
27
28
30 """Store and retrieve a single credential to and from the keyring.
31
32 To use this module you must have the keyring module installed. See
33 <http://pypi.python.org/pypi/keyring/>. This is an optional module and is not
34 installed with oauth2client by default because it does not work on all the
35 platforms that oauth2client supports, such as Google App Engine.
36
37 The keyring module <http://pypi.python.org/pypi/keyring/> is a cross-platform
38 library for access the keyring capabilities of the local system. The user will
39 be prompted for their keyring password when this module is used, and the
40 manner in which the user is prompted will vary per platform.
41
42 Usage:
43 from oauth2client.keyring_storage import Storage
44
45 s = Storage('name_of_application', 'user1')
46 credentials = s.get()
47
48 """
49
50 - def __init__(self, service_name, user_name):
51 """Constructor.
52
53 Args:
54 service_name: string, The name of the service under which the credentials
55 are stored.
56 user_name: string, The name of the user to store credentials for.
57 """
58 self._service_name = service_name
59 self._user_name = user_name
60 self._lock = threading.Lock()
61
63 """Acquires any lock necessary to access this Storage.
64
65 This lock is not reentrant."""
66 self._lock.acquire()
67
69 """Release the Storage lock.
70
71 Trying to release a lock that isn't held will result in a
72 RuntimeError.
73 """
74 self._lock.release()
75
93
95 """Write Credentials to file.
96
97 Args:
98 credentials: Credentials, the credentials to store.
99 """
100 keyring.set_password(self._service_name, self._user_name,
101 credentials.to_json())
102
104 """Delete Credentials file.
105
106 Args:
107 credentials: Credentials, the credentials to store.
108 """
109 keyring.set_password(self._service_name, self._user_name, '')
110