def _get_item(self, keyring, login, create=False): login_key = self._key_for_login(keyring, login) try: item_keys = gnomekeyring.list_item_ids_sync("Login") except gnomekeyring.NoSuchKeyringError: if create: password = self._get_keychain_password() if password is not None: gnomekeyring.create_sync("Login", password) item_keys = [] else: return None, None else: return None, None for key in item_keys: item_info = gnomekeyring.item_get_info_sync("Login", key) if item_info.get_display_name() == login_key: return item_info, key if not create: return None, None item_key = gnomekeyring.item_create_sync( "Login", gnomekeyring.ITEM_GENERIC_SECRET, login_key, {}, str(uuid.uuid4()), True ) item = gnomekeyring.item_get_info_sync("Login", item_key) return item, item_key
def test_keyring_populate(self): attr = { 'token' : 'the-token', 'consumer-key' : 'the-consumer-key', 'usage' : 'software-center-agent-token', } secret = 'consumer_secret=xxx&token=xxx&consumer_key=xxx&token_secret=xxx&name=s-c' keyring_names = gk.list_keyring_names_sync() print keyring_names self.assertFalse(self.KEYRING_NAME in keyring_names) gk.create_sync(self.KEYRING_NAME, "") keyring_names = gk.list_keyring_names_sync() self.assertTrue(self.KEYRING_NAME in keyring_names) res = gk.item_create_sync(self.KEYRING_NAME, gk.ITEM_GENERIC_SECRET, "Software Center Agent token", attr, secret, True) # update if exists self.assertTrue(res) # get the token from the keyring using the 'usage' field # in the attr search_attr = { 'usage' : 'software-center-agent-token', } found = gk.find_items_sync(gk.ITEM_GENERIC_SECRET, search_attr) self.assertEqual(len(found), 1) for item in found: self.assertEqual(item.keyring, self.KEYRING_NAME) #print item.item_id, item.attributes self.assertEqual(item.secret, secret)
def __loadAuthInfo(id): """ Load the login/password associated with id, either from the Gnome keyring or from the prefs """ try: import gnomekeyring as gk useGK = True except: useGK = False # No Gnome keyring if not useGK: login = prefs.get(__name__, id + '_login', None) passwd = prefs.get(__name__, id + '_passwd', None) if login is not None and passwd is not None: return (login, b64decode(passwd)) else: return None # From here we can use the Gnome keyring __loadKeyring() try: gk.create_sync(__keyring, None) except gk.AlreadyExistsError: pass token = prefs.get(__name__, id + '_gkToken', None) if token is not None: try: login, passwd = gk.item_get_info_sync(__keyring, token).get_secret().split('\n') return (login, passwd) except: pass return None
def _get_item(self, keyring, login, create=False): login_key = self._key_for_login(keyring, login) try: item_keys = gnomekeyring.list_item_ids_sync("Login") except gnomekeyring.NoSuchKeyringError: if create: password = self._get_keychain_password() if password is not None: gnomekeyring.create_sync("Login", password) item_keys = [] else: return None, None else: return None, None for key in item_keys: item_info = gnomekeyring.item_get_info_sync("Login", key) if item_info.get_display_name() == login_key: return item_info, key if not create: return None, None item_key = gnomekeyring.item_create_sync( "Login", gnomekeyring.ITEM_GENERIC_SECRET, login_key, {}, str(uuid.uuid4()), True) item = gnomekeyring.item_get_info_sync("Login", item_key) return item, item_key
def __init__(self, keyring_name): """ Initialize a new instance of PasswordManager bound to the specified keyring. Throws a NoPassSetException if a new keyring should be created but no password is set by the user. """ self.keyring_name = keyring_name if not self._keyring_exists: # We want to create a new keyring and therefore we need # a password. We open an inputbox to ask the user for a # new password. We will ask twice because we need a validation # that the user entered the password correct. PASSWORD_MSG = "Enter a new password for the keyring to be created" PASSWORD_MSG_RETRY = "The passwords don't match. Please retry" while True: # We ask the user for a new password. We will ask twice for a # password so we can validate that the user made no typo. # If the user aborts the process we raise a NoPassSetException frist_attempt = inputbox("Create new keyring for gcm", PASSWORD_MSG) if first_attempt == False: raise NoPassSetException second_attempt = inputbox("Create new keyring for gcm", "Re-enter your password!") password_correct = (first_attempt == second_attempt) if password_correct: newpassword = first_attempt break else: PASSWORD_MSG = PASSWORD_MSG_RETRY gk.create_sync(self.keyring_name, newpassword) gk.set_lock_timeout(self.keyring_name, 60) gk.set_lock_on_idle(self.keyring_name, True)
def __check_keyring (self): if not self.loaded: try: gk.list_item_ids_sync(self._KEYRING_NAME) except Exception, e: logger.exception("Error getting the gnome keyring. We'll try to create it: %s",e) logger.debug("Creating keyring " + self._KEYRING_NAME) gk.create_sync(self._KEYRING_NAME, None) self.loaded = True
def cmd_export_to_gnome_keyring(): """Write out accounts to Gnome Keyring. Only useful for 'backup', if you have keyring tools. There is currently no way to import these keys back into kip. Requires python2 and gnomekeyring lib. Note that this does NOT make the passwords usable to Chrome - this is not a counterpart to import_chrome_gnome_keyring. """ import time import gnomekeyring as gk import glib glib.set_application_name('kip') keyrings = gk.list_keyring_names_sync() if not 'kip' in keyrings: gk.create_sync('kip', None) # None means prompt user for password for filename in glob.glob('{}/*'.format(HOME_PWD)): user, pwd, notes = extract(filename) domain = os.path.basename(filename) print("Exporting {} ({})".format(domain, user)) """ msg = "Export %s (%s)? [y|N]" % (domain, user) try: choice = raw_input(msg) except NameError: # python 3 choice = input(msg) if choice.lower() != 'y': print('Skipping') continue """ attributes = { "username": user.encode("utf8"), "notes": notes.encode("utf8"), "date_created": str(int(time.time())), } gk.item_create_sync('kip', gk.ITEM_GENERIC_SECRET, domain, attributes, pwd.encode("utf8"), True)
def __init__(self): self.keyring = None if not haveKeyring: return try: self.keyring = gnomekeyring.get_default_keyring_sync() if self.keyring == None: # Code borrowed from # http://trac.gajim.org/browser/src/common/passwords.py self.keyring = 'default' try: gnomekeyring.create_sync(self.keyring, None) except gnomekeyring.AlreadyExistsError: pass except: logging.exception("Error determining keyring") self.keyring = None
def get_item_id(): global _item_id if _item_id: return _item_id try: results = gk.find_items_sync(gk.ITEM_GENERIC_SECRET, _attrs) _item_id = results[0].item_id except gk.NoMatchError: # Check if ''_keyring'' has been created. if _keyring not in gk.list_keyring_names_sync(): # if it is missing then create a new keyring called ''_keyring'' gk.create_sync(_keyring, _keyring) # Create item _item_id = gk.item_create_sync(_keyring, _type, _display_name, _attrs, '', True) return _item_id
def first_time_setup(self): """First time running Open Sesame? Create keyring and an auto-unlock key in default keyring. Make sure these things don't already exist. """ if not self._auto_unlock_key_position(): pw = password.create_passwords()[0] attrs = {'application': self.keyring} gkr.item_create_sync(self.default_keyring ,gkr.ITEM_GENERIC_SECRET ,self.keyring ,attrs ,pw ,True) found_pos = self._auto_unlock_key_position() item_info = gkr.item_get_info_sync(self.default_keyring, found_pos) gkr.create_sync(self.keyring, item_info.get_secret())
def store_password(self, entry, password, attributes=None, entry_type=EntryType.GENERIC): """Create new entry in keyring with specified data""" assert self.is_available() # create a new keyring if it doesn't exist if not self.KEYRING_NAME in keyring.list_keyring_names_sync(): dialog = PasswordDialog(self._application) dialog.set_title(_('New keyring')) dialog.set_label( _('We need to create a new keyring to safely ' 'store your passwords. Choose the password you ' 'want to use for it.')) response = dialog.get_response() if response[0] == gtk.RESPONSE_OK \ and response[1] == response[2]: # create new keyring keyring.create_sync(self.KEYRING_NAME, response[1]) self.__update_info() else: # wrong password raise KeyringCreateError('No keyring to store password to.') # if keyring is locked, try to unlock it if self.is_locked() and not self.__unlock_keyring(): return False # store password to existing keyring keyring.item_create_sync( self.KEYRING_NAME, self.KEYRING_TYPE[entry_type], entry, attributes if attributes is not None else {}, password, True # update if exists ) return True
def store_password(self, entry, password, attributes=None, entry_type=EntryType.GENERIC): """Create new entry in keyring with specified data""" assert self.is_available() # create a new keyring if it doesn't exist if not self.KEYRING_NAME in keyring.list_keyring_names_sync(): dialog = PasswordDialog(self._application) dialog.set_title(_('New keyring')) dialog.set_label(_( 'We need to create a new keyring to safely ' 'store your passwords. Choose the password you ' 'want to use for it.' )) response = dialog.get_response() if response[0] == gtk.RESPONSE_OK \ and response[1] == response[2]: # create new keyring keyring.create_sync(self.KEYRING_NAME, response[1]) self.__update_info() else: # wrong password raise KeyringCreateError('No keyring to store password to.') # if keyring is locked, try to unlock it if self.is_locked() and not self.__unlock_keyring(): return False # store password to existing keyring keyring.item_create_sync( self.KEYRING_NAME, self.KEYRING_TYPE[entry_type], entry, attributes if attributes is not None else {}, password, True # update if exists ) return True
def __init__(self): self.keyring_name = 'proxywise' self.__keyring_password = '******' self.home = expanduser("~") self.config_path = self.home + "/.config/proxywise" self.config_file_name = self.config_path + "/config.json" if not os.path.exists(self.config_path): os.makedirs(self.config_path) if not os.path.exists(self.config_file_name): config_file = open(self.config_file_name, 'w') json.dump( { "active_profile": None, "ignore_host_list": ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"] }, config_file, sort_keys=True, indent=4, separators=(',', ': ')) config_file.close() if not self.keyring_name in gk.list_keyring_names_sync(): gk.create_sync(self.keyring_name, self.__keyring_password) self.Unlock()
def __init__(self): keyrings = gnomekeyring.list_keyring_names_sync() if KEYRING_NAME not in keyrings: gnomekeyring.create_sync(KEYRING_NAME, KEYRING_PASSWD) keyring = gnomekeyring.list_item_ids_sync(KEYRING_NAME) for user in CONFIG['accounts']: user = user.encode('utf8') found = False for item in keyring: gnomekeyring.unlock_sync(KEYRING_NAME, KEYRING_PASSWD) info = gnomekeyring.item_get_info_sync(KEYRING_NAME, item) gnomekeyring.lock_sync(KEYRING_NAME) if user == info.get_display_name(): found = True break if not found: self.save_user_credentials(user)
def create_app(self): if self._app_name not in gk.list_keyring_names_sync(): gk.create_sync(self._app_name, self._app_pass)
# from the docs: "update_if_exists: If true, then another item matching # the type, and attributes will be updated instead of creating a new item." # So we either store unique attributes with each, or set update_if_exists # to False. # If we do both, additional keys with the same name and different IDs # will get created. gnomekeyring.item_create_sync(keyring, gnomekeyring.ITEM_GENERIC_SECRET, item_name, {'name': item_name}, secret, True) if __name__ == '__main__': args = parse_args() '''Sometimes the 'login' keyring is not automatically marked as default (the user needs to do it manually). If no keyring is marked as default, fall back to the 'login' keyring.''' default_keyring = gnomekeyring.get_default_keyring_sync() or 'login' keyring_pass = get_secret(default_keyring, args.keyring) if not keyring_pass: keyring_pass = getpass.getpass( 'New password for keyring ' + args.keyring + ': ') gnomekeyring.create_sync(args.keyring, keyring_pass) store_secret(default_keyring, args.keyring, keyring_pass) gnomekeyring.unlock_sync(args.keyring, keyring_pass) secret = get_secret(args.keyring, args.item_name) if not secret: secret = getpass.getpass('New password for ' + args.item_name + ': ') store_secret(args.keyring, args.item_name, secret) print(secret)
import gnomekeyring as gk keyring = 'gk' except ImportError: try: import PyKDE4.kdeui.KWallet keyring = 'kk' except ImportError: import PyMacAdmin.Security.Keychain keyring = 'xk' import glib import objects glib.set_application_name('gk_text') if 'pycle' not in gk.list_keyring_names_sync(): gk.create_sync('pycle', 'pyclepass') class Connection(object): """A database connection""" def __init__(self, username, password, **params): if 'key' in params: self.key = params['key'] else: self.key = None if 'sid' in params: self.sid = params['sid'] self.name = username + '@' + self.sid elif 'host' in params: self.host = params['host'] self.name = username + '@' + self.host
def __init__(self, keyring = "truecrypt"): self.keyring = keyring if not keyring in gnomekeyring.list_keyring_names_sync(): password = getText() gnomekeyring.create_sync(keyring, password)
def create_Keyring(self, password = None): try : G.create_sync(self.appletName, to_unicode(password)) except Exception, err : print "[ In GnomeKeyring.create_Keyring() ]: ", err
def create_keyring(self,keyring,password): """ Create a new keyring with given name and password """ gk.create_sync(keyring,password)
def __init__(self): global keyring self.username = None self.password = None self.multiline = False self.separator = False self.gconf = gconf.client_get_default() self.appname = "gtracker" self.username_key = "/apps/gtracker/username" self.password_key = "/apps/gtracker/password" self.interval_key = "/apps/gtracker/interval" self.multi_key = "/apps/gtracker/multiline" self.sep_key = "/apps/gtracker/separator" self.interval = self.gconf.get_int(self.interval_key) if self.interval < 1: self.interval = 15 self.gconf.set_int(self.interval_key, self.interval) self.multiline = self.gconf.get_bool(self.multi_key) if self.multiline == None: self.multiline = True self.gconf.set_bool(self.multi_key, self.multiline) self.separator = self.gconf.get_bool(self.sep_key) if self.separator == None: self.separator = True self.gconf.set_bool(self.sep_key, self.separator) if keyring: try: glib.set_application_name(self.appname) except: pass if gk.is_available(): names = gk.list_keyring_names_sync() if not self.appname in names: gk.create_sync(self.appname, "") keys = gk.list_item_ids_sync(self.appname) if len(keys) == 1: gk.unlock_sync(self.appname, "") info = gk.item_get_info_sync(self.appname, keys[0]) self.username = info.get_display_name() self.password = info.get_secret() else: self.username = "" self.password = "" else: keyring = False if not keyring: self.username = self.gconf.get_string(self.username_key) if self.username == None or len(self.username) < 1: self.username = "" self.gconf.set_string(self.username_key, "") self.password = self.gconf.get_string(self.password_key) if self.password == None or len(self.password) < 1: self.password = "" self.gconf.set_string(self.password_key, self.password) else: self.gconf.unset(self.username_key) self.gconf.unset(self.password_key)
def __init__(self): global keyring self.username = None self.password = None self.multiline = False self.separator = False self.gconf = gconf.client_get_default() self.appname = "gtracker" self.username_key = "/apps/gtracker/username" self.password_key = "/apps/gtracker/password" self.interval_key = "/apps/gtracker/interval" self.multi_key = "/apps/gtracker/multiline" self.sep_key = "/apps/gtracker/separator" self.interval = self.gconf.get_int(self.interval_key) if self.interval<1: self.interval = 15 self.gconf.set_int(self.interval_key,self.interval) self.multiline = self.gconf.get_bool(self.multi_key) if self.multiline==None: self.multiline = True self.gconf.set_bool(self.multi_key,self.multiline) self.separator = self.gconf.get_bool(self.sep_key) if self.separator==None: self.separator = True self.gconf.set_bool(self.sep_key,self.separator) if keyring: try: glib.set_application_name(self.appname) except: pass if gk.is_available(): names = gk.list_keyring_names_sync() if not self.appname in names: gk.create_sync(self.appname,"") keys = gk.list_item_ids_sync(self.appname) if len(keys)==1: gk.unlock_sync(self.appname,"") info = gk.item_get_info_sync(self.appname,keys[0]) self.username = info.get_display_name() self.password = info.get_secret() else: self.username = "" self.password = "" else: keyring = False if not keyring: self.username = self.gconf.get_string(self.username_key) if self.username==None or len(self.username)<1: self.username = "" self.gconf.set_string(self.username_key,"") self.password = self.gconf.get_string(self.password_key) if self.password==None or len(self.password)<1: self.password = "" self.gconf.set_string(self.password_key,self.password) else: self.gconf.unset(self.username_key) self.gconf.unset(self.password_key)