command predetermined diddocument

This commit is contained in:
Cayo Puigdefabregas 2025-01-28 18:15:35 +01:00
parent dd8077a3c1
commit 04e9db01a5
3 changed files with 51 additions and 40 deletions

View file

@ -36,16 +36,16 @@ class TermsConditionsForm2(forms.Form):
if data.get("accept"): if data.get("accept"):
self.user.accept_gdpr = True self.user.accept_gdpr = True
else: else:
self.user.accept_gdpr = False self.user.accept_gdpr = False
return data return data
def save(self, commit=True): def save(self, commit=True):
if commit: if commit:
self.user.save() self.user.save()
return self.user return self.user
return return
class EncryptionKeyForm(forms.Form): class EncryptionKeyForm(forms.Form):
@ -80,8 +80,8 @@ class EncryptionKeyForm(forms.Form):
did = DID.objects.create(label='Default', type=DID.Types.WEB) did = DID.objects.create(label='Default', type=DID.Types.WEB)
did.set_did() did.set_did()
did.save() did.save()
return return
class TermsConditionsForm(forms.Form): class TermsConditionsForm(forms.Form):
@ -131,16 +131,16 @@ class TermsConditionsForm(forms.Form):
if privacy and legal and cookies: if privacy and legal and cookies:
self.user.accept_gdpr = True self.user.accept_gdpr = True
else: else:
self.user.accept_gdpr = False self.user.accept_gdpr = False
return data return data
def save(self, commit=True): def save(self, commit=True):
if commit: if commit:
self.user.save() self.user.save()
return self.user return self.user
return return
class ImportForm(forms.Form): class ImportForm(forms.Form):
@ -197,7 +197,7 @@ class ImportForm(forms.Form):
eidas1=True, eidas1=True,
did=eidas1 did=eidas1
).first() ).first()
return data return data
def clean_schema(self): def clean_schema(self):
@ -288,15 +288,15 @@ class ImportForm(forms.Form):
def save(self, commit=True): def save(self, commit=True):
table = [] table = []
for k, v in self.rows.items(): for k, v in self.rows.items():
table.append(self.create_credential(k, v)) table.append(self.create_credential(k, v))
if commit: if commit:
for cred in table: for cred in table:
cred.save() cred.save()
File_datas.objects.create(file_name=self.file_name) File_datas.objects.create(file_name=self.file_name)
return table return table
return return
def validate_jsonld(self, line, row): def validate_jsonld(self, line, row):
try: try:
@ -355,7 +355,7 @@ class ImportForm(forms.Form):
class SchemaForm(forms.Form): class SchemaForm(forms.Form):
file_template = forms.FileField(label=_("File template")) file_template = forms.FileField(label=_("File template"))
class MembershipForm(forms.ModelForm): class MembershipForm(forms.ModelForm):
class Meta: class Meta:
@ -376,7 +376,7 @@ class MembershipForm(forms.ModelForm):
if members.filter(start_date__lte=start_date, end_date=None).exists(): if members.filter(start_date__lte=start_date, end_date=None).exists():
msg = _("This membership already exists!") msg = _("This membership already exists!")
raise forms.ValidationError(msg) raise forms.ValidationError(msg)
if (start_date and end_date): if (start_date and end_date):
if start_date > end_date: if start_date > end_date:
msg = _("The end date is less than the start date") msg = _("The end date is less than the start date")
@ -399,8 +399,8 @@ class MembershipForm(forms.ModelForm):
if members.exists(): if members.exists():
msg = _("This membership already exists!") msg = _("This membership already exists!")
raise forms.ValidationError(msg) raise forms.ValidationError(msg)
return end_date return end_date
@ -417,7 +417,7 @@ class UserRolForm(forms.ModelForm):
choices = self.fields['service'].choices choices = self.fields['service'].choices
choices.queryset = choices.queryset.exclude(users__user=user) choices.queryset = choices.queryset.exclude(users__user=user)
self.fields['service'].choices = choices self.fields['service'].choices = choices
def clean_service(self): def clean_service(self):
data = super().clean() data = super().clean()
service = UserRol.objects.filter( service = UserRol.objects.filter(

View file

@ -7,6 +7,7 @@ from utils import credtools
from django.conf import settings from django.conf import settings
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.urls import reverse from django.urls import reverse
from pyvckit.did import ( from pyvckit.did import (
generate_did, generate_did,
@ -28,25 +29,28 @@ class Command(BaseCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('predefined_token', nargs='?', default='', type=str, help='predefined token') 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): def handle(self, *args, **kwargs):
ADMIN_EMAIL = settings.INITIAL_ADMIN_EMAIL ADMIN_EMAIL = settings.INITIAL_ADMIN_EMAIL
ADMIN_PASSWORD = settings.INITIAL_ADMIN_PASSWORD ADMIN_PASSWORD = settings.INITIAL_ADMIN_PASSWORD
self.predefined_token = kwargs['predefined_token'] self.predefined_token = kwargs['predefined_token']
self.predefined_did = kwargs['predefined_did'] self.predefined_did = kwargs['predefined_did']
cache.set("KEY_DIDS", "1", None)
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')
self.org = Organization.objects.create( self.org = Organization.objects.create(
name=self.DOMAIN, name=self.DOMAIN,
domain=self.DOMAIN, domain=self.DOMAIN,
main=True 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: if self.OIDC_ORGS:
self.create_organizations() self.create_organizations()
@ -57,23 +61,30 @@ class Command(BaseCommand):
su = User.objects.create_superuser(email=email, password=password) su = User.objects.create_superuser(email=email, password=password)
su.save() su.save()
tk = Token.objects.filter(token=self.predefined_token).first() if self.predefined_token:
if self.predefined_token and not tk: tk = Token.objects.filter(token=self.predefined_token).first()
Token.objects.create(token=self.predefined_token) 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() fdid = self.open_example_did()
if not fdid: if not fdid:
return return
did = DID() did = DID(type=DID.Types.WEB)
new_key_material = fdid.get("key_material", "") new_key_material = fdid.get("key_material", "")
label = fdid.get("label", "") label = fdid.get("label", "")
if not new_key_material: if not new_key_material:
return return
did.set_key_material(new_key_material) did.set_key_material(new_key_material)
if label: if label:
@ -91,20 +102,19 @@ class Command(BaseCommand):
did.did = generate_did(new_key_material, url) did.did = generate_did(new_key_material, url)
key = json.loads(new_key_material) 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() did.save()
def open_example_did(self): def open_example_did(self):
# import pdb; pdb.set_trace()
BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent
didweb_path = os.path.join(BASE_DIR, "examples", "keys_did.json") didweb_path = os.path.join(BASE_DIR, "examples", "keys_did.json")
if self.predefined_web: if self.predefined_did:
didweb_path = self.predefined_web didweb_path = self.predefined_did
data = '' data = ''
with didweb_path.open() as _file: with open(didweb_path) as _file:
try: try:
data = json.loads(_file.read()) data = json.loads(_file.read())
except Exception: except Exception:

View file

@ -125,7 +125,7 @@ class User(AbstractBaseUser):
sb = secret.SecretBox(sb_key) sb = secret.SecretBox(sb_key)
if not isinstance(data, bytes): if not isinstance(data, bytes):
data = data.encode('utf-8') data = data.encode('utf-8')
return base64.b64encode(sb.encrypt(data)).decode('utf-8') return base64.b64encode(sb.encrypt(data)).decode('utf-8')
def get_salt(self): def get_salt(self):
@ -151,6 +151,7 @@ class User(AbstractBaseUser):
return base64.b64encode(value_enc).decode('utf-8') return base64.b64encode(value_enc).decode('utf-8')
def decrypt_data(self, data): def decrypt_data(self, data):
import pdb; pdb.set_trace()
pw = self.decrypt_sensitive_data().encode('utf-8') pw = self.decrypt_sensitive_data().encode('utf-8')
sb = self.get_secret_box(pw) sb = self.get_secret_box(pw)
value = base64.b64decode(data.encode('utf-8')) value = base64.b64decode(data.encode('utf-8'))
@ -166,6 +167,6 @@ class User(AbstractBaseUser):
sb = secret.SecretBox(sb_key) sb = secret.SecretBox(sb_key)
if not isinstance(data, bytes): if not isinstance(data, bytes):
data = data.encode('utf-8') data = data.encode('utf-8')
encrypted_data = base64.b64encode(sb.encrypt(data)).decode('utf-8') encrypted_data = base64.b64encode(sb.encrypt(data)).decode('utf-8')
self.encrypted_sensitive_data = encrypted_data self.encrypted_sensitive_data = encrypted_data