def test_verify(self):
     for no_digits in (6, 8):
         with self.settings(TWO_FACTOR_TOTP_DIGITS=no_digits):
             device = PhoneDevice(key=random_hex().decode())
             self.assertFalse(device.verify_token(-1))
             self.assertFalse(device.verify_token('foobar'))
             self.assertTrue(device.verify_token(totp(device.bin_key, digits=no_digits)))
 def test_verify(self):
     for no_digits in (6, 8):
         with self.settings(TWO_FACTOR_TOTP_DIGITS=no_digits):
             device = PhoneDevice(key=random_hex().decode())
             self.assertFalse(device.verify_token(-1))
             self.assertFalse(device.verify_token('foobar'))
             self.assertTrue(device.verify_token(totp(device.bin_key, digits=no_digits)))
    def test_unicode(self):
        device = PhoneDevice(name="unknown")
        self.assertEqual("unknown (None)", str(device))

        user = User.objects.create_user("bouke")
        device.user = user
        self.assertEqual("unknown (bouke)", str(device))
Exemplo n.º 4
0
    def test_unicode(self):
        device = PhoneDevice(name='unknown')
        self.assertEqual('unknown (None)', str(device))

        user = User.objects.create_user('bouke')
        device.user = user
        self.assertEqual('unknown (bouke)', str(device))
Exemplo n.º 5
0
 def test_verify_token_as_string(self):
     """
     The field used to read the token may be a CharField,
     so the PhoneDevice must be able to validate tokens
     read as strings
     """
     device = PhoneDevice(key=random_hex().decode())
     self.assertTrue(device.verify_token(str(totp(device.bin_key))))
Exemplo n.º 6
0
 def test_verify_token_as_string(self):
     """
     The field used to read the token may be a CharField,
     so the PhoneDevice must be able to validate tokens
     read as strings
     """
     device = PhoneDevice(key=random_hex().decode())
     self.assertTrue(device.verify_token(str(totp(device.bin_key))))
 def test_verify_token_as_string(self):
     """
     The field used to read the token may be a CharField,
     so the PhoneDevice must be able to validate tokens
     read as strings
     """
     for no_digits in (6, 8):
         with self.settings(TWO_FACTOR_TOTP_DIGITS=no_digits):
             device = PhoneDevice(key=random_hex().decode())
             self.assertTrue(device.verify_token(str(totp(device.bin_key, digits=no_digits))))
Exemplo n.º 8
0
 def create_default_device(self,
                           user_profile: UserProfile,
                           number: str = "+12125550100") -> None:
     phone_device = PhoneDevice(user=user_profile,
                                name='default',
                                confirmed=True,
                                number=number,
                                key='abcd',
                                method='sms')
     phone_device.save()
 def test_verify_token_as_string(self):
     """
     The field used to read the token may be a CharField,
     so the PhoneDevice must be able to validate tokens
     read as strings
     """
     for no_digits in (6, 8):
         with self.settings(TWO_FACTOR_TOTP_DIGITS=no_digits):
             device = PhoneDevice(key=random_hex().decode())
             self.assertTrue(device.verify_token(str(totp(device.bin_key, digits=no_digits))))
Exemplo n.º 10
0
 def create_default_device(
     self, user_profile: UserProfile, number: str = "+12125550100"
 ) -> None:
     phone_device = PhoneDevice(
         user=user_profile,
         name="default",
         confirmed=True,
         number=number,
         key="abcd",
         method="sms",
     )
     phone_device.save()
Exemplo n.º 11
0
 def get_device(self, **kwargs):
     """
     Uses the data from the setup step and generated key to recreate device, gets the 'method' step
     in the form_list.
     """
     kwargs = kwargs or {}
     kwargs.update(self.storage.validated_step_data.get('method', {}))
     return PhoneDevice(key=self.get_key(), **kwargs)
Exemplo n.º 12
0
    def test_unicode(self):
        device = PhoneDevice(name='unknown')
        self.assertEqual('unknown (None)', str(device))

        device.user = self.create_user()
        self.assertEqual('unknown ([email protected])', str(device))
Exemplo n.º 13
0
 def test_verify(self):
     device = PhoneDevice(key=random_hex().decode())
     self.assertFalse(device.verify_token(-1))
     self.assertTrue(device.verify_token(totp(device.bin_key)))
Exemplo n.º 14
0
 def create_default_device(self, user_profile: UserProfile,
                           number: str="+12223334444") -> None:
     phone_device = PhoneDevice(user=user_profile, name='default',
                                confirmed=True, number=number,
                                key='abcd', method='sms')
     phone_device.save()
Exemplo n.º 15
0
    def test_unicode(self):
        device = PhoneDevice(name='unknown')
        self.assertEqual('unknown (None)', str(device))

        device.user = self.create_user()
        self.assertEqual('unknown ([email protected])', str(device))
Exemplo n.º 16
0
    def post(self, request, *args, **kwargs):
        request_data = json.loads(request.body.decode("utf-8"))
        form = AuthenticationDeviceForm(request_data)
        if form.is_valid():
            device_type = form.cleaned_data.get('device_type')
            if device_type == 'authenticator':
                code = form.cleaned_data.get('code')
                if code:
                    try:
                        device = TOTPDevice.objects.get(user=request.user)
                        if device.verify_token(code):
                            device.confirmed = True
                            device.save()
                            return self.success_response()
                    except:
                        pass
                    return self.error_response(
                        general_errors=["Invalid code. Please try again."])
                else:
                    device = None
                    try:
                        device = TOTPDevice.objects.get(user=request.user)
                    except:
                        pass

                    if not device:
                        device_data = {
                            'user': request.user,
                            'name': 'authentication_app',
                            'confirmed': False
                        }
                        device = TOTPDevice.objects.create(**device_data)

                    import qrcode
                    import qrcode.image.svg
                    img = qrcode.make(device.config_url,
                                      image_factory=qrcode.image.svg.SvgImage)
                    img_response = HttpResponse(content_type='image/svg+xml')
                    img.save(img_response)
                    import base64

                    return self.success_response(
                        result={
                            'config_url':
                            device.config_url,
                            'svg_image':
                            base64.b64encode(img_response.content).decode(
                                "utf-8")
                        })
            elif device_type == 'sms':
                code = form.cleaned_data.get('code')
                if code:
                    try:
                        device = PhoneDevice.objects.get(user=request.user)
                        if device.verify_token(code):
                            device.confirmed = True
                            device.save()
                            return self.success_response()
                    except:
                        pass
                    return self.error_response(
                        general_errors=["Invalid code. Please try again."])
                else:
                    device = PhoneDevice(
                        user=request.user,
                        name='sms',
                        number='+1' + request_data.get('mobile'),
                        method='sms',
                        confirmed=False,
                    )
                    device.generate_challenge()
                    device.save()
                    return self.success_response()
            return self.error_response()
        return self.form_error_response(form)
Exemplo n.º 17
0
 def test_verify(self):
     device = PhoneDevice(key=random_hex().decode())
     self.assertFalse(device.verify_token(-1))
     self.assertTrue(device.verify_token(totp(device.bin_key)))