2020-05-13 10:38:37 +00:00
|
|
|
from core.classes.otp import BaseOTPProvider
|
|
|
|
from totp.models import TOTPUser
|
|
|
|
|
|
|
|
from dbsettings.functions import getValue
|
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
import pyotp
|
|
|
|
|
|
|
|
class TOTP(BaseOTPProvider):
|
|
|
|
@property
|
|
|
|
def get_name(self):
|
|
|
|
return "Time-based OTP"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_active(self):
|
|
|
|
return True
|
|
|
|
|
2020-05-22 16:13:23 +00:00
|
|
|
def active_for_user(self, user):
|
|
|
|
try:
|
|
|
|
TOTPUser.objects.get(user=user)
|
|
|
|
return super().active_for_user(user)
|
|
|
|
except TOTPUser.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
2020-05-13 10:38:37 +00:00
|
|
|
def start_authentication(self, user):
|
|
|
|
return "Please enter the token displayed in your app."
|
|
|
|
|
|
|
|
def validate_token(self, user, token):
|
|
|
|
try:
|
|
|
|
otpuser = TOTPUser.objects.get(user=user)
|
|
|
|
return pyotp.TOTP(otpuser.secret).verify(token)
|
2020-05-22 16:13:23 +00:00
|
|
|
except TOTPUser.DoesNotExist:
|
2020-05-13 10:38:37 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
OTPPROVIDERS = {"totp": TOTP}
|