-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.py
66 lines (52 loc) · 1.59 KB
/
encoding.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
import aestools
from collections import OrderedDict
USERS = {}
def parse(querystring):
output = OrderedDict()
pairs = querystring.split('&')
for pair in pairs:
key, value = pair.split('=', 1)
output[key] = value
return output
def encode(obj):
output = ''
for key, value in obj.items():
output += key
output += '='
output += value
output += '&'
# chop off last &
return output[0:-1]
def is_admin(cookie):
decrypted = parse_encrypted(cookie)
return decrypted.endswith(b'role=admin')
def remove_symbols(email):
return email.replace('&', "%26").replace('=', '%3D')
def profile(email, role):
email = remove_symbols(email)
return encode(create_or_get_user(email, role))
def profile_for(email, role='user'):
encoded_profile = profile(email, role)
return aestools.encrypt_ecb(bytes(encoded_profile, 'UTF-8'), KEY, True)
def parse_encrypted(bytes):
return aestools.decrypt_ecb(bytes, KEY, True)
def create_or_get_user(email, role='user'):
if email not in USERS:
USERS[email] = User(email, role)
return USERS[email]
KEY = aestools.random_key(16)
class User():
nextid = 0
def getNextId(self):
User.nextid += 1
return User.nextid
def __init__(self, email, role='user'):
self.email = email
self.role = role
self.id = self.getNextId()
def items(self):
ordereddict = OrderedDict()
ordereddict['email'] = self.email
ordereddict['id'] = str(self.id)
ordereddict['role'] = self.role
return ordereddict.items()