如何在Django应用中设置高级一次性短信代码



我有短信认证在我的Django应用程序。这是正确的。但我想使用https://github.com/WICG/sms-one-time-codes的推荐所以,而不是4位数的代码,我希望用户得到这样的史密斯:https://example.com, 747723, 747723 is your ExampleCo authentication code

* *

"https://example.com"是与

相关联的代码的起源。"747723";代码是

"747723是您的exampleeco认证码。是人类可读的解释性文本。

services.py

from pyotp import HOTP
from django.db.models import F
def make_totp(user: User) -> str:
totp = HOTP(user.otp_secret, digits=4)
totp_code = totp.at(user.otp_counter)
return totp_code
def totp_verify(user: User, totp_code: str, verify=None) -> bool:
if user:
totp = HOTP(user.otp_secret, digits=4)
verify = totp.verify(otp=totp_code, counter=user.otp_counter)
if verify:
user.otp_counter = F("otp_counter") + 1
user.save()
return verify
有什么建议可以解决这个问题吗?

解决方案:

views.py

from django.conf import settings
code = make_totp(user=user)
if settings.DEBUG:
result = send_sms(phone=user.phone, totp_code=f"{code} - https://example.com, {code}, {code} is your ExampleCo authentication code")
else:
result = send_sms(phone=user.phone, totp_code=f"{code} - https://example.com, {code}, {code} is your ExampleCo authentication code")

最新更新