Remove VAPIDKeyset.public_key, generate on demand

Also rename `gen_vapid_keys` to `gen_vapid_key`, and merge migrations
This commit is contained in:
3wc 2021-04-07 17:06:40 +02:00
parent 6ac2905a6c
commit 64015d9b5b
5 changed files with 39 additions and 26 deletions

View File

@ -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)

View File

@ -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"=")
)

View 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)),
],
),
]

View File

@ -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)),
],
),
]

View File

@ -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"=")