2016-04-03 20:03:58 +00:00
|
|
|
import hashlib
|
|
|
|
import random
|
|
|
|
from szurubooru import config
|
|
|
|
from szurubooru import errors
|
|
|
|
|
|
|
|
def get_password_hash(salt, password):
|
|
|
|
''' Retrieve new-style password hash. '''
|
|
|
|
digest = hashlib.sha256()
|
2016-04-06 18:38:45 +00:00
|
|
|
digest.update(config.config['secret'].encode('utf8'))
|
2016-04-03 20:03:58 +00:00
|
|
|
digest.update(salt.encode('utf8'))
|
|
|
|
digest.update(password.encode('utf8'))
|
|
|
|
return digest.hexdigest()
|
|
|
|
|
|
|
|
def get_legacy_password_hash(salt, password):
|
|
|
|
''' Retrieve old-style password hash. '''
|
|
|
|
digest = hashlib.sha1()
|
|
|
|
digest.update(b'1A2/$_4xVa')
|
|
|
|
digest.update(salt.encode('utf8'))
|
|
|
|
digest.update(password.encode('utf8'))
|
|
|
|
return digest.hexdigest()
|
|
|
|
|
|
|
|
def create_password():
|
|
|
|
alphabet = {
|
|
|
|
'c': list('bcdfghijklmnpqrstvwxyz'),
|
|
|
|
'v': list('aeiou'),
|
|
|
|
'n': list('0123456789'),
|
|
|
|
}
|
|
|
|
pattern = 'cvcvnncvcv'
|
|
|
|
return ''.join(random.choice(alphabet[l]) for l in list(pattern))
|
|
|
|
|
|
|
|
def is_valid_password(user, password):
|
|
|
|
salt, valid_hash = user.password_salt, user.password_hash
|
|
|
|
possible_hashes = [
|
|
|
|
get_password_hash(salt, password),
|
|
|
|
get_legacy_password_hash(salt, password)
|
|
|
|
]
|
|
|
|
return valid_hash in possible_hashes
|
|
|
|
|
2016-04-29 11:08:41 +00:00
|
|
|
def has_privilege(user, privilege_name):
|
2016-04-06 18:38:45 +00:00
|
|
|
all_ranks = config.config['ranks']
|
2016-04-03 20:03:58 +00:00
|
|
|
assert privilege_name in config.config['privileges']
|
2016-04-06 17:16:44 +00:00
|
|
|
assert user.rank in all_ranks
|
2016-04-03 20:03:58 +00:00
|
|
|
minimal_rank = config.config['privileges'][privilege_name]
|
|
|
|
good_ranks = all_ranks[all_ranks.index(minimal_rank):]
|
2016-04-29 11:08:41 +00:00
|
|
|
return user.rank in good_ranks
|
|
|
|
|
|
|
|
def verify_privilege(user, privilege_name):
|
|
|
|
if not has_privilege(user, privilege_name):
|
2016-04-03 20:03:58 +00:00
|
|
|
raise errors.AuthError('Insufficient privileges to do this.')
|
|
|
|
|
|
|
|
def generate_authentication_token(user):
|
|
|
|
''' Generate nonguessable challenge (e.g. links in password reminder). '''
|
2016-04-06 15:56:34 +00:00
|
|
|
digest = hashlib.md5()
|
2016-04-06 18:38:45 +00:00
|
|
|
digest.update(config.config['secret'].encode('utf8'))
|
2016-04-03 20:03:58 +00:00
|
|
|
digest.update(user.password_salt.encode('utf8'))
|
|
|
|
return digest.hexdigest()
|