mirror of https://gitlab.com/litecord/litecord.git
general code cleanup
* remove encoding from .encode()/.decode() calls as 'utf-8' is already the default * code cleanup/compactify
This commit is contained in:
parent
d41641929c
commit
e8b29ddd90
|
|
@ -16,7 +16,7 @@ async def raw_token_check(token, db=None):
|
||||||
user_id, _hmac = token.split('.')
|
user_id, _hmac = token.split('.')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user_id = base64.b64decode(user_id.encode('utf-8'))
|
user_id = base64.b64decode(user_id.encode())
|
||||||
user_id = int(user_id)
|
user_id = int(user_id)
|
||||||
except (ValueError, binascii.Error):
|
except (ValueError, binascii.Error):
|
||||||
raise AuthError('Invalid user ID type')
|
raise AuthError('Invalid user ID type')
|
||||||
|
|
|
||||||
|
|
@ -15,32 +15,31 @@ bp = Blueprint('auth', __name__)
|
||||||
|
|
||||||
async def hash_data(data: str) -> str:
|
async def hash_data(data: str) -> str:
|
||||||
"""Hash information with bcrypt."""
|
"""Hash information with bcrypt."""
|
||||||
data = bytes(data, 'utf-8')
|
buf = data.encode()
|
||||||
|
|
||||||
future = app.loop.run_in_executor(
|
hashed = await app.loop.run_in_executor(
|
||||||
None, bcrypt.hashpw, data, bcrypt.gensalt(14))
|
None, bcrypt.hashpw, buf, bcrypt.gensalt(14)
|
||||||
|
)
|
||||||
|
|
||||||
hashed = await future
|
return hashed.decode()
|
||||||
return hashed.decode('utf-8')
|
|
||||||
|
|
||||||
|
|
||||||
async def check_password(pwd_hash, given_password) -> bool:
|
async def check_password(pwd_hash: str, given_password: str) -> bool:
|
||||||
"""Check if a given password matches the given hash."""
|
"""Check if a given password matches the given hash."""
|
||||||
pwd_hash = pwd_hash.encode('utf-8')
|
pwd_encoded = pwd_hash.encode()
|
||||||
given_password = given_password.encode('utf-8')
|
given_encoded = given_password.encode()
|
||||||
|
|
||||||
future = app.loop.run_in_executor(
|
return await app.loop.run_in_executor(
|
||||||
None, bcrypt.checkpw, given_password, pwd_hash)
|
None, bcrypt.checkpw, given_encoded, pwd_encoded
|
||||||
|
)
|
||||||
return await future
|
|
||||||
|
|
||||||
|
|
||||||
def make_token(user_id, user_pwd_hash) -> str:
|
def make_token(user_id, user_pwd_hash) -> str:
|
||||||
"""Generate a single token for a user."""
|
"""Generate a single token for a user."""
|
||||||
signer = itsdangerous.Signer(user_pwd_hash)
|
signer = itsdangerous.Signer(user_pwd_hash)
|
||||||
user_id = base64.b64encode(str(user_id).encode('utf-8'))
|
user_id = base64.b64encode(str(user_id).encode())
|
||||||
|
|
||||||
return signer.sign(user_id).decode('utf-8')
|
return signer.sign(user_id).decode()
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/register', methods=['POST'])
|
@bp.route('/register', methods=['POST'])
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue