From 64015d9b5b5fa2c069ef8f218ea4ab9f87159f52 Mon Sep 17 00:00:00 2001 From: 3wc <3wc.git@doesthisthing.work> Date: Wed, 7 Apr 2021 17:06:40 +0200 Subject: [PATCH] Remove VAPIDKeyset.public_key, generate on demand Also rename `gen_vapid_keys` to `gen_vapid_key`, and merge migrations --- djangoldp_webpushnotification/admin.py | 3 +++ .../{gen_vapid_keys.py => gen_vapid_key.py} | 2 -- .../migrations/0001_initial.py | 21 +++++++++++++++++++ .../migrations/0012_vapidkeyset.py | 19 ----------------- djangoldp_webpushnotification/models.py | 20 +++++++++++++----- 5 files changed, 39 insertions(+), 26 deletions(-) rename djangoldp_webpushnotification/management/commands/{gen_vapid_keys.py => gen_vapid_key.py} (81%) create mode 100644 djangoldp_webpushnotification/migrations/0001_initial.py delete mode 100644 djangoldp_webpushnotification/migrations/0012_vapidkeyset.py diff --git a/djangoldp_webpushnotification/admin.py b/djangoldp_webpushnotification/admin.py index bda33a1..83aa596 100644 --- a/djangoldp_webpushnotification/admin.py +++ b/djangoldp_webpushnotification/admin.py @@ -12,5 +12,8 @@ class VAPIDKeysetAdmin(DjangoLDPAdmin): def private_key_view(self, obj): return obj.private_key.tobytes() + class Meta: + verbose_name = 'VAPID key-set' + admin.site.register(VAPIDKeyset, VAPIDKeysetAdmin) diff --git a/djangoldp_webpushnotification/management/commands/gen_vapid_keys.py b/djangoldp_webpushnotification/management/commands/gen_vapid_key.py similarity index 81% rename from djangoldp_webpushnotification/management/commands/gen_vapid_keys.py rename to djangoldp_webpushnotification/management/commands/gen_vapid_key.py index 2396574..d959d20 100644 --- a/djangoldp_webpushnotification/management/commands/gen_vapid_keys.py +++ b/djangoldp_webpushnotification/management/commands/gen_vapid_key.py @@ -11,10 +11,8 @@ class Command(BaseCommand): def handle(self, *args, **options): priv_key = SigningKey.generate(curve=ecdsa.NIST256p) - pub_key = priv_key.get_verifying_key() VAPIDKeyset.objects.create( - public_key=urlsafe_b64encode(b"\x04" + pub_key.to_string()).strip(b"="), private_key=urlsafe_b64encode(priv_key.to_string()).strip(b"=") ) diff --git a/djangoldp_webpushnotification/migrations/0001_initial.py b/djangoldp_webpushnotification/migrations/0001_initial.py new file mode 100644 index 0000000..78380b5 --- /dev/null +++ b/djangoldp_webpushnotification/migrations/0001_initial.py @@ -0,0 +1,21 @@ +# Generated by Django 2.2.19 on 2021-04-07 14:38 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='VAPIDKeyset', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('private_key', models.BinaryField(max_length=43)), + ], + ), + ] diff --git a/djangoldp_webpushnotification/migrations/0012_vapidkeyset.py b/djangoldp_webpushnotification/migrations/0012_vapidkeyset.py deleted file mode 100644 index 49ede53..0000000 --- a/djangoldp_webpushnotification/migrations/0012_vapidkeyset.py +++ /dev/null @@ -1,19 +0,0 @@ -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('djangoldp_notification', '0011_auto_20210218_1145'), - ] - - operations = [ - migrations.CreateModel( - name='VAPIDKeyset', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('public_key', models.BinaryField(max_length=87)), - ('private_key', models.BinaryField(max_length=43)), - ], - ), - ] diff --git a/djangoldp_webpushnotification/models.py b/djangoldp_webpushnotification/models.py index 2c46b4c..294c85c 100644 --- a/djangoldp_webpushnotification/models.py +++ b/djangoldp_webpushnotification/models.py @@ -1,14 +1,24 @@ +from ecdsa import SigningKey, NIST256p from django.db import models +from base64 import urlsafe_b64decode, urlsafe_b64encode -def send_request(target, object_iri, instance, created): - ActivityQueueService.send_activity(target, json) class VAPIDKeyset(models.Model): - public_key = models.BinaryField(max_length=87) private_key = models.BinaryField(max_length=43) - + def __str__(self): return "public_key:{}... private_key:{}...".format( - self.public_key.tobytes()[:10], + self.public_key.tobytes()[:10], self.private_key.tobytes()[:10] ) + + @property + def public_key(self): + key_str = self.private_key.tobytes() + padding = len(key_str) % 4 + key_str += b"=" * padding + key = SigningKey.from_string( + urlsafe_b64decode(key_str), + curve=NIST256p + ).get_verifying_key() + return urlsafe_b64encode(b"\x04" + key.to_string()).strip(b"=")