[
120
]
There are two new exceptions in here; they both take usernames, so we'll define
them as subclasses of
AuthException
:
class NotLoggedInError(AuthException):
pass
class NotPermittedError(AuthException):
pass
Finally, we can add a default
authorizor
to go with our default authenticator:
authorizor = Authorizor(authenticator)
That completes a basic authentication/authorization system. We can test the system
at the Python prompt, checking to see whether a user,
joe
, is permitted to do tasks in
the paint department:
>>> import auth
>>> auth.authenticator.add_user("joe", "joepassword")
>>> auth.authorizor.add_permission("paint")
>>> auth.authorizor.check_permission("paint", "joe")
Traceback (most recent call last):
File "", line 1, in
File "auth.py", line 109, in check_permission
raise NotLoggedInError(username)
auth.NotLoggedInError: joe
>>> auth.authenticator.is_logged_in("joe")
False
>>> auth.authenticator.login("joe", "joepassword")
True
>>> auth.authorizor.check_permission("paint", "joe")
Traceback (most recent call last):
File "", line 1, in
File "auth.py", line 116, in check_permission
raise NotPermittedError(username)
auth.NotPermittedError: joe
>>> auth.authorizor.check_permission("mix", "joe")
Traceback (most recent call last):
File "auth.py", line 111, in check_permission
perm_set = self.permissions[perm_name]
www.it-ebooks.info
Chapter 4
[
121
]
Do'stlaringiz bilan baham: |