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 threading 
 23   
 24  import keyring 
 25   
 26  from oauth2client.client import Credentials 
 27  from oauth2client.client import Storage as BaseStorage 
 28   
 29   
 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   
 64      """Acquires any lock necessary to access this Storage. 
 65   
 66      This lock is not reentrant.""" 
 67      self._lock.acquire() 
  68   
 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   
 94   
 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   
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