53 lines
1.1 KiB
Python
53 lines
1.1 KiB
Python
"""Authentication service"""
|
|||
|
|
import bcrypt
|
||
|
|
from app.models.user import User
|
||
|
|
|
||
|
|
|
||
|
|
def verify_credentials(username, password):
|
||
|
|
"""Verify username and password
|
||
|
|
|
||
|
|
Args:
|
||
|
|
username: Username to check
|
||
|
|
password: Plain text password to verify
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
User or None: User object if credentials valid, None otherwise
|
||
|
|
"""
|
||
|
|
if not username or not password:
|
||
|
|
return None
|
||
|
|
|
||
|
|
user = User.get_by_username(username)
|
||
|
|
|
||
|
|
if not user or not user.is_active:
|
||
|
|
return None
|
||
|
|
|
||
|
|
if user.check_password(password):
|
||
|
|
return user
|
||
|
|
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def hash_password(password):
|
||
|
|
"""Hash password using bcrypt
|
||
|
|
|
||
|
|
Args:
|
||
|
|
password: Plain text password
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
str: Hashed password
|
||
|
|
"""
|
||
|
|
return User.hash_password(password)
|
||
|
|
|
||
|
|
|
||
|
|
def check_password(password, password_hash):
|
||
|
|
"""Check password against hash
|
||
|
|
|
||
|
|
Args:
|
||
|
|
password: Plain text password
|
||
|
|
password_hash: Bcrypt hash to check against
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
bool: True if password matches, False otherwise
|
||
|
|
"""
|
||
|
|
return bcrypt.checkpw(password.encode('utf-8'), password_hash.encode('utf-8'))
|