fix decrypt creds and add Events

This commit is contained in:
Cayo Puigdefabregas 2024-01-21 13:45:08 +01:00
parent 06cc971090
commit baee7ba135
2 changed files with 29 additions and 7 deletions

View File

@ -19,7 +19,9 @@ class AuthorizeForm(forms.Form):
self.user = kwargs.pop('user', None) self.user = kwargs.pop('user', None)
self.org = kwargs.pop('org', None) self.org = kwargs.pop('org', None)
self.code = kwargs.pop('code', None) self.code = kwargs.pop('code', None)
self.pw = kwargs.pop('pw', None)
self.presentation_definition = kwargs.pop('presentation_definition', []) self.presentation_definition = kwargs.pop('presentation_definition', [])
self.subject_did = None
reg = r'({})'.format('|'.join(self.presentation_definition)) reg = r'({})'.format('|'.join(self.presentation_definition))
@ -49,7 +51,12 @@ class AuthorizeForm(forms.Form):
txt = _('There are some problems with this credentials') txt = _('There are some problems with this credentials')
raise ValidationError(txt) raise ValidationError(txt)
self.list_credentials.append(c) cred = self.user.decrypt_data(
c.data,
self.pw
)
self.subject_did = c.subject_did
self.list_credentials.append(cred)
if not self.code: if not self.code:
txt = _("There isn't code in request") txt = _("There isn't code in request")
@ -69,13 +76,14 @@ class AuthorizeForm(forms.Form):
return return
def get_verificable_presentation(self): def get_verificable_presentation(self):
did = self.list_credentials[0].subject_did did = self.subject_did
vp_template = get_template('credentials/verifiable_presentation.json') vp_template = get_template('credentials/verifiable_presentation.json')
vc_list = json.dumps([json.loads(x.data) for x in self.list_credentials]) vc_list = json.dumps([json.loads(x) for x in self.list_credentials])
context = { context = {
"holder_did": did.did, "holder_did": did.did,
"verifiable_credential_list": vc_list "verifiable_credential_list": vc_list
} }
unsigned_vp = vp_template.render(context) unsigned_vp = vp_template.render(context)
self.vp = create_verifiable_presentation(did.key_material, unsigned_vp) key_material = did.get_key_material(self.pw)
self.vp = create_verifiable_presentation(key_material, unsigned_vp)

View File

@ -13,6 +13,7 @@ from django.contrib import messages
from oidc4vp.models import Authorization, Organization, OAuth2VPToken from oidc4vp.models import Authorization, Organization, OAuth2VPToken
from idhub.mixins import UserView from idhub.mixins import UserView
from idhub.models import Event
from oidc4vp.forms import AuthorizeForm from oidc4vp.forms import AuthorizeForm
from utils.idhub_ssikit import verify_presentation from utils.idhub_ssikit import verify_presentation
@ -43,6 +44,11 @@ class AuthorizeView(UserView, FormView):
kwargs['presentation_definition'] = vps kwargs['presentation_definition'] = vps
kwargs["org"] = self.get_org() kwargs["org"] = self.get_org()
kwargs["code"] = self.request.GET.get('code') kwargs["code"] = self.request.GET.get('code')
enc_pw = self.request.session["key_did"]
kwargs['pw'] = self.request.user.decrypt_data(
enc_pw,
self.request.user.password+self.request.session._session_key
)
return kwargs return kwargs
def get_form(self, form_class=None): def get_form(self, form_class=None):
@ -55,12 +61,12 @@ class AuthorizeView(UserView, FormView):
authorization = form.save() authorization = form.save()
if not authorization or authorization.status_code != 200: if not authorization or authorization.status_code != 200:
messages.error(self.request, _("Error sending credential!")) messages.error(self.request, _("Error sending credential!"))
return super().form_valid(form) return redirect(self.success_url)
try: try:
authorization = authorization.json() authorization = authorization.json()
except: except:
messages.error(self.request, _("Error sending credential!")) messages.error(self.request, _("Error sending credential!"))
return super().form_valid(form) return redirect(self.success_url)
verify = authorization.get('verify') verify = authorization.get('verify')
result, msg = verify.split(",") result, msg = verify.split(",")
@ -74,8 +80,16 @@ class AuthorizeView(UserView, FormView):
elif authorization.get('response'): elif authorization.get('response'):
txt = authorization.get('response') txt = authorization.get('response')
messages.success(self.request, txt) messages.success(self.request, txt)
cred = form.credentials.first()
verifier = form.org.name
if cred and verifier:
Event.set_EV_CREDENTIAL_PRESENTED(cred, verifier)
txt2 = f"Verifier {verifier} send: " + txt
Event.set_EV_USR_SEND_VP(txt2, self.request.user)
url = reverse_lazy('idhub:user_dashboard')
return redirect(url)
return super().form_valid(form) return redirect(self.success_url)
def get_org(self): def get_org(self):
client_id = self.request.GET.get("client_id") client_id = self.request.GET.get("client_id")