2023-11-24 15:36:05 +00:00
|
|
|
import requests
|
2023-11-24 16:53:43 +00:00
|
|
|
import secrets
|
2023-11-24 15:36:05 +00:00
|
|
|
|
2023-11-24 16:53:43 +00:00
|
|
|
from django.conf import settings
|
2023-11-24 15:36:05 +00:00
|
|
|
from django.http import QueryDict
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from idhub_auth.models import User
|
2023-11-24 16:53:43 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
|
|
|
|
SALT_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
|
|
|
|
|
|
|
|
def gen_salt(length: int) -> str:
|
|
|
|
"""Generate a random string of SALT_CHARS with specified ``length``."""
|
|
|
|
if length <= 0:
|
|
|
|
raise ValueError("Salt length must be positive")
|
|
|
|
|
|
|
|
return "".join(secrets.choice(SALT_CHARS) for _ in range(length))
|
|
|
|
|
|
|
|
|
|
|
|
def set_client_id():
|
|
|
|
return gen_salt(24)
|
|
|
|
|
|
|
|
|
|
|
|
def set_client_secret():
|
|
|
|
return gen_salt(48)
|
|
|
|
|
|
|
|
|
|
|
|
def set_code():
|
|
|
|
return gen_salt(24)
|
2023-11-24 15:36:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Organization(models.Model):
|
2023-11-24 16:53:43 +00:00
|
|
|
"""
|
|
|
|
This class represent a member of one net trust or federated host
|
|
|
|
"""
|
2023-11-24 15:36:05 +00:00
|
|
|
name = models.CharField(max_length=250)
|
2023-11-24 17:10:43 +00:00
|
|
|
client_id = models.CharField(
|
|
|
|
max_length=24,
|
|
|
|
default=set_client_id,
|
|
|
|
unique=True
|
|
|
|
)
|
|
|
|
client_secret = models.CharField(
|
|
|
|
max_length=48,
|
|
|
|
default=set_client_secret
|
|
|
|
)
|
2023-11-24 15:36:05 +00:00
|
|
|
response_uri = models.URLField(
|
2023-11-24 16:53:43 +00:00
|
|
|
help_text=_("Url where to send the verificable presentation"),
|
2023-11-24 15:36:05 +00:00
|
|
|
max_length=250
|
|
|
|
)
|
|
|
|
|
2023-11-24 16:53:43 +00:00
|
|
|
def send(self, vp):
|
|
|
|
"""
|
|
|
|
Send the verificable presentation to Verifier
|
|
|
|
"""
|
|
|
|
org = Organization.objects.get(
|
|
|
|
response_uri=settings.RESPONSE_URI
|
|
|
|
)
|
|
|
|
auth = (org.client_id, org.client_secret)
|
|
|
|
return requests.post(self.url, data=vp, auth=auth)
|
|
|
|
|
2023-11-24 15:36:05 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2023-11-24 16:53:43 +00:00
|
|
|
|
|
|
|
###################
|
|
|
|
# Verifier clases #
|
|
|
|
###################
|
2023-11-24 15:36:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Authorization(models.Model):
|
2023-11-24 16:53:43 +00:00
|
|
|
"""
|
|
|
|
This class represent a query through browser the client to the wallet.
|
|
|
|
The Verifier need to do a redirection to the user to Wallet.
|
|
|
|
The code we use as a soft foreing key between Authorization and OAuth2VPToken.
|
|
|
|
"""
|
|
|
|
code = models.CharField(max_length=24, default=set_code)
|
2023-11-24 15:36:05 +00:00
|
|
|
created = models.DateTimeField(auto_now=True)
|
2023-11-24 16:53:43 +00:00
|
|
|
presentation_definition = models.CharField(max_length=250)
|
2023-11-24 15:36:05 +00:00
|
|
|
organization = models.ForeignKey(
|
|
|
|
Organization,
|
|
|
|
on_delete=models.CASCADE,
|
2023-11-24 16:53:43 +00:00
|
|
|
related_name='authorizations',
|
2023-11-24 15:36:05 +00:00
|
|
|
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",
|
2023-11-24 16:53:43 +00:00
|
|
|
"client_id": self.organization.client_id,
|
2023-11-24 15:36:05 +00:00
|
|
|
"response_uri": response_uri,
|
|
|
|
"presentation_definition": "...",
|
2023-11-24 16:53:43 +00:00
|
|
|
"nonce": gen_salt(5),
|
2023-11-24 15:36:05 +00:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
)
|
2023-11-24 16:53:43 +00:00
|
|
|
return url
|
|
|
|
|
2023-11-24 15:36:05 +00:00
|
|
|
|
|
|
|
class OAuth2VPToken(models.Model):
|
2023-11-24 16:53:43 +00:00
|
|
|
"""
|
|
|
|
This class represent the response of Wallet to Verifier
|
|
|
|
and the result of verify.
|
|
|
|
"""
|
2023-11-24 15:36:05 +00:00
|
|
|
created = models.DateTimeField(auto_now=True)
|
2023-11-24 16:53:43 +00:00
|
|
|
code = models.CharField(max_length=250)
|
|
|
|
result_verify = models.BooleanField(max_length=250)
|
|
|
|
presentation_definition = models.CharField(max_length=250)
|
2023-11-24 15:36:05 +00:00
|
|
|
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,
|
|
|
|
)
|
2023-11-24 16:53:43 +00:00
|
|
|
authorization = models.ForeignKey(
|
|
|
|
Authorization,
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
null=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
def verifing(self):
|
|
|
|
pass
|
2023-11-24 15:36:05 +00:00
|
|
|
|