def check_code(self, code): from users.utils import check_otp_code assert self.is_authenticated() ok = check_otp_code(self.user.otp_secret_key, code) msg = '' if ok else otp_failed_msg return ok, msg
def form_valid(self, form): user = get_user_or_tmp_user(self.request) otp_code = form.cleaned_data.get('otp_code') otp_secret_key = user.otp_secret_key if check_otp_code(otp_secret_key, otp_code): auth_login(self.request, user) self.send_auth_signal(success=True, user=user) return redirect(self.get_success_url()) else: self.send_auth_signal(success=False, username=user.username, reason=LoginLog.REASON_MFA) form.add_error('otp_code', _('MFA code invalid, or ntp sync server time')) return super().form_invalid(form)
def post(self, request): otp_code = request.data.get('otp_code', '') seed = request.data.get('seed', '') user = cache.get(seed, None) if not user: return Response( {'msg': _('Please verify the user name and password first')}, status=401 ) if not check_otp_code(user.otp_secret_key, otp_code): self.send_auth_signal(success=False, username=user.username, reason=LoginLog.REASON_MFA) return Response({'msg': _('MFA certification failed')}, status=401) self.send_auth_signal(success=True, user=user) token = user.create_bearer_token(request) data = {'token': token, 'user': self.serializer_class(user).data} return Response(data)
def form_valid(self, form): user = get_user_or_tmp_user(self.request) otp_code = form.cleaned_data.get('otp_code') otp_secret_key = user.otp_secret_key if check_otp_code(otp_secret_key, otp_code): auth_login(self.request, user) self.send_auth_signal(success=True, user=user) return redirect(self.get_success_url()) else: self.send_auth_signal( success=False, username=user.username, reason=LoginLog.REASON_MFA ) form.add_error( 'otp_code', _('MFA code invalid, or ntp sync server time') ) return super().form_invalid(form)