This repository has been archived on 2021-04-21. You can view files and clone it, but cannot push or open issues or pull requests.
djangoldp-webpushnotification/djangoldp_webpushnotification/models.py

24 lines
702 B
Python
Raw Normal View History

from base64 import urlsafe_b64decode, urlsafe_b64encode
2021-04-07 14:11:30 +00:00
2021-04-07 15:18:09 +00:00
from django.db import models
from ecdsa import NIST256p, SigningKey
2021-04-07 13:52:32 +00:00
class VAPIDKeyset(models.Model):
2021-04-07 15:18:09 +00:00
private_key = models.BinaryField(max_length=43)
2021-04-07 15:18:09 +00:00
def __str__(self):
return "public_key:{}... private_key:{}...".format(
2021-04-15 12:27:44 +00:00
self.public_key[:10], self.private_key[:10]
2021-04-07 15:18:09 +00:00
)
2021-04-07 15:18:09 +00:00
@property
def public_key(self):
key_str = self.private_key
2021-04-07 15:18:09 +00:00
padding = len(key_str) % 4
key_str += b"=" * padding
key = SigningKey.from_string(
2021-04-15 12:27:58 +00:00
urlsafe_b64decode(key_str), curve=NIST256p
2021-04-07 15:18:09 +00:00
).get_verifying_key()
return urlsafe_b64encode(b"\x04" + key.to_string()).strip(b"=")