add type in schemas

This commit is contained in:
Cayo Puigdefabregas 2023-11-29 17:29:31 +01:00
parent 89f1668c5c
commit 8da426ef34
5 changed files with 123 additions and 94 deletions

View File

@ -772,11 +772,14 @@ class SchemasNewView(SchemasMix):
return return
try: try:
data = f.read().decode('utf-8') data = f.read().decode('utf-8')
assert credtools.validate_schema(json.loads(data)) ldata = json.loads(data)
assert credtools.validate_schema(ldata)
name = ldata.get('name')
assert name
except Exception: except Exception:
messages.error(self.request, _('This is not a valid schema!')) messages.error(self.request, _('This is not a valid schema!'))
return return
schema = Schemas.objects.create(file_schema=file_name, data=data) schema = Schemas.objects.create(file_schema=file_name, data=data, type=name)
schema.save() schema.save()
return schema return schema
@ -818,10 +821,14 @@ class SchemasImportAddView(SchemasMix):
data = self.open_file(file_name) data = self.open_file(file_name)
try: try:
json.loads(data) json.loads(data)
ldata = json.loads(data)
assert credtools.validate_schema(ldata)
name = ldata.get('name')
assert name
except Exception: except Exception:
messages.error(self.request, _('This is not a valid schema!')) messages.error(self.request, _('This is not a valid schema!'))
return return
schema = Schemas.objects.create(file_schema=file_name, data=data) schema = Schemas.objects.create(file_schema=file_name, data=data, type=name)
schema.save() schema.save()
return schema return schema

View File

@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2023-11-15 09:58 # Generated by Django 4.2.5 on 2023-11-29 16:14
from django.conf import settings from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -57,27 +57,6 @@ 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=[
@ -111,6 +90,7 @@ class Migration(migrations.Migration):
verbose_name='ID', verbose_name='ID',
), ),
), ),
('type', models.CharField(max_length=250)),
('file_schema', models.CharField(max_length=250)), ('file_schema', models.CharField(max_length=250)),
('data', models.TextField()), ('data', models.TextField()),
('created_at', models.DateTimeField(auto_now=True)), ('created_at', models.DateTimeField(auto_now=True)),
@ -274,36 +254,39 @@ class Migration(migrations.Migration):
'type', 'type',
models.PositiveSmallIntegerField( models.PositiveSmallIntegerField(
choices=[ choices=[
(1, 'EV_USR_REGISTERED'), (1, 'User registered'),
(2, 'EV_USR_WELCOME'), (2, 'User welcomed'),
(3, 'EV_DATA_UPDATE_REQUESTED_BY_USER'), (3, 'Data update requested by user'),
(4, 'EV_DATA_UPDATE_REQUESTED'), (
(5, 'EV_USR_UPDATED_BY_ADMIN'), 4,
(6, 'EV_USR_UPDATED'), 'Data update requested. Pending approval by administrator',
(7, 'EV_USR_DELETED_BY_ADMIN'), ),
(8, 'EV_DID_CREATED_BY_USER'), (5, "User's data updated by admin"),
(9, 'EV_DID_CREATED'), (6, 'Your data updated by admin'),
(10, 'EV_DID_DELETED'), (7, 'User deactivated by admin'),
(11, 'EV_CREDENTIAL_DELETED_BY_ADMIN'), (8, 'DID created by user'),
(12, 'EV_CREDENTIAL_DELETED'), (9, 'DID created'),
(13, 'EV_CREDENTIAL_ISSUED_FOR_USER'), (10, 'DID deleted'),
(14, 'EV_CREDENTIAL_ISSUED'), (11, 'Credential deleted by user'),
(15, 'EV_CREDENTIAL_PRESENTED_BY_USER'), (12, 'Credential deleted'),
(16, 'EV_CREDENTIAL_PRESENTED'), (13, 'Credential issued for user'),
(17, 'EV_CREDENTIAL_ENABLED'), (14, 'Credential issued'),
(18, 'EV_CREDENTIAL_CAN_BE_REQUESTED'), (15, 'Credential presented by user'),
(19, 'EV_CREDENTIAL_REVOKED_BY_ADMIN'), (16, 'Credential presented'),
(20, 'EV_CREDENTIAL_REVOKED'), (17, 'Credential enabled'),
(21, 'EV_ROLE_CREATED_BY_ADMIN'), (18, 'Credential available'),
(22, 'EV_ROLE_MODIFIED_BY_ADMIN'), (19, 'Credential revoked by admin'),
(23, 'EV_ROLE_DELETED_BY_ADMIN'), (20, 'Credential revoked'),
(24, 'EV_SERVICE_CREATED_BY_ADMIN'), (21, 'Role created by admin'),
(25, 'EV_SERVICE_MODIFIED_BY_ADMIN'), (22, 'Role modified by admin'),
(26, 'EV_SERVICE_DELETED_BY_ADMIN'), (23, 'Role deleted by admin'),
(27, 'EV_ORG_DID_CREATED_BY_ADMIN'), (24, 'Service created by admin'),
(28, 'EV_ORG_DID_DELETED_BY_ADMIN'), (25, 'Service modified by admin'),
(29, 'EV_USR_DEACTIVATED_BY_ADMIN'), (26, 'Service deleted by admin'),
(30, 'EV_USR_ACTIVATED_BY_ADMIN'), (27, 'Organisational DID created by admin'),
(28, 'Organisational DID deleted by admin'),
(29, 'User deactivated'),
(30, 'User activated'),
] ]
), ),
), ),

View File

@ -431,6 +431,7 @@ class DID(models.Model):
class Schemas(models.Model): class Schemas(models.Model):
type = models.CharField(max_length=250)
file_schema = models.CharField(max_length=250) file_schema = models.CharField(max_length=250)
data = models.TextField() data = models.TextField()
created_at = models.DateTimeField(auto_now=True) created_at = models.DateTimeField(auto_now=True)
@ -492,10 +493,9 @@ class VerificableCredential(models.Model):
return json.loads(self.data) return json.loads(self.data)
def type(self): def type(self):
if self.data: # if self.data and:
return self.get_schema.get('type')[-1] # return self.get_schema.get('type')[-1]
return self.schema.type
return self.schema.name()
def description(self): def description(self):
if not self.data: if not self.data:

View File

@ -1,41 +1,78 @@
from django import forms from django import forms
from django.conf import settings
from oidc4vp.models import Organization
class Organization(forms.Form): # class OrganizationForm(forms.Form):
wallet = forms.ChoiceField( # wallet = forms.ChoiceField(
"Wallet", # "Wallet",
choices=[(x.id, x.name) for x in Organization.objects.all()] # choices=[(x.id, x.name) for x in Organization.objects.all()]
# )
# def clean_wallet(self):
# data = self.cleaned_data["wallet"]
# organization = Organization.objects.filter(
# id=data
# )
# if not organization.exists():
# raise ValidationError("organization is not valid!")
# self.organization = organization.first()
# return data
# def authorize(self):
# data = {
# "response_type": "vp_token",
# "response_mode": "direct_post",
# "client_id": self.organization.client_id,
# "response_uri": settings.RESPONSE_URI,
# "presentation_definition": self.pv_definition(),
# "nonce": ""
# }
# query_dict = QueryDict('', mutable=True)
# query_dict.update(data)
# url = '{response_uri}/authorize?{params}'.format(
# response_uri=self.organization.response_uri,
# params=query_dict.urlencode()
# )
# def pv_definition(self):
# return ""
class AuthorizeForm(forms.Form):
organization = forms.ChoiceField(choices=[])
def __init__(self, *args, **kwargs):
# import pdb; pdb.set_trace()
self.user = kwargs.pop('user', None)
self.presentation_definition = kwargs.pop('presentation_definition', [])
self.credentials = self.user.vcredentials.filter(
schema__type__in=self.presentation_definition
) )
super().__init__(*args, **kwargs)
self.fields['organization'].choices = [
(x.id, x.name) for x in Organization.objects.filter()
if x.response_uri != settings.RESPONSE_URI
]
def clean_wallet(self): def save(self, commit=True):
data = self.cleaned_data["wallet"] self.org = Organization.objects.filter(
organization = Organization.objects.filter( id=self.data['organization']
id=data
) )
if not self.org.exists():
return
if not organization.exists(): self.org = self.org[0]
raise ValidationError("organization is not valid!")
self.organization = organization.first() if commit:
url = self.org.demand_authorization()
if url.status_code == 200:
return url.json().get('redirect_uri')
return data return
def authorize(self):
data = {
"response_type": "vp_token",
"response_mode": "direct_post",
"client_id": self.organization.client_id,
"response_uri": settings.RESPONSE_URI,
"presentation_definition": self.pv_definition(),
"nonce": ""
}
query_dict = QueryDict('', mutable=True)
query_dict.update(data)
url = '{response_uri}/authorize?{params}'.format(
response_uri=self.organization.response_uri,
params=query_dict.urlencode()
)
def pv_definition(self):
return ""

View File

@ -11,9 +11,8 @@ from django.urls import reverse_lazy
from oidc4vp.models import Authorization, Organization from oidc4vp.models import Authorization, Organization
from idhub.mixins import UserView from idhub.mixins import UserView
from idhub.user.forms import ( from oidc4vp.forms import AuthorizeForm
DemandAuthorizationForm
)
# from django.core.mail import send_mail # from django.core.mail import send_mail
# from django.http import HttpResponse, HttpResponseRedirect # from django.http import HttpResponse, HttpResponseRedirect
@ -29,12 +28,15 @@ class AuthorizeView(UserView, FormView):
template_name = "credentials_presentation.html" template_name = "credentials_presentation.html"
subtitle = _('Credential presentation') subtitle = _('Credential presentation')
icon = 'bi bi-patch-check-fill' icon = 'bi bi-patch-check-fill'
form_class = DemandAuthorizationForm form_class = AuthorizeForm
success_url = reverse_lazy('idhub:user_demand_authorization') success_url = reverse_lazy('idhub:user_demand_authorization')
def get_form_kwargs(self): def get_form_kwargs(self):
kwargs = super().get_form_kwargs() kwargs = super().get_form_kwargs()
kwargs['user'] = self.request.user kwargs['user'] = self.request.user
vps = self.request.GET.get('presentation_definition')
# import pdb; pdb.set_trace()
kwargs['presentation_definition'] = json.loads(vps)
return kwargs return kwargs
def form_valid(self, form): def form_valid(self, form):
@ -77,7 +79,7 @@ class VerifyView(View):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
org = self.validate(request) org = self.validate(request)
import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
# # TODO: incorporate request.POST["presentation_submission"] as schema definition # # TODO: incorporate request.POST["presentation_submission"] as schema definition
# (presentation_valid, _) = verify_presentation(request.POST["vp_token"]) # (presentation_valid, _) = verify_presentation(request.POST["vp_token"])
# if not presentation_valid: # if not presentation_valid: