Вот мой код реализации:
- Метод для генерации otp-кода:< /li>
totp = pyotp.TOTP(secret, interval=settings.verification_code_expire_time)
verification_code = totp.now()
if not user.code:
user.code = VerificationCode(code=verification_code)
else:
user.code.code = verification_code
await db.commit()
return verification_code
- Метод сброса пароля:
result = await db.execute(query)
verification_code = result.scalar_one_or_none()
if not verification_code:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid verification code.")
totp = pyotp.TOTP(pyotp.random_base32(), interval=settings.verification_code_expire_time)
if not totp.verify(reset_password_schema.verification_code):
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Verification code has been expired.")
query = (
update(User).where(User.id == verification_code.user_id).values(
password=reset_password_schema.new_password)
)
Подробнее здесь: https://stackoverflow.com/questions/790 ... le-methods