Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- class User:
- def __init__(self, username, password):
- self.username = username
- self.password = self._encrypt_password(password)
- self.is_logged = False
- def _encrypt_password(self, password):
- hash_text = (self.username + password).encode('utf-8')
- return hashlib.sha256(hash_text).hexdigest()
- def check_password(self, password):
- return self.password == self._encrypt_password(password)
- class AuthenticException(Exception): pass
- class PermissionError(Exception): pass
- class IncorrectUsername(AuthenticException): pass
- class IncorrectPassword(AuthenticException): pass
- class PasswordToShort(AuthenticException): pass
- class UsernameAlreadyExists(AuthenticException): pass
- class NotPermittedError(AuthenticException): pass
- class NotLoggedError(AuthenticException): pass
- class Authicator:
- def __init__(self):
- self.users = {}
- def add_user(self, username, password):
- if username is self.users:
- raise UsernameAlreadyExists('Podany użytkownik już istnieje')
- if len(password < 0):
- raise PasswordToShort('Hasło musi zawierać conajmniej 8 znaków')
- def login(self, username, password):
- try:
- user = self.users[username]
- except KeyError:
- raise IncorrectUsername('Niepoprawna nazwa użytkownika')
- def isLogged(self, username):
- if username in self.users:
- return self.users[username].is_logged
- else:
- return False
- class Authorizor:
- def __init__(self, authenticator):
- self.permissions = []
- self.authenticator = authenticator
- def add_persmission(self, perm):
- try:
- self.permissions[perm]
- except KeyError:
- self.permissions[perm] - set()
- else:
- raise PermissionError('Takie uprawnienie juz istnieje')
- def permit_user(self, username, perm):
- try:
- perm_set = self.permissions[perm]
- except KeyError:
- raise PermissionError('Takie uprawnienie nie istnieje')
- else:
- if username not in self.authenticator.users:
- raise IncorrectUsername('Niepoprawna nazwa uzytkownika')
- else:
- perm_set.add(username)
- def check_permission(self, username, perm):
- if not self.authenticator.is_logged(username):
- raise NotLoggedError('Uzytkownik nie jest zalogowany')
- try:
- perm_set = self.permissions[perm]
- except KeyError:
- raise PermissionError('Takie uprawnienie nie istnieje')
- else:
- return True
- authenticator = Authenticator()
- authorizor = Authorizor(Authicator)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement