]> git.datanom.net - securemail.git/blame - user.py
Basic framework finished
[securemail.git] / user.py
CommitLineData
8c4f590c
MR
1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2018 Michael Rasmussen <mir@datanom.net>
4
5# This file is part of SecureMail.
6
7# SecureMail is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# SecureMail is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with SecureMail. If not, see <https://www.gnu.org/licenses/>.
19
20try:
21 import cPickle as pickle
22except:
23 import pickle
24from db import DBInterface as DBI
25from cryptonize import Cryptonize
26
27class NoSuchUserException(Exception):
28 pass
29
30class User:
31 """
32 Class implementing the backend users
33 """
34 def __init__(self, key=None):
35 if key is not None:
36 self.load(key)
37
38 def store(self, key):
39 crypto = Cryptonize()
40 cipher = crypto.symmetric_encrypt(key, pickle.dumps(self.__dict__))
41 DBI.store_user(crypto.generate_hash(key), cipher)
42
43 def load(self, key):
44 crypto = Cryptonize()
45 cipher = DBI.load_user(crypto.generate_hash(key))
46 if cipher is None:
47 raise NoSuchUserException('{0}: User not found'.format(key))
48 plain = crypto.symmetric_decrypt(key, cipher)
49 try:
50 obj = pickle.loads(plain)
51 self.__dict__.update(obj)
52 except pickle.UnpicklingError as e:
53 raise NoSuchUserException(e)
54
55 @property
56 def name(self):
57 return self._name
58
59 @name.setter
60 def name(self, name):
61 self._name = name
62
63 @property
64 def email(self):
65 return self.email
66
67 @email.setter
68 def email(self, email):
69 self._email = email
70
71
72if __name__ == '__main__':
73 try:
74 u = User('test')
75 for attr, value in u.__dict__.items():
76 print ('{0}: {1}'.format(attr, value))
77 c = Cryptonize()
78 key = 'æselØre' #c.get_random_key()
79 cipher = c.symmetric_encrypt(key, pickle.dumps(u))
80 obj = pickle.loads(c.symmetric_decrypt(key, cipher))
81 for attr, value in obj.__dict__.items():
82 print ('{0}: {1}'.format(attr, value))
83 except NoSuchUserException:
84 u = User()
85 u.name = 'testname'
86 u.email = 'testname@securemail.icu'
87 u.store('test')
88 except Exception as e:
89 print (e)
This page took 0.064253 seconds and 5 git commands to generate.