diff --git a/idhub/migrations/0001_initial.py b/idhub/migrations/0001_initial.py index 24b6c3c..fe6fc19 100644 --- a/idhub/migrations/0001_initial.py +++ b/idhub/migrations/0001_initial.py @@ -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.db import migrations, models @@ -30,6 +30,27 @@ class Migration(migrations.Migration): ('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( name='Rol', fields=[ @@ -118,7 +139,7 @@ class Migration(migrations.Migration): 'status', models.PositiveSmallIntegerField( choices=[ - (1, 'Enable'), + (1, 'Enabled'), (2, 'Issued'), (3, 'Revoked'), (4, 'Expired'), diff --git a/idhub/models.py b/idhub/models.py index c558712..8a805d4 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -1,4 +1,5 @@ import json +import requests from django.db import models from django.utils.translation import gettext_lazy as _ from idhub_auth.models import User @@ -174,3 +175,18 @@ class UserRol(models.Model): on_delete=models.CASCADE, 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) \ No newline at end of file diff --git a/idhub/templates/idhub/user/credentials_presentation.html b/idhub/templates/idhub/user/credentials_presentation.html index 1ac25fd..b94e3c8 100644 --- a/idhub/templates/idhub/user/credentials_presentation.html +++ b/idhub/templates/idhub/user/credentials_presentation.html @@ -6,4 +6,29 @@ {{ subtitle }} +{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +
+
+ {% bootstrap_form form %} +
+
+
+ {% trans "Cancel" %} + +
+ +
{% endblock %} diff --git a/idhub/user/forms.py b/idhub/user/forms.py index 1138eee..8cbc94e 100644 --- a/idhub/user/forms.py +++ b/idhub/user/forms.py @@ -1,6 +1,11 @@ from django import forms 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): @@ -35,7 +40,8 @@ class RequestCredentialForm(forms.Form): ) cred = VerificableCredential.objects.filter( user=self.user, - id=self.data['credential'] + id=self.data['credential'], + status=VerificableCredential.Status.ENABLED ) if not all([cred.exists(), did.exists()]): return @@ -50,3 +56,40 @@ class RequestCredentialForm(forms.Form): 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 + diff --git a/idhub/user/views.py b/idhub/user/views.py index d35159b..5071968 100644 --- a/idhub/user/views.py +++ b/idhub/user/views.py @@ -13,7 +13,7 @@ from django.urls import reverse_lazy from django.http import HttpResponse from django.contrib import messages 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.models import DID, VerificableCredential @@ -130,10 +130,25 @@ class UserCredentialsRequestView(MyWallet, FormView): return super().form_valid(form) -class UserCredentialsPresentationView(MyWallet, TemplateView): +class UserCredentialsPresentationView(MyWallet, FormView): template_name = "idhub/user/credentials_presentation.html" subtitle = _('Credentials Presentation') 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): @@ -148,6 +163,7 @@ class UserDidsView(MyWallet, TemplateView): }) return context + class UserDidRegisterView(MyWallet, CreateView): template_name = "idhub/user/did_register.html" subtitle = _('Add a new Identities (DID)') diff --git a/idhub_auth/migrations/0001_initial.py b/idhub_auth/migrations/0001_initial.py index 8251b11..2196067 100644 --- a/idhub_auth/migrations/0001_initial.py +++ b/idhub_auth/migrations/0001_initial.py @@ -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