/usr/lib/python2.7/dist-packages/Mailnag/common/credentialstore.py is in mailnag 1.2.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# credentialstore.py
#
# Copyright 2015, 2016 Patrick Ulbrich <zulu99@gmx.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import hashlib
# TODO: Make this class an enum
# when Mailnag is ported to python 3
class CredentialStoreType:
NONE = 'none'
GNOME = 'gnome'
# KDE = 'kde'
_credentialstoretype = CredentialStoreType.NONE
try:
import gi
gi.require_version('GnomeKeyring', '1.0')
from gi.repository import GnomeKeyring
_credentialstoretype = CredentialStoreType.GNOME
except: pass
#
# CredentialStore base class
#
class CredentialStore:
_instance = None
def set(self, key, secret):
pass
def get(self, key):
pass
def remove(self, key):
pass
@staticmethod
def get_default():
if (CredentialStore._instance == None) and (_credentialstoretype != CredentialStoreType.NONE):
CredentialStore._instance = SupportedCredentialStores[_credentialstoretype]()
return CredentialStore._instance
@staticmethod
def from_string(strn):
cs = None
if strn == 'auto':
cs = CredentialStore.get_default()
elif strn in SupportedCredentialStores:
cs = SupportedCredentialStores[strn]()
return cs
#
# GNOME CredentialStore
#
class GnomeCredentialStore(CredentialStore):
def __init__(self):
(result, kr_name) = GnomeKeyring.get_default_keyring_sync()
self._defaultKeyring = kr_name
if self._defaultKeyring == None:
self._defaultKeyring = 'login'
result = GnomeKeyring.unlock_sync(self._defaultKeyring, None)
if result != GnomeKeyring.Result.OK:
raise KeyringUnlockException('Failed to unlock default keyring')
self._migrate_keyring()
def get(self, key):
attrs = self._get_attrs(key)
result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs)
if result == GnomeKeyring.Result.OK:
return items[0].secret
else:
return ''
def set(self, key, secret):
if secret == '':
return
attrs = self._get_attrs(key)
existing_secret = ''
result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs)
if result == GnomeKeyring.Result.OK:
existing_secret = items[0].secret
if existing_secret != secret:
GnomeKeyring.item_create_sync(self._defaultKeyring, \
GnomeKeyring.ItemType.GENERIC_SECRET, key, \
attrs, secret, True)
def remove(self, key):
attrs = self._get_attrs(key)
result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs)
if result == GnomeKeyring.Result.OK:
GnomeKeyring.item_delete_sync(self._defaultKeyring, items[0].item_id)
def _get_attrs(self, key):
attrs = GnomeKeyring.Attribute.list_new()
keyid = hashlib.md5(key.encode('utf-8')).hexdigest()
GnomeKeyring.Attribute.list_append_string(attrs, 'source', 'Mailnag')
GnomeKeyring.Attribute.list_append_string(attrs, 'api-version', '1.1')
GnomeKeyring.Attribute.list_append_string(attrs, 'keyid', keyid)
return attrs
# Migrates pre Mailnag 1.1 keyring items into the new format
def _migrate_keyring(self):
attrs = GnomeKeyring.Attribute.list_new()
GnomeKeyring.Attribute.list_append_string(attrs, 'application', 'Mailnag')
result, items = GnomeKeyring.find_items_sync(GnomeKeyring.ItemType.GENERIC_SECRET, attrs)
if result == GnomeKeyring.Result.OK:
for i in items:
result, info = GnomeKeyring.item_get_info_sync(self._defaultKeyring, i.item_id)
self.set(info.get_display_name(), i.secret)
GnomeKeyring.item_delete_sync(self._defaultKeyring, i.item_id)
#
# Exception thrown if the GNOME keyring can't be unlocked
#
class KeyringUnlockException(Exception):
def __init__(self, message):
Exception.__init__(self, message)
#
# All supported credential stores
#
SupportedCredentialStores = {
CredentialStoreType.GNOME : GnomeCredentialStore
#CredentialStoreType.KDE : KDECredentialStore
}
|