get token from api dlt

This commit is contained in:
Cayo Puigdefabregas 2024-06-21 12:01:55 +02:00
parent fd8f404908
commit b960c87d83
4 changed files with 36 additions and 17 deletions

View File

@ -2,6 +2,6 @@
"@context": {
"legalName": "https://idhub.pangea.org/context/#legalName",
"allowedSchemas": "https://idhub.pangea.org/context/#allowedSchemas",
"email": "https://idhub.pangea.org/context/#email"
"domain": "https://idhub.pangea.org/context/#domain"
}
}

View File

@ -446,6 +446,7 @@ class DID(models.Model):
key_material = models.TextField()
ether_address = models.CharField(max_length=250, null=True)
ether_privkey = models.CharField(max_length=250, null=True)
api_token = models.CharField(max_length=250, null=True)
eidas1 = models.BooleanField(default=False)
user = models.ForeignKey(
User,
@ -464,8 +465,7 @@ class DID(models.Model):
return False
def get_key_material(self):
user = self.user or self.get_organization()
return user.decrypt_data(self.key_material)
self.decrypt_data(self.key_material)
def set_key_material(self, value):
self.key_material = self.encrypt_data(value)
@ -507,6 +507,9 @@ class DID(models.Model):
return Organization.objects.get(main=True)
def set_ether_address(self):
if self.ether_address:
return
priv, self.ether_address = generate_ether_address()
self.ether_privkey = self.encrypt_data(priv)
@ -517,20 +520,27 @@ class DID(models.Model):
user.save()
return user.encrypt_data(value)
def send_credential_as_issuer_to_TA(self):
def decrypt_data(self, value):
user = self.user or self.get_organization()
return user.decrypt_data(value)
def send_api(self, data, token=settings.TOKEN_TA_API):
url = settings.VERIFIABLE_REGISTER_URL
token = settings.TOKEN_TA_API
if not url or not token:
return
headers = {"Bearer {}".format(token)}
headers = {"Authenticate": "Bearer {}".format(token)}
credential = self._render_credential_issuer()
response = requests.post(url=url, data=credential, headers=headers)
response = requests.post(url=url, data=value, headers=headers)
if response.status_code >= 300:
return
self.credential_as_issuer = response.text
return response.json()
def send_credential_as_issuer_to_TA(self):
credential = self._render_credential_issuer()
response = self.send_api(credential)
self.credential_as_issuer = json.dumps(response)
def get_context(self):
format = "%Y-%m-%dT%H:%M:%SZ"
@ -548,7 +558,7 @@ class DID(models.Model):
"subject_did": self.did,
"legalName": org.name or "",
"allowedSchemas": allow_schemas,
"email": self.user.email,
"domain": self.org.domain,
"credential_status_id": credential_status_id,
}
return context
@ -562,6 +572,16 @@ class DID(models.Model):
return ujson.dumps(credential)
def get_api_token(self):
if self.api_token:
return self.decrypt_data(self.api_token)
priv = self.decrypt_data(self.ether_privkey)
response = self.send_api(priv)
if not response:
return
self.api_token = self.encrypt_data(response)
class Schemas(models.Model):

View File

@ -49,7 +49,7 @@
"id": "{{ subject_did }}",
"legalName": "{{ legalName }}",
"allowedSchemas": "{{ allowedSchemas }}",
"email": "{{ email }}"
"domain": "{{ domain }}"
},
"credentialStatus": {
"id": "{{ credential_status_id }}",

View File

@ -30,10 +30,9 @@
"type": "string",
"minLength": 1
},
"email": {
"domain": {
"type": "string",
"format": "email",
"pattern": "^\\S+@\\S+\\.\\S+$"
"minLength": 1
},
"allowedSchemas": {
"description": "List of schemas",