Package oauth2client :: Module keyring_storage
[hide private]
[frames] | no frames]

Source Code for Module oauth2client.keyring_storage

  1  # Copyright 2014 Google Inc. All rights reserved. 
  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   
 17  A Storage for Credentials that uses the keyring module. 
 18  """ 
 19   
 20  __author__ = 'jcgregorio@google.com (Joe Gregorio)' 
 21   
 22  import threading 
 23   
 24  import keyring 
 25   
 26  from oauth2client.client import Credentials 
 27  from oauth2client.client import Storage as BaseStorage 
 28   
 29   
30 -class Storage(BaseStorage):
31 """Store and retrieve a single credential to and from the keyring. 32 33 To use this module you must have the keyring module installed. See 34 <http://pypi.python.org/pypi/keyring/>. This is an optional module and is not 35 installed with oauth2client by default because it does not work on all the 36 platforms that oauth2client supports, such as Google App Engine. 37 38 The keyring module <http://pypi.python.org/pypi/keyring/> is a cross-platform 39 library for access the keyring capabilities of the local system. The user will 40 be prompted for their keyring password when this module is used, and the 41 manner in which the user is prompted will vary per platform. 42 43 Usage: 44 from oauth2client.keyring_storage import Storage 45 46 s = Storage('name_of_application', 'user1') 47 credentials = s.get() 48 49 """ 50
51 - def __init__(self, service_name, user_name):
52 """Constructor. 53 54 Args: 55 service_name: string, The name of the service under which the credentials 56 are stored. 57 user_name: string, The name of the user to store credentials for. 58 """ 59 self._service_name = service_name 60 self._user_name = user_name 61 self._lock = threading.Lock()
62
63 - def acquire_lock(self):
64 """Acquires any lock necessary to access this Storage. 65 66 This lock is not reentrant.""" 67 self._lock.acquire()
68
69 - def release_lock(self):
70 """Release the Storage lock. 71 72 Trying to release a lock that isn't held will result in a 73 RuntimeError. 74 """ 75 self._lock.release()
76
77 - def locked_get(self):
78 """Retrieve Credential from file. 79 80 Returns: 81 oauth2client.client.Credentials 82 """ 83 credentials = None 84 content = keyring.get_password(self._service_name, self._user_name) 85 86 if content is not None: 87 try: 88 credentials = Credentials.new_from_json(content) 89 credentials.set_store(self) 90 except ValueError: 91 pass 92 93 return credentials
94
95 - def locked_put(self, credentials):
96 """Write Credentials to file. 97 98 Args: 99 credentials: Credentials, the credentials to store. 100 """ 101 keyring.set_password(self._service_name, self._user_name, 102 credentials.to_json())
103
104 - def locked_delete(self):
105 """Delete Credentials file. 106 107 Args: 108 credentials: Credentials, the credentials to store. 109 """ 110 keyring.set_password(self._service_name, self._user_name, '')
111