sql_app: implement password hashing when create_user is used.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-03-16 22:46:50 +03:00
parent 75984d1f1f
commit ab30844d95
Signed by: Hesham
GPG Key ID: 74876157D199B09E
2 changed files with 11 additions and 7 deletions

View File

@ -2,7 +2,7 @@
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from . import models, schemas from . import models, schemas, crypto
def get_user(db: Session, user_id: int): def get_user(db: Session, user_id: int):
@ -18,8 +18,11 @@ def get_users(db: Session, skip: int = 0, limit: int = 100):
def create_user(db: Session, user: schemas.UserCreate): def create_user(db: Session, user: schemas.UserCreate):
fake_hashed_password = user.password + "notreallyhashed" key = crypto.gen_new_key(user.password)
db_user = models.User(email=user.email, hashed_password=fake_hashed_password) salt = key[1]
hashed_pass = key[0]
# TODO: check if user already exists? based on name,email ...
db_user = models.User(email=user.email, hashed_password=hashed_pass, passwd_salt=salt)
db.add(db_user) db.add(db_user)
db.commit() db.commit()
db.refresh(db_user) db.refresh(db_user)

View File

@ -13,11 +13,12 @@ def get_new_salt(n_bytes : int = 32) -> bytes:
return os.urandom(n_bytes) return os.urandom(n_bytes)
"""
Generate a new key and salt to store.
Returns (key, salt)
"""
def gen_new_key(plain_passwd : str) -> tuple: def gen_new_key(plain_passwd : str) -> tuple:
"""
Generate a new key and salt to store.
Returns (key, salt)
"""
salt = get_new_salt(32) salt = get_new_salt(32)
return (calc_key(plain_passwd, salt), salt) return (calc_key(plain_passwd, salt), salt)