From 02d582427b092dee4a0550c607a6e2259e1f52e7 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 28 Jan 2025 18:15:35 +0100 Subject: [PATCH] command predetermined diddocument --- idhub/admin/forms.py | 38 ++++++++++---------- idhub/management/commands/demo_data.py | 48 ++++++++++++++++---------- idhub_auth/models.py | 5 +-- 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index 0ec7aee..24d9f17 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -36,16 +36,16 @@ class TermsConditionsForm2(forms.Form): if data.get("accept"): self.user.accept_gdpr = True else: - self.user.accept_gdpr = False + self.user.accept_gdpr = False return data - + def save(self, commit=True): if commit: self.user.save() return self.user - - return + + return class EncryptionKeyForm(forms.Form): @@ -80,8 +80,8 @@ class EncryptionKeyForm(forms.Form): did = DID.objects.create(label='Default', type=DID.Types.WEB) did.set_did() did.save() - - return + + return class TermsConditionsForm(forms.Form): @@ -131,16 +131,16 @@ class TermsConditionsForm(forms.Form): if privacy and legal and cookies: self.user.accept_gdpr = True else: - self.user.accept_gdpr = False + self.user.accept_gdpr = False return data - + def save(self, commit=True): if commit: self.user.save() return self.user - - return + + return class ImportForm(forms.Form): @@ -197,7 +197,7 @@ class ImportForm(forms.Form): eidas1=True, did=eidas1 ).first() - + return data def clean_schema(self): @@ -288,15 +288,15 @@ class ImportForm(forms.Form): def save(self, commit=True): table = [] for k, v in self.rows.items(): - table.append(self.create_credential(k, v)) + table.append(self.create_credential(k, v)) if commit: for cred in table: cred.save() File_datas.objects.create(file_name=self.file_name) return table - - return + + return def validate_jsonld(self, line, row): try: @@ -355,7 +355,7 @@ class ImportForm(forms.Form): class SchemaForm(forms.Form): file_template = forms.FileField(label=_("File template")) - + class MembershipForm(forms.ModelForm): class Meta: @@ -376,7 +376,7 @@ class MembershipForm(forms.ModelForm): if members.filter(start_date__lte=start_date, end_date=None).exists(): msg = _("This membership already exists!") raise forms.ValidationError(msg) - + if (start_date and end_date): if start_date > end_date: msg = _("The end date is less than the start date") @@ -399,8 +399,8 @@ class MembershipForm(forms.ModelForm): if members.exists(): msg = _("This membership already exists!") raise forms.ValidationError(msg) - - + + return end_date @@ -417,7 +417,7 @@ class UserRolForm(forms.ModelForm): choices = self.fields['service'].choices choices.queryset = choices.queryset.exclude(users__user=user) self.fields['service'].choices = choices - + def clean_service(self): data = super().clean() service = UserRol.objects.filter( diff --git a/idhub/management/commands/demo_data.py b/idhub/management/commands/demo_data.py index db68043..391c636 100644 --- a/idhub/management/commands/demo_data.py +++ b/idhub/management/commands/demo_data.py @@ -7,6 +7,7 @@ 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 django.core.cache import cache from django.urls import reverse from pyvckit.did import ( generate_did, @@ -28,25 +29,28 @@ class Command(BaseCommand): 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') + parser.add_argument('predefined_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: - for u in range(1, 6): - user = 'user{}@example.org'.format(u) - self.create_users(user, '1234') + cache.set("KEY_DIDS", "1", None) self.org = Organization.objects.create( name=self.DOMAIN, domain=self.DOMAIN, main=True ) + self.org.set_encrypted_sensitive_data() + self.org.save() + + self.create_admin_users(ADMIN_EMAIL, ADMIN_PASSWORD) + if settings.CREATE_TEST_USERS: + for u in range(1, 6): + user = 'user{}@example.org'.format(u) + self.create_users(user, '1234') if self.OIDC_ORGS: self.create_organizations() @@ -57,23 +61,30 @@ class Command(BaseCommand): su = User.objects.create_superuser(email=email, password=password) su.save() - tk = Token.objects.filter(token=self.predefined_token).first() - if self.predefined_token and not tk: - Token.objects.create(token=self.predefined_token) + if self.predefined_token: + tk = Token.objects.filter(token=self.predefined_token).first() + if not tk: + Token.objects.create(token=self.predefined_token) - self.create_default_did(su, password) + self.create_default_did() + + def create_default_did(self): + + # did = DID.objects.create(label='Default', type=DID.Types.WEB) + # did.set_did() + # did.save() + # return - def create_default_did(self, admin, password): - # import pdb; pdb.set_trace() fdid = self.open_example_did() if not fdid: return - did = DID() + did = DID(type=DID.Types.WEB) 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: @@ -91,20 +102,19 @@ class Command(BaseCommand): did.did = generate_did(new_key_material, url) key = json.loads(new_key_material) - url, did.didweb_document = gen_did_document(self.did, key) + url, did.didweb_document = gen_did_document(did.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 + if self.predefined_did: + didweb_path = self.predefined_did data = '' - with didweb_path.open() as _file: + with open(didweb_path) as _file: try: data = json.loads(_file.read()) except Exception: diff --git a/idhub_auth/models.py b/idhub_auth/models.py index 0552b35..2a6becd 100644 --- a/idhub_auth/models.py +++ b/idhub_auth/models.py @@ -125,7 +125,7 @@ class User(AbstractBaseUser): sb = secret.SecretBox(sb_key) if not isinstance(data, bytes): data = data.encode('utf-8') - + return base64.b64encode(sb.encrypt(data)).decode('utf-8') def get_salt(self): @@ -151,6 +151,7 @@ class User(AbstractBaseUser): return base64.b64encode(value_enc).decode('utf-8') def decrypt_data(self, data): + import pdb; pdb.set_trace() pw = self.decrypt_sensitive_data().encode('utf-8') sb = self.get_secret_box(pw) value = base64.b64decode(data.encode('utf-8')) @@ -166,6 +167,6 @@ class User(AbstractBaseUser): sb = secret.SecretBox(sb_key) if not isinstance(data, bytes): data = data.encode('utf-8') - + encrypted_data = base64.b64encode(sb.encrypt(data)).decode('utf-8') self.encrypted_sensitive_data = encrypted_data