diff --git a/oidc4vp/models.py b/oidc4vp/models.py index ca449c7..66ceff1 100644 --- a/oidc4vp/models.py +++ b/oidc4vp/models.py @@ -168,7 +168,7 @@ class OAuth2VPToken(models.Model): authorization = models.ForeignKey( Authorization, on_delete=models.SET_NULL, - related_name='oauth2vptoken', + related_name='vp_tokens', null=True, ) @@ -176,10 +176,7 @@ class OAuth2VPToken(models.Model): code = kwargs.pop("code", None) super().__init__(*args, **kwargs) - self.authorization = get_object_or_404( - Authorization, - code=code - ) + self.authorization = Authorization.objects.filter(code=code).first() def verifing(self): self.result_verify = verify_presentation(self.vp_token) diff --git a/oidc4vp/views.py b/oidc4vp/views.py index fd164bf..3f34bfd 100644 --- a/oidc4vp/views.py +++ b/oidc4vp/views.py @@ -40,7 +40,6 @@ class AuthorizeView(UserView, FormView): return kwargs def form_valid(self, form): - # import pdb; pdb.set_trace() authorization = form.save() if not authorization or authorization.status_code != 200: messages.error(self.request, _("Error sending credential!")) @@ -67,7 +66,6 @@ class AuthorizeView(UserView, FormView): return super().form_valid(form) def get_org(self): - # import pdb; pdb.set_trace() client_id = self.request.GET.get("client_id") if not client_id: raise Http404("Organization not found!") @@ -93,7 +91,6 @@ class VerifyView(View): return HttpResponse(res) def post(self, request, *args, **kwargs): - # import pdb; pdb.set_trace() code = self.request.POST.get("code") vp_tk = self.request.POST.get("vp_token") @@ -107,6 +104,8 @@ class VerifyView(View): organization=org, code=code ) + if not vp_token.authorization: + raise Http404("Page not Found!") vp_token.verifing() response = vp_token.get_response_verify() diff --git a/promotion/views.py b/promotion/views.py index fbc6343..79b47ea 100644 --- a/promotion/views.py +++ b/promotion/views.py @@ -38,7 +38,6 @@ class ContractView(FormView): success_url = reverse_lazy('promotion:thanks') def get_context_data(self, **kwargs): - # import pdb; pdb.set_trace() self.context = super().get_context_data(**kwargs) code = self.request.GET.get("code") self.get_discount(code) @@ -50,29 +49,34 @@ class ContractView(FormView): "total": 25.0 }) if self.promotion: - self.context['sim'] = self.context.get_discount(self.context["sim"]) - self.context['mensual'] = self.context.get_discount(self.context["mensual"]) - self.context['total'] = self.context.get_discount(self.context["total"]) + self.context['sim'] = self.promotion.get_discount(self.context["sim"]) + self.context['mensual'] = self.promotion.get_discount(self.context["mensual"]) + self.context['total'] = self.promotion.get_discount(self.context["total"]) + + if self.vp_token: + self.context['verificable_presentation'] = self.vp_token + return self.context def get_form_kwargs(self): kwargs = super().get_form_kwargs() + code = self.request.GET.get("code") + self.get_discount(code) if not self.vp_token: return kwargs self.vp_token.get_user_info() - kwargs['verificable_presentation'] = self.vp_token - kwargs["nif"] = self.vp_token.user_info.get("nif", '') - kwargs["name"] = self.vp_token.user_info.get("name", '') - kwargs["first_last_name"] = self.vp_token.user_info.get("first_last_name", '') - kwargs["second_last_name"] = self.vp_token.user_info.get("second_last_name", '') - kwargs["email"] = self.vp_token.user_info.get("email", '') - kwargs["email_repeat"] = self.vp_token.user_info.get("email", '') - kwargs["telephone"] = self.vp_token.user_info.get("telephone", '') - kwargs["birthday"] = self.vp_token.user_info.get("birthday", '') - kwargs["gen"] = self.vp_token.user_info.get("gen", '') - kwargs["lang"] = self.vp_token.user_info.get("lang", '') + kwargs['initial']["nif"] = self.vp_token.user_info.get("nif", '') + kwargs['initial']["name"] = self.vp_token.user_info.get("name", '') + kwargs['initial']["first_last_name"] = self.vp_token.user_info.get("first_last_name", '') + kwargs['initial']["second_last_name"] = self.vp_token.user_info.get("second_last_name", '') + kwargs['initial']["email"] = self.vp_token.user_info.get("email", '') + kwargs['initial']["email_repeat"] = self.vp_token.user_info.get("email", '') + kwargs['initial']["telephone"] = self.vp_token.user_info.get("telephone", '') + kwargs['initial']["birthday"] = self.vp_token.user_info.get("birthday", '') + kwargs['initial']["gen"] = self.vp_token.user_info.get("gen", '') + kwargs['initial']["lang"] = self.vp_token.user_info.get("lang", '') return kwargs def form_valid(self, form): @@ -81,16 +85,18 @@ class ContractView(FormView): def get_discount(self, code): if not code: return + if self.authorization: + return self.authorization = Authorization.objects.filter( code=code, - code_unused=False + code_used=False ).first() if self.authorization: - if self.authorization.promotions: - self.promotion = self.authorization.promotionsp[-1] - if self.authorization.vp_tokens: - self.vp_tokens = self.authorization.vp_tokens[-1] + if self.authorization.promotions.exists(): + self.promotion = self.authorization.promotions.all()[0] + if self.authorization.vp_tokens.exists(): + self.vp_token = self.authorization.vp_tokens.all()[0] class SelectWalletView(FormView):