Create Device for SuperUser

61 views
Skip to first unread message

Senotier Yann

unread,
Jul 9, 2019, 2:24:02 AM7/9/19
to django-otp
Hi,

Is there a way to programmatically create an OTP Device for a django superuser without doing it manually via the admin interface? 
I am able to register the device manually via the admin page, but I would like to be able to tear down my whole application and re-create it from the code, without having to register a device manually every time. As part of the deployment script, I am creating automatically the super user, but I haven't found a way to automatically create the Device and associate it with the user.

In other words, I would like to take an existing Device (with existing key) that I previously registered with a QR code, and being able to recreate it automatically once I recreate my model.

Would something like that work and be a clean way to do it, or is there any other existing methods that should be used to do that?

from django_otp.plugins.otp_totp.models import TOTPDevice

# Register phone for 2 factor authentication
device = TOTPDevice(user=user, name="Phone", confirmed="True", key="vcbd5fd66f453d578fg45f479fa2d349fsg54dc49d")

Thank you
Regards
Yann

W Koot

unread,
Jul 9, 2019, 5:48:12 AM7/9/19
to djang...@googlegroups.com
I use a signal to automatically create a temp static token (which is invalidated elsewhere) for all users on creation.
Perhaps you can modify this to your needs, e.g. not creating a new one but hooking it up to your existing one.

@receiver(post_save, sender=get_user_model())
@transaction.atomic
def post_save_user(sender, instance, created, **kwargs):
if created:
sotp_device = StaticDevice(user=instance, name='Temp')
sotp_device.save()

base85_16char_token = b85encode(urandom(15)).decode('utf-8').lower()[:16]

static_token = StaticToken(device=sotp_device, token=base85_16char_token)
static_token.save()

--
You received this message because you are subscribed to the Google Groups "django-otp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-otp+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-otp/24fb630a-bdb2-437e-a47c-fa3195202b48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Senotier Yann

unread,
Jul 19, 2019, 12:43:49 AM7/19/19
to django-otp
Hi,

Thank you for your response. Your code below seems to create a dummy token and device right? Which means I would not be able to login using a real existing device.
What I am trying to do is link an existing device with the user programmatically. In other words is there a link between the token you are mentioning and the "key" parameters in the TOTPDevice model? If there is then maybe I would be able to use that existing key and the code you provided to create not a dummy device/token, but a real one with the existing key.

Thank you
Regards
Yann
To unsubscribe from this group and stop receiving emails from it, send an email to djang...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages