-
Notifications
You must be signed in to change notification settings - Fork 0
/
ez_user.py
81 lines (70 loc) · 2.85 KB
/
ez_user.py
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
# encoding=utf-8 ==============================================================#
# ez_user #
#==============================================================================#
#============#
# Includes #
#============#
from Crypto.Hash import SHA # Shorter IDs than with 256
import ez_crypto as ec
import ez_database as ed
#==============================================================================#
# class User #
#==============================================================================#
class User(object):
"""
- ask for username, pw
- create db entry
- create private/public key pair
- store public key public
- db-entry for groups, network
- db-entry for with all msg-ids as recipient
"""
# TODO: (bcn 2014-08-01) will we remove IP again ?
components = ['UID', 'name', 'public_key', 'IP', 'last_IPs']
def __init__(self, name='', current_ip='', public_key=None, _dict=None):
"""
This method will create a new user of the network.
IP is a string of the form 123.123.123.123:12345.
"""
if _dict:
for x in User.components:
setattr(self, x, _dict[x])
else:
self.name = name
if public_key:
self.public_key = public_key
else:
er = ec.eZ_RSA()
self.public_key = er.generate_keys(self.name)
self.UID = SHA.new(self.name + self.public_key).hexdigest()
self.IP = current_ip
# The database doesn't like lists. We can join them with some char though
self.last_IPs = ' '
def current_ip_and_port(self):
lst = self.IP.split(':')
return (lst[0], lst[1])
#==============================================================================#
# class UserDatabase #
#==============================================================================#
class UserDatabase(ed.Database):
"""
The UserDatabase class gives access to the saved information about users in
the SQL database
"""
def __init__(self, **kwargs):
"""
Opens a local sqlite database in the default location default_db,
which is saved to and loaded from disk automatically. To create a merely
temporary database in memory use 'sqlite:///:memory:'
"""
ed.Database.__init__(self, 'Users', User, **kwargs)
def check_for_ip(self, ip, port):
"""
Given IP and port, check whether the user is in DB is in DB and return UID.
"""
user = self.get_entry(IP=':'.join([ip, port]))
return user.UID
#==============================================================================#
# Global Instance #
#==============================================================================#
user_database = UserDatabase()