IdHub/idhub/management/commands/initial_datas.py

119 lines
4 KiB
Python
Raw Normal View History

2023-11-07 16:43:23 +00:00
import os
import csv
2023-12-12 17:00:21 +00:00
import json
2023-11-07 16:43:23 +00:00
from pathlib import Path
2023-12-12 17:00:21 +00:00
from utils import credtools
from django.conf import settings
2023-11-03 12:29:15 +00:00
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth import get_user_model
2024-01-04 18:17:18 +00:00
from django.core.cache import cache
2023-11-07 16:43:23 +00:00
from decouple import config
2023-12-12 17:00:21 +00:00
from idhub.models import DID, Schemas
2023-11-24 15:36:05 +00:00
from oidc4vp.models import Organization
2023-12-11 12:55:56 +00:00
from promotion.models import Promotion
2023-11-03 12:29:15 +00:00
User = get_user_model()
class Command(BaseCommand):
help = "Insert minimum datas for the project"
def handle(self, *args, **kwargs):
2023-11-07 16:43:23 +00:00
ADMIN_EMAIL = config('ADMIN_EMAIL', 'admin@example.org')
ADMIN_PASSWORD = config('ADMIN_PASSWORD', '1234')
USER_EMAIL = config('USER_EMAIL', 'user1@example.org')
USER_PASSWORD = config('USER_PASSWORD', '1234')
self.create_admin_users(ADMIN_EMAIL, ADMIN_PASSWORD)
self.create_users(USER_EMAIL, USER_PASSWORD)
BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent
ORGANIZATION = os.path.join(BASE_DIR, settings.ORG_FILE)
2023-11-07 16:43:23 +00:00
with open(ORGANIZATION, newline='\n') as csvfile:
f = csv.reader(csvfile, delimiter=';', quotechar='"')
for r in f:
self.create_organizations(r[0].strip(), r[1].strip())
if settings.SYNC_ORG_DEV == 'y':
self.sync_credentials_organizations("pangea.org", "somconnexio.coop")
self.sync_credentials_organizations("local 8000", "local 9000")
2023-12-12 17:00:21 +00:00
self.create_schemas()
2023-11-03 12:29:15 +00:00
def create_admin_users(self, email, password):
2024-01-04 15:27:27 +00:00
su = User.objects.create_superuser(email=email, password=password)
su.set_encrypted_sensitive_data(password)
su.save()
2024-01-04 18:17:18 +00:00
key = su.decrypt_sensitive_data(password)
key_dids = {su.id: key}
cache.set("KEY_DIDS", key_dids, None)
2024-01-06 18:18:59 +00:00
self.create_defaults_dids(su, key)
2023-11-03 12:29:15 +00:00
def create_users(self, email, password):
2024-01-04 15:27:27 +00:00
u = User.objects.create(email=email, password=password)
2023-11-03 12:29:15 +00:00
u.set_password(password)
2024-01-04 15:27:27 +00:00
u.set_encrypted_sensitive_data(password)
2023-11-03 12:29:15 +00:00
u.save()
2024-01-04 18:17:18 +00:00
key = u.decrypt_sensitive_data(password)
2024-01-06 18:18:59 +00:00
self.create_defaults_dids(u, key)
2023-11-03 12:29:15 +00:00
def create_organizations(self, name, url):
2023-11-28 08:39:02 +00:00
Organization.objects.create(name=name, response_uri=url)
2023-12-11 12:55:56 +00:00
2023-12-11 19:06:53 +00:00
def sync_credentials_organizations(self, test1, test2):
org1 = Organization.objects.get(name=test1)
org2 = Organization.objects.get(name=test2)
2023-12-11 12:55:56 +00:00
org2.my_client_id = org1.client_id
org2.my_client_secret = org1.client_secret
org1.my_client_id = org2.client_id
org1.my_client_secret = org2.client_secret
2023-12-11 14:38:16 +00:00
org1.save()
org2.save()
2024-01-06 18:18:59 +00:00
def create_defaults_dids(self, u, password):
2024-01-17 13:11:47 +00:00
did = DID(label="Default", user=u, type=DID.Types.KEY)
2024-01-06 18:18:59 +00:00
did.set_did(password)
did.save()
2023-12-12 17:00:21 +00:00
def create_schemas(self):
schemas_files = os.listdir(settings.SCHEMAS_DIR)
schemas = [x for x in schemas_files
if not Schemas.objects.filter(file_schema=x).exists()]
for x in schemas_files:
if Schemas.objects.filter(file_schema=x).exists():
continue
self._create_schemas(x)
def _create_schemas(self, file_name):
data = self.open_file(file_name)
try:
ldata = json.loads(data)
assert credtools.validate_schema(ldata)
2024-01-15 18:10:55 +00:00
dname = ldata.get('name')
2024-01-21 12:46:01 +00:00
title = ldata.get('title')
2024-01-15 18:10:55 +00:00
assert dname
2024-01-21 12:46:01 +00:00
assert title
2023-12-12 17:00:21 +00:00
except Exception:
2024-01-21 12:46:01 +00:00
title = ''
2024-01-22 17:13:06 +00:00
_name = ''
_name = json.dumps(ldata.get('name', ''))
_description = json.dumps(ldata.get('description', ''))
Schemas.objects.create(
file_schema=file_name,
data=data,
type=title,
_name=_name,
_description=_description
)
2023-12-12 17:00:21 +00:00
def open_file(self, file_name):
data = ''
filename = Path(settings.SCHEMAS_DIR).joinpath(file_name)
with filename.open() as schema_file:
data = schema_file.read()
return data