add presentation credential

This commit is contained in:
Cayo Puigdefabregas 2023-11-02 17:13:49 +01:00
parent 60efbf4a25
commit e2d70f83e2
6 changed files with 128 additions and 7 deletions

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2023-10-27 09:32 # Generated by Django 4.2.5 on 2023-11-02 15:08
from django.conf import settings from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -30,6 +30,27 @@ class Migration(migrations.Migration):
('created_at', models.DateTimeField(auto_now=True)), ('created_at', models.DateTimeField(auto_now=True)),
], ],
), ),
migrations.CreateModel(
name='Organization',
fields=[
(
'id',
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID',
),
),
('name', models.CharField(max_length=250)),
(
'url',
models.CharField(
help_text='Url where to send the presentation', max_length=250
),
),
],
),
migrations.CreateModel( migrations.CreateModel(
name='Rol', name='Rol',
fields=[ fields=[
@ -118,7 +139,7 @@ class Migration(migrations.Migration):
'status', 'status',
models.PositiveSmallIntegerField( models.PositiveSmallIntegerField(
choices=[ choices=[
(1, 'Enable'), (1, 'Enabled'),
(2, 'Issued'), (2, 'Issued'),
(3, 'Revoked'), (3, 'Revoked'),
(4, 'Expired'), (4, 'Expired'),

View File

@ -1,4 +1,5 @@
import json import json
import requests
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from idhub_auth.models import User from idhub_auth.models import User
@ -174,3 +175,18 @@ class UserRol(models.Model):
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name='users', related_name='users',
) )
class Organization(models.Model):
name = models.CharField(max_length=250)
url = models.CharField(
help_text=_("Url where to send the presentation"),
max_length=250
)
def __str__(self):
return self.name
def send(self, cred):
return
requests.post(self.url, data=cred.data)

View File

@ -6,4 +6,29 @@
<i class="{{ icon }}"></i> <i class="{{ icon }}"></i>
{{ subtitle }} {{ subtitle }}
</h3> </h3>
{% load django_bootstrap5 %}
<form role="form" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
{% for field, error in form.errors.items %}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-sm-4">
{% bootstrap_form form %}
</div>
</div>
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'idhub:user_credentials' %}">{% trans "Cancel" %}</a>
<input class="btn btn-green-user" type="submit" name="submit" value="{% trans 'Send' %}" />
</div>
</form>
{% endblock %} {% endblock %}

View File

@ -1,6 +1,11 @@
from django import forms from django import forms
from idhub_auth.models import User from idhub_auth.models import User
from idhub.models import DID, VerificableCredential from idhub.models import DID, VerificableCredential, Organization
ORGANIZATION = [
(x.id, x.name) for x in Organization.objects.filter()
]
class ProfileForm(forms.ModelForm): class ProfileForm(forms.ModelForm):
@ -35,7 +40,8 @@ class RequestCredentialForm(forms.Form):
) )
cred = VerificableCredential.objects.filter( cred = VerificableCredential.objects.filter(
user=self.user, user=self.user,
id=self.data['credential'] id=self.data['credential'],
status=VerificableCredential.Status.ENABLED
) )
if not all([cred.exists(), did.exists()]): if not all([cred.exists(), did.exists()]):
return return
@ -50,3 +56,40 @@ class RequestCredentialForm(forms.Form):
return return
class CredentialPresentationForm(forms.Form):
organization = forms.ChoiceField(choices=ORGANIZATION)
credential = forms.ChoiceField(choices=[])
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super().__init__(*args, **kwargs)
self.fields['credential'].choices = [
(x.id, x.type()) for x in VerificableCredential.objects.filter(
user=self.user,
status=VerificableCredential.Status.ISSUED
)
]
def save(self, commit=True):
org = Organization.objects.filter(
id=self.data['organization']
)
cred = VerificableCredential.objects.filter(
user=self.user,
id=self.data['credential'],
status=VerificableCredential.Status.ISSUED
)
if not all([org.exists(), cred.exists()]):
return
org =org[0]
cred = cred[0]
if commit:
org.send(cred)
return cred
return

View File

@ -13,7 +13,7 @@ from django.urls import reverse_lazy
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib import messages from django.contrib import messages
from apiregiter import iota from apiregiter import iota
from idhub.user.forms import ProfileForm, RequestCredentialForm from idhub.user.forms import ProfileForm, RequestCredentialForm, CredentialPresentationForm
from idhub.mixins import UserView from idhub.mixins import UserView
from idhub.models import DID, VerificableCredential from idhub.models import DID, VerificableCredential
@ -130,10 +130,25 @@ class UserCredentialsRequestView(MyWallet, FormView):
return super().form_valid(form) return super().form_valid(form)
class UserCredentialsPresentationView(MyWallet, TemplateView): class UserCredentialsPresentationView(MyWallet, FormView):
template_name = "idhub/user/credentials_presentation.html" template_name = "idhub/user/credentials_presentation.html"
subtitle = _('Credentials Presentation') subtitle = _('Credentials Presentation')
icon = 'bi bi-patch-check-fill' icon = 'bi bi-patch-check-fill'
form_class = CredentialPresentationForm
success_url = reverse_lazy('idhub:user_credentials')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def form_valid(self, form):
cred = form.save()
if cred:
messages.success(self.request, _("The credential was presented successfully!"))
else:
messages.error(self.request, _("Error sending credential!"))
return super().form_valid(form)
class UserDidsView(MyWallet, TemplateView): class UserDidsView(MyWallet, TemplateView):
@ -148,6 +163,7 @@ class UserDidsView(MyWallet, TemplateView):
}) })
return context return context
class UserDidRegisterView(MyWallet, CreateView): class UserDidRegisterView(MyWallet, CreateView):
template_name = "idhub/user/did_register.html" template_name = "idhub/user/did_register.html"
subtitle = _('Add a new Identities (DID)') subtitle = _('Add a new Identities (DID)')

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2023-10-27 09:32 # Generated by Django 4.2.5 on 2023-11-02 15:08
from django.db import migrations, models from django.db import migrations, models