From 1f891be3c7f1b935f911c444386fe4603ff54f24 Mon Sep 17 00:00:00 2001 From: lone-wolve Date: Sat, 25 Feb 2023 19:00:46 +0000 Subject: [PATCH 1/2] Pydantic model for users --- Backend/schema/users.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Backend/schema/users.py diff --git a/Backend/schema/users.py b/Backend/schema/users.py new file mode 100644 index 00000000..ecad255e --- /dev/null +++ b/Backend/schema/users.py @@ -0,0 +1,10 @@ +from pydantic import BaseModel +from typing import Optional + + +class CreateUser(BaseModel): + username:str + email: Optional [str] + first_name: str + last_name: str + password: str \ No newline at end of file From ba29381548ea50ba51da8a9c0066ce2155e16548 Mon Sep 17 00:00:00 2001 From: lone-wolve Date: Sat, 25 Feb 2023 19:04:01 +0000 Subject: [PATCH 2/2] user authentication routes file --- Backend/routes/auth.py | 76 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Backend/routes/auth.py diff --git a/Backend/routes/auth.py b/Backend/routes/auth.py new file mode 100644 index 00000000..5dbacf3d --- /dev/null +++ b/Backend/routes/auth.py @@ -0,0 +1,76 @@ +#!/usr/bin/python3 +""" authentication """ + +from fastapi import APIRouter, Depends, HTTPException +from schema.users import CreateUser +from model import Reconcile +from passlib.context import CryptContext +from Config.db import engine, SessionLocal +from sqlalchemy.orm import Session +from model import Reconcile +from fastapi.security import OAuth2PasswordRequestForm + + + + +bcrypt_context = CryptContext(schemes=["bcrypt"], deprecated="auto" ) + +router = APIRouter() + +Reconcile.Base.metadata.create_all(bind=engine) +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + + +def get_password_hash(password): + + return bcrypt_context.hash(password) + + +def verify_password(plain_password, hashed_password): + + return bcrypt_context.verify(plain_password, hashed_password) + +def authenticate_user(username:str, password: str, db): + + user = db.query(Reconcile.Users)\ + .filter(Reconcile.Users.username == username)\ + .first() + + if not user : + return False + if not verify_password(password, user.hased_password): + return False + + return user + + +@router.post("/create/user") +def create_new_user(create_user:CreateUser, db:Session = Depends(get_db)): + create_user_model = Reconcile.Users() + create_user_model.email = create_user.email + create_user_model.username = create_user.username + create_user_model.first_name = create_user.first_name + create_user_model.last_name = create_user.last_name + hash_password = get_password_hash(create_user.password) + create_user_model.hased_password = hash_password + create_user_model.is_active = True + + + db.add(create_user_model) + db.commit() + + +@router.post("/token") +def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)): + + user = authenticate_user(form_data.username, form_data.password, db) + + if not user: + raise HTTPException(status_code=404, detail= "User not valid") + + return "User Valid"