python3: fix encoding/decoding of PWA private key (#35425)

This commit is contained in:
Frédéric Péters 2019-08-18 12:42:22 +02:00
parent 176576c5e3
commit 50c44019a5
2 changed files with 5 additions and 3 deletions

View File

@ -65,7 +65,7 @@ class PwaSettings(models.Model):
vapid = Vapid()
vapid.generate_keys()
self.push_notifications_infos = {
'private_key': vapid.private_pem(),
'private_key': force_text(vapid.private_pem()),
}
elif not self.push_notifications:
self.push_notifications_infos = {}

View File

@ -21,6 +21,7 @@ from django.conf import settings
from django.http import HttpResponse, HttpResponseForbidden, Http404, JsonResponse
from django.template.loader import get_template, TemplateDoesNotExist
from django.utils.encoding import force_text, force_bytes
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
@ -50,11 +51,12 @@ def js_response(request, template_name, **kwargs):
if settings.PWA_VAPID_PUBLIK_KEY: # legacy
pwa_vapid_public_key = settings.PWA_VAPID_PUBLIK_KEY
else:
pwa_vapid_public_key = base64.urlsafe_b64encode(
pwa_vapid_public_key = force_text(
base64.urlsafe_b64encode(
Vapid.from_pem(pwa_settings.push_notifications_infos['private_key'].encode('ascii')
).private_key.public_key().public_bytes(
encoding=serialization.Encoding.X962,
format=serialization.PublicFormat.UncompressedPoint)).strip('=')
format=serialization.PublicFormat.UncompressedPoint)).strip(b'='))
context = {
'pwa_vapid_public_key': pwa_vapid_public_key,
'pwa_notification_badge_url': settings.PWA_NOTIFICATION_BADGE_URL,