Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. import hashlib
  2.  
  3. class User:
  4.     def __init__(self, username, password):
  5.         self.username = username
  6.         self.password = self._encrypt_password(password)
  7.         self.is_logged = False
  8.  
  9.     def _encrypt_password(self, password):
  10.         hash_text = (self.username + password).encode('utf-8')
  11.         return hashlib.sha256(hash_text).hexdigest()
  12.  
  13.     def check_password(self, password):
  14.         return self.password == self._encrypt_password(password)
  15.  
  16. class AuthenticException(Exception): pass
  17. class PermissionError(Exception): pass
  18. class IncorrectUsername(AuthenticException): pass
  19. class IncorrectPassword(AuthenticException): pass
  20. class PasswordToShort(AuthenticException): pass
  21. class UsernameAlreadyExists(AuthenticException): pass
  22. class NotPermittedError(AuthenticException): pass
  23. class NotLoggedError(AuthenticException): pass
  24.  
  25.  
  26. class Authicator:
  27.     def __init__(self):
  28.         self.users = {}
  29.  
  30.     def add_user(self, username, password):
  31.  
  32.         if username is self.users:
  33.             raise UsernameAlreadyExists('Podany użytkownik już istnieje')
  34.  
  35.         if len(password < 0):
  36.             raise PasswordToShort('Hasło musi zawierać conajmniej 8 znaków')
  37.  
  38.     def login(self, username, password):
  39.         try:
  40.             user = self.users[username]
  41.         except KeyError:
  42.             raise IncorrectUsername('Niepoprawna nazwa użytkownika')
  43.  
  44.     def isLogged(self, username):
  45.         if username in self.users:
  46.             return self.users[username].is_logged
  47.         else:
  48.             return False
  49.  
  50. class Authorizor:
  51.  
  52.     def __init__(self, authenticator):
  53.         self.permissions = []
  54.         self.authenticator = authenticator
  55.  
  56.     def add_persmission(self, perm):
  57.         try:
  58.             self.permissions[perm]
  59.         except KeyError:
  60.             self.permissions[perm] - set()
  61.  
  62.         else:
  63.             raise PermissionError('Takie uprawnienie juz istnieje')
  64.  
  65.  
  66.     def permit_user(self, username, perm):
  67.         try:
  68.             perm_set = self.permissions[perm]
  69.         except KeyError:
  70.             raise PermissionError('Takie uprawnienie nie istnieje')
  71.         else:
  72.             if username not in self.authenticator.users:
  73.                 raise IncorrectUsername('Niepoprawna nazwa uzytkownika')
  74.             else:
  75.                 perm_set.add(username)
  76.  
  77.     def check_permission(self, username, perm):
  78.         if not self.authenticator.is_logged(username):
  79.             raise NotLoggedError('Uzytkownik nie jest zalogowany')
  80.  
  81.         try:
  82.             perm_set =  self.permissions[perm]
  83.         except KeyError:
  84.             raise PermissionError('Takie uprawnienie nie istnieje')
  85.         else:
  86.             return True
  87.  
  88.  
  89.  
  90. authenticator = Authenticator()
  91. authorizor = Authorizor(Authicator)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement