blob: efe2949cd55d4012bcd9b68f973909c690f7d136 [file] [log] [blame]
Joe Gregorioebb31952012-08-01 15:57:15 -04001# Copyright (C) 2012 Google Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""A keyring based Storage.
16
17A Storage for Credentials that uses the keyring module.
18"""
19
20__author__ = 'jcgregorio@google.com (Joe Gregorio)'
21
22import keyring
23import threading
24
25from client import Storage as BaseStorage
26from client import Credentials
27
28
29class Storage(BaseStorage):
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
62 def acquire_lock(self):
63 """Acquires any lock necessary to access this Storage.
64
65 This lock is not reentrant."""
66 self._lock.acquire()
67
68 def release_lock(self):
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
76 def locked_get(self):
77 """Retrieve Credential from file.
78
79 Returns:
80 oauth2client.client.Credentials
81 """
82 credentials = None
83 content = keyring.get_password(self._service_name, self._user_name)
84
85 if content is not None:
86 try:
87 credentials = Credentials.new_from_json(content)
88 credentials.set_store(self)
89 except ValueError:
90 pass
91
92 return credentials
93
94 def locked_put(self, credentials):
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
103 def locked_delete(self):
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, '')