2016-03-28 12:14:50 +00:00
|
|
|
''' Exports AuthService. '''
|
|
|
|
|
|
|
|
from szurubooru.services.errors import AuthError
|
2016-03-19 20:37:04 +00:00
|
|
|
|
|
|
|
class AuthService(object):
|
2016-03-28 12:14:50 +00:00
|
|
|
''' Services related to user authentication '''
|
|
|
|
|
2016-03-28 20:31:08 +00:00
|
|
|
def __init__(self, config, password_service):
|
2016-03-19 20:37:04 +00:00
|
|
|
self._config = config
|
2016-03-28 12:14:50 +00:00
|
|
|
self._password_service = password_service
|
2016-03-19 20:37:04 +00:00
|
|
|
|
2016-03-28 12:14:50 +00:00
|
|
|
def is_valid_password(self, user, password):
|
|
|
|
''' Returns whether the given password for a given user is valid. '''
|
|
|
|
salt, valid_hash = user.password_salt, user.password_hash
|
|
|
|
possible_hashes = [
|
|
|
|
self._password_service.get_password_hash(salt, password),
|
|
|
|
self._password_service.get_legacy_password_hash(salt, password)
|
|
|
|
]
|
|
|
|
return valid_hash in possible_hashes
|
|
|
|
|
2016-03-19 20:37:04 +00:00
|
|
|
def verify_privilege(self, user, privilege_name):
|
2016-03-28 12:14:50 +00:00
|
|
|
'''
|
|
|
|
Throws an AuthError if the given user doesn't have given privilege.
|
|
|
|
'''
|
2016-03-30 19:23:19 +00:00
|
|
|
all_ranks = self._config['service']['user_ranks']
|
2016-03-19 20:37:04 +00:00
|
|
|
|
|
|
|
assert privilege_name in self._config['privileges']
|
2016-03-28 12:22:25 +00:00
|
|
|
assert user.access_rank in all_ranks
|
2016-03-19 20:37:04 +00:00
|
|
|
minimal_rank = self._config['privileges'][privilege_name]
|
|
|
|
good_ranks = all_ranks[all_ranks.index(minimal_rank):]
|
2016-03-28 12:22:25 +00:00
|
|
|
if user.access_rank not in good_ranks:
|
2016-03-28 12:14:50 +00:00
|
|
|
raise AuthError('Insufficient privileges to do this.')
|