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

26 lines
744 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(
self.public_key[:10],
self.private_key.tobytes()[:10]
)
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(
urlsafe_b64decode(key_str),
curve=NIST256p
).get_verifying_key()
return urlsafe_b64encode(b"\x04" + key.to_string()).strip(b"=")