|
1 | | -from sqlalchemy.orm import Session |
2 | | -from . import models |
3 | 1 | import bcrypt |
4 | 2 | import hashlib |
| 3 | +from sqlalchemy.orm import Session |
| 4 | +from . import models |
5 | 5 |
|
6 | 6 |
|
7 | | -def create_user(db: Session, username: str, password: str): |
8 | | - # TODO: Add password strength check, should be long (20+ chars) OR complex (with uppercase, lowercase, numbers and symbols) |
9 | | - |
10 | | - # Hash the password with bcrypt |
11 | | - # bcrypt has a 72-byte limit, so we'll hash long passwords with SHA-256 first |
12 | | - if len(password) > 72: |
13 | | - password = hashlib.sha256(password.encode()).hexdigest() |
14 | | - |
15 | | - # Hash with bcrypt |
16 | | - hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) |
17 | | - |
18 | | - # Create user object |
19 | | - user = models.User(username=username, hashed_password=hashed_password.decode('utf-8')) |
| 7 | +class UserRepository: |
| 8 | + def __init__(self, db: Session): |
| 9 | + self._db = db |
20 | 10 |
|
21 | | - # Add to database |
22 | | - db.add(user) |
23 | | - db.commit() |
24 | | - db.refresh(user) |
25 | | - return user |
| 11 | + def create_user(self, username: str, password: str): |
| 12 | + hashed_password = self._hash_password(password) |
| 13 | + user = models.User(username=username, hashed_password=hashed_password) |
| 14 | + self._db.add(user) |
| 15 | + self._db.commit() |
| 16 | + self._db.refresh(user) |
| 17 | + return user |
26 | 18 |
|
| 19 | + def get_user_by_username(self, username: str): |
| 20 | + return self._db.query(models.User).filter(models.User.username == username).first() |
27 | 21 |
|
28 | | -def get_user_by_username(db: Session, username: str): |
29 | | - return db.query(models.User).filter(models.User.username == username).first() |
| 22 | + def get_user_by_id(self, user_id: int): |
| 23 | + return self._db.query(models.User).filter(models.User.id == user_id).first() |
30 | 24 |
|
| 25 | + def _hash_password(self, password: str) -> str: |
| 26 | + if len(password) > 72: |
| 27 | + password = hashlib.sha256(password.encode()).hexdigest() |
| 28 | + return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') |
31 | 29 |
|
32 | | -def get_user_by_id(db: Session, user_id: int): |
33 | | - return db.query(models.User).filter(models.User.id == user_id).first() |
| 30 | + @staticmethod |
| 31 | + def verify_password(plain_password: str, hashed_password: str) -> bool: |
| 32 | + if len(plain_password) > 72: |
| 33 | + plain_password = hashlib.sha256(plain_password.encode()).hexdigest() |
| 34 | + return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) |
34 | 35 |
|
35 | 36 |
|
36 | 37 | def verify_password(plain_password: str, hashed_password: str) -> bool: |
37 | | - # Handle long passwords by hashing them first |
38 | | - if len(plain_password) > 72: |
39 | | - plain_password = hashlib.sha256(plain_password.encode()).hexdigest() |
40 | | - |
41 | | - # Verify password |
42 | | - return bcrypt.checkpw(plain_password.encode('utf-8'), hashed_password.encode('utf-8')) |
| 38 | + return UserRepository.verify_password(plain_password, hashed_password) |
0 commit comments