This repository has been archived by the owner on Oct 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
person.py
162 lines (131 loc) · 4.76 KB
/
person.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
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
import datetime
import hashlib
import pickle
import random
import re
from string import printable
LEGAL_USR_ID = """User id requirements
- a minimal length of 1
- a maximum length of 16
- characters can only be
· a digit
· a letter
· a underscore
"""
USR_ID_PATTERN = r"^[\d\w_]{1,16}$"
LEGAL_PSW = """Password requirements for your data security
- a minimal length of 8
- a maximum length of 100
- at least one of each of
· digits
· lower case letters
· upper case letters
"""
PSW_PATTERN = r"^[\d]*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$"
PERSON_SAVE = "save/person.p"
class Person:
loyal_limit = 24
single = None
def __init__(self, usr_id, password):
self.usr_id = usr_id
self.password = password
self.hash = None
self.cards = []
self.credits = 0
self.__class__.single = self
def generate_hash(self):
salt = "energymax"
pepper = random.choice(printable)
code = self.password + salt + pepper # add salt and pepper for better encryption
code = code.encode("utf-8")
self.hash = hashlib.sha256(code).hexdigest()
def register(self):
# check usr_id and password syntax, return error message if illegal
if not re.match(USR_ID_PATTERN, self.usr_id):
return LEGAL_USR_ID
if not re.match(PSW_PATTERN, self.password):
return LEGAL_PSW
# check if user name is registered
try:
usr_map = pickle.load(open(PERSON_SAVE, 'br'))
except (FileNotFoundError, EOFError):
usr_map = {}
if self.usr_id in usr_map:
return "User name is occupied"
# generate hash
self.generate_hash()
# save hash
usr_map[self.usr_id] = self.hash, self.cards, self.credits
pickle.dump(usr_map, open(PERSON_SAVE, 'bw'))
def login(self):
# load from file
try:
usr_map = pickle.load(open(PERSON_SAVE, 'br'))
except (FileNotFoundError, EOFError):
return "No user named " + self.usr_id + " found"
usr_info = usr_map.get(self.usr_id, None)
# check user id
if not self.usr_id:
return "User name cannot be empty"
if not usr_info:
return "No user named " + self.usr_id + " found"
else:
self.hash, self.cards, self.credits = usr_info
# check password
salt = "energymax"
for pepper in printable:
code = self.password + salt + pepper
code = code.encode("utf-8")
if self.hash == hashlib.sha256(code).hexdigest():
return None
return "Incorrect password"
def change_psw(self, ori_psw, new_psw):
# check password
if not ori_psw == self.password:
return "The original password is not correct."
# check new password syntax
if new_psw == self.password:
return "You need to change to a different password."
if not re.match(PSW_PATTERN, self.password):
return LEGAL_PSW
# generate hash
self.password = new_psw
self.generate_hash()
# save new password
usr_map = pickle.load(open(PERSON_SAVE, 'br'))
h, self.cards, p = usr_map[self.usr_id]
usr_map[self.usr_id] = self.hash, self.cards, self.credits
pickle.dump(usr_map, open(PERSON_SAVE, 'bw'))
def add_card(self, number, exp_date):
# three card maximum
if len(self.cards) >= 3:
return "You can not bind more than three bank cards.\n" \
"Remove a existing card and retry."
# load existing cards
usr_map = pickle.load(open(PERSON_SAVE, 'br'))
# check if card already exists
for h, cards, p in usr_map.values():
if number in [c[0] for c in cards]:
return "Card already exists."
# save to file
self.cards.append((number, exp_date))
usr_map[self.usr_id] = self.hash, self.cards, self.credits
pickle.dump(usr_map, open(PERSON_SAVE, 'bw'))
def remove_card(self, index):
# save to self and file
del self.cards[index]
usr_map = pickle.load(open(PERSON_SAVE, 'br'))
usr_map[self.usr_id] = self.hash, self.cards, self.credits
pickle.dump(usr_map, open(PERSON_SAVE, 'bw'))
def earn_credits(self, points):
# add points to account credits and save
self.credits += points
usr_map = pickle.load(open(PERSON_SAVE, 'br'))
usr_map[self.usr_id] = self.hash, self.cards, self.credits
pickle.dump(usr_map, open(PERSON_SAVE, 'bw'))
@property
def loyalty(self):
loyalty = Person.loyal_limit - self.credits
if loyalty < 0:
loyalty = 0
return loyalty