From 9d19e0e73c5d840f4c5c2dc5a9194cc5df77813c Mon Sep 17 00:00:00 2001 From: trav Date: Wed, 7 Apr 2021 09:44:56 -0400 Subject: [PATCH] added in this stuff https://git.startinblox.com/djangoldp-packages/djangoldp-notification/merge_requests/43#note_59310 --- admin.py | 24 ++++++++++++++++++++++++ management/commands/gen_vapid_keys.py | 23 +++++++++++++++++++++++ migrations/0012_vapidkeyset.py | 19 +++++++++++++++++++ models.py | 17 +++++++++++++++++ setup.cfg | 5 ++++- 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 admin.py create mode 100644 management/commands/gen_vapid_keys.py create mode 100644 migrations/0012_vapidkeyset.py create mode 100644 models.py diff --git a/admin.py b/admin.py new file mode 100644 index 0000000..f4f8c45 --- /dev/null +++ b/admin.py @@ -0,0 +1,24 @@ +from django.contrib import admin +from djangoldp.admin import DjangoLDPAdmin +from djangoldp.models import Model +from .models import Notification, NotificationSetting, Subscription, VAPIDKeyset + + + +class NotificationSettingAdmin(DjangoLDPAdmin): + ordering = ['urlid'] + + +class VAPIDKeysetAdmin(DjangoLDPAdmin): + readonly_fields = ('public_key_view', 'private_key_view') + + def public_key_view(self, obj): + return obj.public_key.tobytes() + + def private_key_view(self, obj): + return obj.private_key.tobytes() + +admin.site.register(Notification, NotificationAdmin) +admin.site.register(Subscription, SubscriptionAdmin) +admin.site.register(NotificationSetting, NotificationSettingAdmin) +admin.site.register(VAPIDKeyset, VAPIDKeysetAdmin) \ No newline at end of file diff --git a/management/commands/gen_vapid_keys.py b/management/commands/gen_vapid_keys.py new file mode 100644 index 0000000..10a2ee2 --- /dev/null +++ b/management/commands/gen_vapid_keys.py @@ -0,0 +1,23 @@ +import ecdsa +from ecdsa import SigningKey +from base64 import urlsafe_b64encode + +from django.core.management.base import BaseCommand +from djangoldp_notification.models import VAPIDKeyset + + +class Command(BaseCommand): + help = "Generate VAPID key pair" + + 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"=") + ) + + self.stdout.write("VAPID Keyset succesfully generated") + + exit(0) \ No newline at end of file diff --git a/migrations/0012_vapidkeyset.py b/migrations/0012_vapidkeyset.py new file mode 100644 index 0000000..49ede53 --- /dev/null +++ b/migrations/0012_vapidkeyset.py @@ -0,0 +1,19 @@ +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/models.py b/models.py new file mode 100644 index 0000000..b68ed10 --- /dev/null +++ b/models.py @@ -0,0 +1,17 @@ +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.private_key.tobytes()[:10] + ) + +@receiver(post_save, sender=Notification) +def send_email_on_notification(sender, instance, created, **kwargs): + if created and instance.summary and getattr(settings,'JABBER_DEFAULT_HOST',False) and instance.user.email: diff --git a/setup.cfg b/setup.cfg index 8e9c04d..aadfaa5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -10,9 +10,12 @@ license = MIT [options] packages = find: install_requires = - djangoldp~=0.5 + djangoldp~=2.1.0 + djangoldp_account~=2.1 + ecdsa~=0.16.1 [options.extras_require] + include_package_data = True dev = factory_boy>=2.11.0