-
Notifications
You must be signed in to change notification settings - Fork 0
/
authpy.py
134 lines (92 loc) · 3.26 KB
/
authpy.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
import hashlib
class User(object):
def __init__(self,username,password):
self.username=username
self.password=self._encrypt_pw(password)
self.is_logged_in=False
def _encrypt_pw(self, password):
hash_string=(self.username+password)
hash_string=hash_string.encode("utf8")
return hashlib.sha512(hash_string).hexdigest()
def check_password(self, password):
encrypted=self._encrypt_pw(password)
return encrypted==self.password
class AuthException(Exception):
def __init__(self,username,user=None):
super().__init__(username,user)
self.username=username
self.user=user
class usernameAlreadyExist(AuthException):
pass
class PasswordTooShort(AuthException):
pass
class Authenticator(object):
def __init__(self):
self.users={}
def add_user(self,username,password):
if username in self.users:
raise usernameAlreadyExist(username)
if len(password)<6:
raise PasswordTooShort(username)
self.users[username]=User(username,password)
class InvalidUsername(AuthException):
pass
class InvalidPassword(AuthException):
pass
def login(self,username,password):
try:
user=self.users[username]
except KeyError:
raise InvalidUsername(username)
if not user.check_password(password):
raise InvalidPassword(username,user)
user.is_logged_in=True
return True
def is_logged_in(self,username):
if username in self.users:
return self.users[username].is_logged_in
return False
authenticator=Authenticator()
class Authorizor(object):
def __init__(self,authenticator):
self.authenticator=authenticator
self.permissions={}
def add_permission(self,permission_name):
try:
permission_set=self.permissions[permission_name]
except KeyError:
self.permissions[permission_name]=set()
else:
raise PermissionError("Permission Exists")
def permit_user(self,permission_name,username):
try:
permission_set=self.permissions[permission_name]
except KeyError:
raise PermissionError("Permission is not exist.")
else:
if username not in self.authenticator.users:
raise InvalidUsername(username)
permission_set.add(username)
class PermissionError(Exception):
pass
def check_permission(self,permission_name,username):
if not self.authenticator.is_logged_in(username):
raise NotLoggedInError(username)
try:
permission_set=self.permissions[permission_name]
except KeyError:
raise PermissionError("Permission is not exist.")
else:
if username not in permission_set:
raise NotPermittedError(username)
else:
return True
class NotLoggedInError(AuthException):
pass
class NotPermittedError(AuthException):
pass
authorizor=Authorizor(authenticator)
import auth
auth.authenticator.add_user("joe", "joepassword")
auth.authorizor.add_permission("paint")
auth.authorizor.check_permission("paint", "joe")