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