def save(self):
        from talos.models import BasicIdentity
        from talos.contrib.sms_sender import SMSSender
        from talos_rest.utils import send_email

        self.token.principal.email = self.token.identifier_value
        self.token.principal.save()
        self.token.is_active = False
        self.token.save()

        basic_identity = BasicIdentity.objects.get(principal=self.principal)
        basic_identity.username = self.token.identifier_value
        basic_identity.save()

        context = {'email': self.principal.email}

        send_email(context, [self.token.principal.email],
                   'talos/email_reset/confirmed_email_reset_subject.txt',
                   'talos/email_reset/confirmed_email_reset_body.txt',
                   'talos/email_reset/confirmed_email_reset_body.html')

        phone_text = render_to_string(
            'talos/email_reset/confirmed_email_reset_mobile.txt')
        sms_sender = SMSSender()
        sms_sender.send_message(self.token.principal.phone, phone_text)
    def send_otp(self, principal, credential):
        import pyotp
        from ..contrib.sms_sender import SMSSender

        salt = credential.salt
        totp = pyotp.TOTP(salt, interval=200)

        sms_sender = SMSSender()
        sms_sender.send_message(principal.phone, totp.now())
    def save(self):
        from talos.contrib.sms_sender import SMSSender

        self.token.principal.phone = self.token.identifier_value
        self.token.principal.save()
        self.token.is_active = False
        self.token.save()

        phone_text = render_to_string(
            'talos/phone_reset/confirmed_phone_reset_phone.txt')
        sms_sender = SMSSender()
        sms_sender.send_message(self.token.principal.phone, phone_text)
    def save(self):
        from talos.models import BasicIdentity
        from talos.contrib.sms_sender import SMSSender
        from talos_rest.utils import send_email

        old_email = self.token.principal.email

        self.token.principal.email = self.token.identifier_value
        self.token.principal.is_email_verified = True
        self.token.principal.save()
        self.token.is_active = False
        self.token.save()

        basic_identity = BasicIdentity.objects.get(
            principal=self.token.principal)
        basic_identity.username = self.token.identifier_value
        basic_identity.save()
        # Remove session to logout
        self.request.session.flush()

        context = {
            'email': self.token.principal.email,
            'recipient_name': self.token.principal.full_name
        }

        send_email(context, [self.token.principal.email],
                   'talos/email_change/confirmed_email_subject.txt',
                   'talos/email_change/confirmed_email_body.txt',
                   'talos/email_change/confirmed_email_body.html')

        send_email(context, [old_email],
                   'talos/email_change/confirmed_email_subject.txt',
                   'talos/email_change/confirmed_email_body.txt',
                   'talos/email_change/confirmed_email_body.html')

        mail_change_text = render_to_string(
            'talos/email_change/confirmed_email_body_mobile.txt')
        sms_sender = SMSSender()
        sms_sender.send_message(self.token.principal.phone, mail_change_text)
    def generate_credentials(self, principal, credentials):
        from ..models import _tznow
        from pyotp import TOTP
        from talos.models import OneTimePasswordCredential
        from ..contrib.sms_sender import SMSSender

        try:
            otp_credential = self._credential_directory.credentials.get(
                principal=principal,
                valid_from__lte=_tznow(),
                valid_till__gte=_tznow())

            secret_key = otp_credential.salt.decode()
            totp = TOTP(secret_key)

            sms_sender = SMSSender()
            sms_sender.send_message(principal.phone, 'Your registraion code is %s' % totp.now())

            return True

        except OneTimePasswordCredential.DoesNotExist:
            pass
        return False
    def create_credentials(self, principal, credentials):
        from uuid import uuid4
        from ..models import OneTimePasswordCredential
        from ..models import _tzmin
        from ..models import _tzmax
        from pyotp import random_base32
        from talos.contrib.sms_sender import SMSSender
        import pyotp

        otp_credential = OneTimePasswordCredential()
        otp_credential.uuid = uuid4()
        otp_credential.directory = self._credential_directory
        otp_credential.principal = principal
        otp_credential.valid_from = _tzmin()
        otp_credential.valid_till = _tzmax()
        base32_secret = random_base32()
        otp_credential.salt = base32_secret.encode()
        otp_credential.save()


        totp = pyotp.TOTP(otp_credential.salt, interval=200)
        sms_sender = SMSSender()
        sms_sender.send_message(principal.phone, 'Your code is {}'.format(totp.now()))