add oidc4vp module

This commit is contained in:
Cayo Puigdefabregas 2023-11-24 16:36:05 +01:00
parent b279ab94d6
commit 7db6d1f4e3
12 changed files with 152 additions and 20 deletions

View File

@ -5,7 +5,7 @@ from pathlib import Path
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from decouple import config from decouple import config
from idhub.models import Organization from oidc4vp.models import Organization
User = get_user_model() User = get_user_model()

View File

@ -1,6 +1,5 @@
import json import json
import pytz import pytz
import requests
import datetime import datetime
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
@ -639,18 +638,3 @@ class UserRol(models.Model):
class Meta: class Meta:
unique_together = ('user', 'service',) unique_together = ('user', 'service',)
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

@ -1,7 +1,7 @@
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, Organization from idhub.models import DID, VerificableCredential
from oidc4vp.models import Organization
class ProfileForm(forms.ModelForm): class ProfileForm(forms.ModelForm):
@ -56,7 +56,6 @@ class RequestCredentialForm(forms.Form):
return return
class CredentialPresentationForm(forms.Form): class CredentialPresentationForm(forms.Form):
organization = forms.ChoiceField(choices=[]) organization = forms.ChoiceField(choices=[])
credential = forms.ChoiceField(choices=[]) credential = forms.ChoiceField(choices=[])

0
oidc4vp/__init__.py Normal file
View File

3
oidc4vp/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
oidc4vp/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class Oidc4VpConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'oidc4vp'

41
oidc4vp/forms.py Normal file
View File

@ -0,0 +1,41 @@
from django import forms
class Organization(forms.Form):
wallet = forms.ChoiceField(
"Wallet",
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 ""

View File

77
oidc4vp/models.py Normal file
View File

@ -0,0 +1,77 @@
import requests
from django.db import models
from django.http import QueryDict
from django.utils.translation import gettext_lazy as _
from idhub_auth.models import User
class Organization(models.Model):
name = models.CharField(max_length=250)
client_id = models.CharField()
client_secret = models.CharField()
response_uri = models.URLField(
help_text=_("Url where to send the presentation"),
max_length=250
)
def __str__(self):
return self.name
def send(self, vcred):
return requests.post(self.url, data=vcred)
class Authorization(models.Model):
created = models.DateTimeField(auto_now=True)
presentation_definition = models.CharField()
organization = models.ForeignKey(
Organization,
on_delete=models.CASCADE,
related_name='vp_tokens',
null=True,
)
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
null=True,
)
def authorize(self):
response_uri = self.__class__.objects.filter(
response_uri=settings.RESPONSE_URI
)
data = {
"response_type": "vp_token",
"response_mode": "direct_post",
"client_id": "...",
"response_uri": response_uri,
"presentation_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()
)
class OAuth2VPToken(models.Model):
created = models.DateTimeField(auto_now=True)
response_code = models.CharField()
result_verify = models.BooleanField()
presentation_definition = models.CharField()
organization = models.ForeignKey(
Organization,
on_delete=models.CASCADE,
related_name='vp_tokens',
null=True,
)
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
related_name='vp_tokens',
null=True,
)

3
oidc4vp/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

17
oidc4vp/views.py Normal file
View File

@ -0,0 +1,17 @@
from django.shortcuts import render
class PeopleEditView(People, FormView):
template_name = "idhub/admin/user_edit.html"
form_class = ProfileForm
success_url = reverse_lazy('idhub:admin_people_list')
def form_valid(self, form):
user = form.save()
messages.success(self.request, _('The credential was sended successfully'))
# Event.set_EV_USR_UPDATED_BY_ADMIN(user)
# Event.set_EV_USR_UPDATED(user)
return super().form_valid(form)

View File

@ -71,6 +71,7 @@ INSTALLED_APPS = [
'django_extensions', 'django_extensions',
'django_bootstrap5', 'django_bootstrap5',
'idhub_auth', 'idhub_auth',
'oidc4vp',
'idhub' 'idhub'
] ]
@ -183,3 +184,4 @@ USE_I18N = True
USE_L10N = True USE_L10N = True
AUTH_USER_MODEL = 'idhub_auth.User' AUTH_USER_MODEL = 'idhub_auth.User'
RESPONSE_URI = config('RESPONSE_URI', default="")