Remove VAPIDKeyset.public_key, generate on demand
Also rename `gen_vapid_keys` to `gen_vapid_key`, and merge migrations
This commit is contained in:
parent
6ac2905a6c
commit
64015d9b5b
@ -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)
|
||||
|
@ -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"=")
|
||||
)
|
||||
|
21
djangoldp_webpushnotification/migrations/0001_initial.py
Normal file
21
djangoldp_webpushnotification/migrations/0001_initial.py
Normal file
@ -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)),
|
||||
],
|
||||
),
|
||||
]
|
@ -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)),
|
||||
],
|
||||
),
|
||||
]
|
@ -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"=")
|
||||
|
Reference in New Issue
Block a user