2023-11-24 15:36:05 +00:00
|
|
|
from django import forms
|
2023-11-29 16:29:31 +00:00
|
|
|
from django.conf import settings
|
2023-11-24 15:36:05 +00:00
|
|
|
|
2023-11-29 16:29:31 +00:00
|
|
|
from oidc4vp.models import Organization
|
2023-11-24 15:36:05 +00:00
|
|
|
|
|
|
|
|
2023-11-29 16:29:31 +00:00
|
|
|
# class OrganizationForm(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
|
|
|
|
# )
|
2023-11-24 15:36:05 +00:00
|
|
|
|
2023-11-29 16:29:31 +00:00
|
|
|
# if not organization.exists():
|
|
|
|
# raise ValidationError("organization is not valid!")
|
2023-11-24 15:36:05 +00:00
|
|
|
|
2023-11-29 16:29:31 +00:00
|
|
|
# self.organization = organization.first()
|
2023-11-24 15:36:05 +00:00
|
|
|
|
2023-11-29 16:29:31 +00:00
|
|
|
# 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):
|
2023-12-01 16:10:21 +00:00
|
|
|
# organization = forms.ChoiceField(choices=[])
|
2023-11-29 16:29:31 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# import pdb; pdb.set_trace()
|
2023-12-01 16:10:21 +00:00
|
|
|
self.data = kwargs.get('data', {}).copy()
|
2023-11-29 16:29:31 +00:00
|
|
|
self.user = kwargs.pop('user', None)
|
|
|
|
self.presentation_definition = kwargs.pop('presentation_definition', [])
|
2023-12-01 16:10:21 +00:00
|
|
|
|
|
|
|
reg = r'({})'.format('|'.join(self.presentation_definition))
|
|
|
|
|
2023-11-29 16:29:31 +00:00
|
|
|
self.credentials = self.user.vcredentials.filter(
|
2023-12-01 16:10:21 +00:00
|
|
|
schema__type__iregex=reg
|
2023-11-29 16:29:31 +00:00
|
|
|
)
|
|
|
|
super().__init__(*args, **kwargs)
|
2023-12-01 16:10:21 +00:00
|
|
|
for vp in self.presentation_definition:
|
|
|
|
vp = vp.lower()
|
|
|
|
choices = [
|
|
|
|
(str(x.id), x.schema.type.lower()) for x in self.credentials.filter(
|
|
|
|
schema__type__iexact=vp)
|
|
|
|
]
|
|
|
|
self.fields[vp.lower()] = forms.ChoiceField(
|
|
|
|
widget=forms.RadioSelect,
|
|
|
|
choices=choices
|
|
|
|
)
|
2023-11-29 16:29:31 +00:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
2023-12-01 16:10:21 +00:00
|
|
|
# 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()
|
|
|
|
# if url.status_code == 200:
|
|
|
|
# return url.json().get('redirect_uri')
|
2023-11-29 16:29:31 +00:00
|
|
|
|
|
|
|
return
|
2023-11-24 15:36:05 +00:00
|
|
|
|