From d3dd048efd73a0adfdf4a2cd660d57121c4a5fbd Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Mon, 18 Sep 2023 09:30:13 -0400 Subject: [PATCH] BROKEN: got a little further now building JWT --- src/db.ts | 2 -- src/notificationService.ts | 30 ++++++++++++++---------------- src/vapidService.ts | 12 ------------ 3 files changed, 14 insertions(+), 30 deletions(-) diff --git a/src/db.ts b/src/db.ts index 46f21fa..fe7fc25 100644 --- a/src/db.ts +++ b/src/db.ts @@ -86,11 +86,9 @@ class DBService { async getVapidKeys(): Promise { - console.log(__filename, "getVapidKeys", this.isReady); let result = [ new VapidKeys() ]; if ( this.isReady ) { result = await this.dataSource.manager.find(VapidKeys); - console.log(__filename, "results of find: ", result); } else { console.log(__filename, "Database is not ready"); diff --git a/src/notificationService.ts b/src/notificationService.ts index 6d197ec..2d58ef5 100644 --- a/src/notificationService.ts +++ b/src/notificationService.ts @@ -1,10 +1,8 @@ -//import SubscriptionService from './subscriptionService.js'; import VapidService from './vapidService.js'; import { VapidKeys } from './VapidKeys.js'; import * as https from 'https'; import * as http_ece from 'http_ece'; import crypto from 'crypto'; -//import { Subscription } from "./Subscription.js" export interface Message { title: string; @@ -24,7 +22,6 @@ export interface BrowserSubscription { export class NotificationService { private static instance: NotificationService; -// private subscriptionService: SubscriptionService = SubscriptionService.getInstance(); private vapidService: VapidService = VapidService.getInstance(); constructor() { @@ -37,9 +34,6 @@ export class NotificationService { return NotificationService.instance; } - private generateSalt(length = 16): Buffer { - return crypto.randomBytes(length); - } async sendNotification(subscription: BrowserSubscription, message: Message) { await this.pushToEndpoint(subscription, message); @@ -88,26 +82,30 @@ export class NotificationService { } - private async encrypt(publicKey: string, auth: string, payload: Buffer): Promise { + private async encrypt(p256dh: string, auth: string, payload: Buffer): Promise { try { - console.log('Public Key:', publicKey); + console.log('Public Key:', p256dh); console.log('Auth:', auth); - + const vapidKeys: VapidKeys[] = await this.vapidService.getVapidKeys(); const vapidkey: VapidKeys = vapidKeys[0]; const vapidPrivateKeyBase64: string = vapidkey['privateKey']; - console.log(vapidPrivateKeyBase64); + + console.log("vapidPrivateKeyBase64: ", vapidPrivateKeyBase64); + const vapidPrivateKeyBuffer: Buffer = Buffer.from(vapidPrivateKeyBase64, 'base64'); const ecdh = crypto.createECDH('prime256v1'); ecdh.setPrivateKey(vapidPrivateKeyBuffer); - const publicKeyBuffer: Buffer = Buffer.from(publicKey, 'base64'); -// const authBuffer: Buffer = Buffer.from(auth, 'base64'); + const publicKeyBuffer: Buffer = Buffer.from(p256dh, 'base64'); + console.log("1: ", payload); + console.log("3: ", publicKeyBuffer); + console.log("4: ", auth); + return http_ece.encrypt(payload, { - 'salt': this.generateSalt(), - 'privateKey': ecdh, // Your VAPID private key - 'publicKey': publicKeyBuffer, // Client's public key from the subscription object subscription.keys.p256dh - 'authSecret': auth // Client's auth secret from the subscription object subscription.keys.auth + 'privateKey': ecdh, + 'dh': publicKeyBuffer, + 'authSecret': Buffer.from(auth) }); } catch (error) { console.error('Error encrypting payload:', error); diff --git a/src/vapidService.ts b/src/vapidService.ts index a52ae83..82c9ef5 100644 --- a/src/vapidService.ts +++ b/src/vapidService.ts @@ -53,20 +53,8 @@ class VapidService { return result; } -/* - private async addVapidKeys(vapidkeys: VapidKeyData): Promise { - let result = new VapidKeys(); - const keys = await this.getVapidKeys(); - - if (keys.length == 1 && typeof(keys[0].publicKey) == "undefined" ) { - result = await this.dbService.saveVapidKeys(vapidkeys.publicKey, vapidkeys.privateKey); - } - return result; - } -*/ async getVapidKeys(): Promise { - console.log("getVapidKeys"); let result = await this.dbService.getVapidKeys(); return result; }