add create_default_did in initial_data

This commit is contained in:
Cayo Puigdefabregas 2025-01-27 20:34:24 +01:00 committed by pedro
parent 8b3f4704e7
commit b6162e2491

View file

@ -7,7 +7,13 @@ from utils import credtools
from django.conf import settings
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from idhub.models import Schemas
from django.urls import reverse
from pyvckit.did import (
generate_did,
gen_did_document,
)
from idhub.models import Schemas, DID
from oidc4vp.models import Organization
from webhook.models import Token
@ -16,17 +22,19 @@ User = get_user_model()
class Command(BaseCommand):
help = "Insert minimum datas for the project"
help = "Insert minimum data for the project"
DOMAIN = settings.DOMAIN
OIDC_ORGS = settings.OIDC_ORGS
def add_arguments(self, parser):
parser.add_argument('predefined_token', nargs='?', default='', type=str, help='predefined token')
parser.add_argument('example_did', nargs='?', default='', type=str, help='predefined did')
def handle(self, *args, **kwargs):
ADMIN_EMAIL = settings.INITIAL_ADMIN_EMAIL
ADMIN_PASSWORD = settings.INITIAL_ADMIN_PASSWORD
self.predefined_token = kwargs['predefined_token']
self.predefined_did = kwargs['predefined_did']
self.create_admin_users(ADMIN_EMAIL, ADMIN_PASSWORD)
if settings.CREATE_TEST_USERS:
@ -53,6 +61,57 @@ class Command(BaseCommand):
if self.predefined_token and not tk:
Token.objects.create(token=self.predefined_token)
self.create_default_did(su, password)
def create_default_did(self, admin, password):
# import pdb; pdb.set_trace()
fdid = self.open_example_did()
if not fdid:
return
did = DID()
new_key_material = fdid.get("key_material", "")
label = fdid.get("label", "")
if not new_key_material:
return
did.set_key_material(new_key_material)
if label:
did.label = label
if did.type == did.Types.KEY:
did.did = generate_did(new_key_material)
elif did.type == did.Types.WEB:
url = "https://{}".format(settings.DOMAIN)
path = reverse("idhub:serve_did", args=["a"])
if path:
path = path.split("/a/did.json")[0]
url = "https://{}/{}".format(settings.DOMAIN, path)
did.did = generate_did(new_key_material, url)
key = json.loads(new_key_material)
url, did.didweb_document = gen_did_document(self.did, key)
did.save()
def open_example_did(self):
# import pdb; pdb.set_trace()
BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent
didweb_path = os.path.join(BASE_DIR, "examples", "keys_did.json")
if self.predefined_web:
didweb_path = self.predefined_web
data = ''
with didweb_path.open() as _file:
try:
data = json.loads(_file.read())
except Exception:
pass
return data
def create_users(self, email, password):
u = User.objects.create(email=email, password=password)
u.set_password(password)