def login( form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(deps.get_db), ) -> schemas.Token: """ OAuth2 compatible token login, get an access token for future requests """ user: models.User = db.query(models.User).filter( models.User.email == form_data.username.lower()).first() if not user or not verify_password(form_data.password, user.password): raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Incorrect email or password") if not user.active: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user") token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES) return schemas.Token( access_token=create_access_token(user.id, user.email, user.username, expires_delta=token_expires), token_type="bearer", )
async def authenticate( self, db: AsyncSession, *, email_or_cpf: str, password: str ) -> Optional[User]: if user := await self.get_by_email_or_cpf( db, email_or_cpf=email_or_cpf ): if verify_password(password, user.password): return user
async def authenticate(self, db: AsyncSession, *, email: str, password: str) -> Optional[User]: user = await self.get_by_email(db, email=email) if not user: return None if not verify_password(password, user.hashed_password): return None return user
async def authenticate(self, db: AsyncSession, *, email: str, password: str) -> Optional[models.User]: user = await self.get_by_key(db, key="email", value=email) if not user or len(user) == 0: return None else: user = user[0] if not verify_password(password, user.hashed_password): return None return user
def verify_password(self, password: str): return security.verify_password(password, self.password_hash)
def test_verify_password(password, expected, password_hash): assert verify_password(password, password_hash) is expected