--- /dev/null
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2018 Michael Rasmussen <mir@datanom.net>
+
+# This file is part of SecureMail.
+
+# SecureMail 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 3 of the License, or
+# (at your option) any later version.
+#
+# SecureMail 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 SecureMail. If not, see <https://www.gnu.org/licenses/>.
+
+try:
+ import cPickle as pickle
+except:
+ import pickle
+from db import DBInterface as DBI
+from cryptonize import Cryptonize
+
+class NoSuchUserException(Exception):
+ pass
+
+class User:
+ """
+ Class implementing the backend users
+ """
+ def __init__(self, key=None):
+ if key is not None:
+ self.load(key)
+
+ def store(self, key):
+ crypto = Cryptonize()
+ cipher = crypto.symmetric_encrypt(key, pickle.dumps(self.__dict__))
+ DBI.store_user(crypto.generate_hash(key), cipher)
+
+ def load(self, key):
+ crypto = Cryptonize()
+ cipher = DBI.load_user(crypto.generate_hash(key))
+ if cipher is None:
+ raise NoSuchUserException('{0}: User not found'.format(key))
+ plain = crypto.symmetric_decrypt(key, cipher)
+ try:
+ obj = pickle.loads(plain)
+ self.__dict__.update(obj)
+ except pickle.UnpicklingError as e:
+ raise NoSuchUserException(e)
+
+ @property
+ def name(self):
+ return self._name
+
+ @name.setter
+ def name(self, name):
+ self._name = name
+
+ @property
+ def email(self):
+ return self.email
+
+ @email.setter
+ def email(self, email):
+ self._email = email
+
+
+if __name__ == '__main__':
+ try:
+ u = User('test')
+ for attr, value in u.__dict__.items():
+ print ('{0}: {1}'.format(attr, value))
+ c = Cryptonize()
+ key = 'æselØre' #c.get_random_key()
+ cipher = c.symmetric_encrypt(key, pickle.dumps(u))
+ obj = pickle.loads(c.symmetric_decrypt(key, cipher))
+ for attr, value in obj.__dict__.items():
+ print ('{0}: {1}'.format(attr, value))
+ except NoSuchUserException:
+ u = User()
+ u.name = 'testname'
+ u.email = 'testname@securemail.icu'
+ u.store('test')
+ except Exception as e:
+ print (e)