2023-11-28 08:39:02 +00:00
|
|
|
import requests
|
2023-10-10 08:52:04 +00:00
|
|
|
from django import forms
|
2023-11-28 08:39:02 +00:00
|
|
|
from django.conf import settings
|
2023-10-25 15:49:17 +00:00
|
|
|
from idhub_auth.models import User
|
2023-11-24 15:36:05 +00:00
|
|
|
from idhub.models import DID, VerificableCredential
|
|
|
|
from oidc4vp.models import Organization
|
2023-11-02 16:13:49 +00:00
|
|
|
|
2023-10-10 08:52:04 +00:00
|
|
|
|
2023-10-11 07:52:05 +00:00
|
|
|
class ProfileForm(forms.ModelForm):
|
2023-10-10 08:52:04 +00:00
|
|
|
MANDATORY_FIELDS = ['first_name', 'last_name', 'email']
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2023-11-02 13:17:07 +00:00
|
|
|
fields = ('first_name', 'last_name', 'email')
|
|
|
|
|
|
|
|
|
|
|
|
class RequestCredentialForm(forms.Form):
|
|
|
|
did = forms.ChoiceField(choices=[])
|
|
|
|
credential = forms.ChoiceField(choices=[])
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user', None)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['did'].choices = [
|
|
|
|
(x.did, x.label) for x in DID.objects.filter(user=self.user)
|
|
|
|
]
|
|
|
|
self.fields['credential'].choices = [
|
|
|
|
(x.id, x.type()) for x in VerificableCredential.objects.filter(
|
|
|
|
user=self.user,
|
|
|
|
status=VerificableCredential.Status.ENABLED
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
did = DID.objects.filter(
|
|
|
|
user=self.user,
|
|
|
|
did=self.data['did']
|
|
|
|
)
|
|
|
|
cred = VerificableCredential.objects.filter(
|
|
|
|
user=self.user,
|
2023-11-02 16:13:49 +00:00
|
|
|
id=self.data['credential'],
|
|
|
|
status=VerificableCredential.Status.ENABLED
|
2023-11-02 13:17:07 +00:00
|
|
|
)
|
|
|
|
if not all([cred.exists(), did.exists()]):
|
|
|
|
return
|
|
|
|
|
|
|
|
did = did[0].did
|
|
|
|
cred = cred[0]
|
2023-11-21 09:00:59 +00:00
|
|
|
try:
|
|
|
|
cred.issue(did)
|
|
|
|
except Exception:
|
|
|
|
return
|
2023-11-02 13:17:07 +00:00
|
|
|
|
|
|
|
if commit:
|
|
|
|
cred.save()
|
|
|
|
return cred
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2023-11-02 16:13:49 +00:00
|
|
|
|
2023-11-28 08:39:02 +00:00
|
|
|
class DemandAuthorizationForm(forms.Form):
|
|
|
|
organization = forms.ChoiceField(choices=[])
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user', None)
|
|
|
|
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 save(self, commit=True):
|
|
|
|
self.org = Organization.objects.filter(
|
|
|
|
id=self.data['organization']
|
|
|
|
)
|
|
|
|
if not self.org.exists():
|
|
|
|
return
|
|
|
|
|
|
|
|
self.org = self.org[0]
|
|
|
|
|
|
|
|
if commit:
|
|
|
|
url = self.org.demand_authorization()
|
|
|
|
auth = (self.org.client_id, self.org.client_secret)
|
|
|
|
# res = requests.get(url, auth=auth)
|
|
|
|
# import pdb; pdb.set_trace()
|
|
|
|
# if res.status == 200:
|
|
|
|
# return res.body
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
2023-11-02 16:13:49 +00:00
|
|
|
class CredentialPresentationForm(forms.Form):
|
2023-11-03 10:44:58 +00:00
|
|
|
organization = forms.ChoiceField(choices=[])
|
2023-11-28 08:39:02 +00:00
|
|
|
# credential = forms.ChoiceField(choices=[])
|
2023-11-02 16:13:49 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop('user', None)
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-11-03 10:44:58 +00:00
|
|
|
self.fields['organization'].choices = [
|
|
|
|
(x.id, x.name) for x in Organization.objects.filter()
|
|
|
|
]
|
2023-11-28 08:39:02 +00:00
|
|
|
# self.fields['credential'].choices = [
|
|
|
|
# (x.id, x.type()) for x in VerificableCredential.objects.filter(
|
|
|
|
# user=self.user,
|
|
|
|
# status=VerificableCredential.Status.ISSUED
|
|
|
|
# )
|
|
|
|
# ]
|
2023-11-02 16:13:49 +00:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
2023-11-09 16:58:06 +00:00
|
|
|
self.org = Organization.objects.filter(
|
2023-11-02 16:13:49 +00:00
|
|
|
id=self.data['organization']
|
|
|
|
)
|
2023-11-09 16:58:06 +00:00
|
|
|
self.cred = VerificableCredential.objects.filter(
|
2023-11-02 16:13:49 +00:00
|
|
|
user=self.user,
|
|
|
|
id=self.data['credential'],
|
|
|
|
status=VerificableCredential.Status.ISSUED
|
|
|
|
)
|
2023-11-09 16:58:06 +00:00
|
|
|
if not all([self.org.exists(), self.cred.exists()]):
|
2023-11-02 16:13:49 +00:00
|
|
|
return
|
|
|
|
|
2023-11-09 16:58:06 +00:00
|
|
|
self.org = self.org[0]
|
|
|
|
self.cred = self.cred[0]
|
2023-11-02 16:13:49 +00:00
|
|
|
|
|
|
|
if commit:
|
2023-11-09 16:58:06 +00:00
|
|
|
self.org.send(self.cred)
|
|
|
|
return self.cred
|
2023-11-02 16:13:49 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|