From b53aab012de662a1131de78bee7215496e93ca40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sat, 19 Mar 2022 18:06:17 +0000 Subject: [PATCH] double user creation fail --- api/kiwi_vpn_api/db/schemas.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/api/kiwi_vpn_api/db/schemas.py b/api/kiwi_vpn_api/db/schemas.py index bc86a0b..e6eed79 100644 --- a/api/kiwi_vpn_api/db/schemas.py +++ b/api/kiwi_vpn_api/db/schemas.py @@ -4,6 +4,7 @@ from datetime import datetime from passlib.context import CryptContext from pydantic import BaseModel, validator +from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import Session from . import models @@ -83,10 +84,12 @@ class User(UserBase): .first()) if user is None: + # inexistent user, fake doing password verification crypt_context.dummy_verify() return None if not crypt_context.verify(password, user.password): + # password hash mismatch return None return cls.from_orm(user) @@ -97,21 +100,25 @@ class User(UserBase): db: Session, user: UserCreate, crypt_context: CryptContext, - ) -> User: - user = models.User( - name=user.name, - password=crypt_context.hash(user.password), - capabilities=[ - models.UserCapability(capability=capability) - for capability in user.capabilities - ] - ) + ) -> User | None: + try: + user = models.User( + name=user.name, + password=crypt_context.hash(user.password), + capabilities=[ + models.UserCapability(capability=capability) + for capability in user.capabilities + ] + ) - db.add(user) - db.commit() - db.refresh(user) + db.add(user) + db.commit() + db.refresh(user) - return cls.from_orm(user) + return cls.from_orm(user) + + except IntegrityError: + pass class DistinguishedNameBase(BaseModel):