def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: user = self.executor.plan.context.get(PLAN_CONTEXT_PENDING_USER) if not user: LOGGER.debug("No pending user, continuing") return self.executor.stage_ok() # Currently, this stage only supports one device per user. If the user already # has a device, just skip to the next stage if StaticDevice.objects.filter(user=user).exists(): return self.executor.stage_ok() stage: AuthenticatorStaticStage = self.executor.current_stage if SESSION_STATIC_DEVICE not in self.request.session: device = StaticDevice(user=user, confirmed=True, name="Static Token") tokens = [] for _ in range(0, stage.token_count): tokens.append( StaticToken(device=device, token=StaticToken.random_token())) self.request.session[SESSION_STATIC_DEVICE] = device self.request.session[SESSION_STATIC_TOKENS] = tokens return super().get(request, *args, **kwargs)
def save(self, user): static_device = StaticDevice(name=self.cleaned_data["name"], user=user) static_device.save() for i in range(10): code = StaticToken(token=StaticToken.random_token().upper()) code.device = static_device code.save() return static_device
def handle(self, *args, **options): if len(args) != 1: raise CommandError('Please specify exactly one username.') username = args[0] try: user = get_user_model().objects.get_by_natural_key(username) except ObjectDoesNotExist: raise CommandError('User "{0}" does not exist.'.format(username)) device = next(StaticDevice.objects.filter(user=user).iterator(), None) if device is None: device = StaticDevice.objects.create(user=user, name='Backup Code') token = options.get('token') if token is None: token = StaticToken.random_token() device.token_set.add(StaticToken(token=token)) print(token, file=self.stdout)