from core.classes.otp import BaseOTPProvider from smsotp.models import OTPToken from core.modules.sms import get_default_sms_provider from dbsettings.functions import getValue from django.utils import timezone class SMSOTP(BaseOTPProvider): @property def get_name(self): return "SMS OTP" def create_token(self, user): token = OTPToken.objects.create(user=user) try: token.send_sms() return True except: return False @property def is_active(self): return bool(get_default_sms_provider()) def start_authentication(self, user): if self.create_token(user): return "We have sent you an SMS containing your authentication token." else: return "An error has occurred, please try again later or contact the administrator." def validate_token(self, user, token): try: max_age = timezone.now() - timezone.timedelta(seconds=int(getValue("smsotp.max_age", "300"))) OTPToken.objects.get(user=user, token=token, creation__gte=max_age).delete() return True except OTPToken.DoesNotExist: return False OTPPROVIDERS = {"smsotp": SMSOTP}