Browse Source

BROKEN: got a little further now building JWT

unsubscribe-mute
Matthew Raymer 1 year ago
parent
commit
d3dd048efd
  1. 2
      src/db.ts
  2. 30
      src/notificationService.ts
  3. 12
      src/vapidService.ts

2
src/db.ts

@ -86,11 +86,9 @@ class DBService {
async getVapidKeys(): Promise<VapidKeys[]> { async getVapidKeys(): Promise<VapidKeys[]> {
console.log(__filename, "getVapidKeys", this.isReady);
let result = [ new VapidKeys() ]; let result = [ new VapidKeys() ];
if ( this.isReady ) { if ( this.isReady ) {
result = await this.dataSource.manager.find(VapidKeys); result = await this.dataSource.manager.find(VapidKeys);
console.log(__filename, "results of find: ", result);
} else { } else {
console.log(__filename, "Database is not ready"); console.log(__filename, "Database is not ready");

30
src/notificationService.ts

@ -1,10 +1,8 @@
//import SubscriptionService from './subscriptionService.js';
import VapidService from './vapidService.js'; import VapidService from './vapidService.js';
import { VapidKeys } from './VapidKeys.js'; import { VapidKeys } from './VapidKeys.js';
import * as https from 'https'; import * as https from 'https';
import * as http_ece from 'http_ece'; import * as http_ece from 'http_ece';
import crypto from 'crypto'; import crypto from 'crypto';
//import { Subscription } from "./Subscription.js"
export interface Message { export interface Message {
title: string; title: string;
@ -24,7 +22,6 @@ export interface BrowserSubscription {
export class NotificationService { export class NotificationService {
private static instance: NotificationService; private static instance: NotificationService;
// private subscriptionService: SubscriptionService = SubscriptionService.getInstance();
private vapidService: VapidService = VapidService.getInstance(); private vapidService: VapidService = VapidService.getInstance();
constructor() { constructor() {
@ -37,9 +34,6 @@ export class NotificationService {
return NotificationService.instance; return NotificationService.instance;
} }
private generateSalt(length = 16): Buffer {
return crypto.randomBytes(length);
}
async sendNotification(subscription: BrowserSubscription, message: Message) { async sendNotification(subscription: BrowserSubscription, message: Message) {
await this.pushToEndpoint(subscription, message); await this.pushToEndpoint(subscription, message);
@ -88,26 +82,30 @@ export class NotificationService {
} }
private async encrypt(publicKey: string, auth: string, payload: Buffer): Promise<Buffer> { private async encrypt(p256dh: string, auth: string, payload: Buffer): Promise<Buffer> {
try { try {
console.log('Public Key:', publicKey); console.log('Public Key:', p256dh);
console.log('Auth:', auth); console.log('Auth:', auth);
const vapidKeys: VapidKeys[] = await this.vapidService.getVapidKeys(); const vapidKeys: VapidKeys[] = await this.vapidService.getVapidKeys();
const vapidkey: VapidKeys = vapidKeys[0]; const vapidkey: VapidKeys = vapidKeys[0];
const vapidPrivateKeyBase64: string = vapidkey['privateKey']; const vapidPrivateKeyBase64: string = vapidkey['privateKey'];
console.log(vapidPrivateKeyBase64);
console.log("vapidPrivateKeyBase64: ", vapidPrivateKeyBase64);
const vapidPrivateKeyBuffer: Buffer = Buffer.from(vapidPrivateKeyBase64, 'base64'); const vapidPrivateKeyBuffer: Buffer = Buffer.from(vapidPrivateKeyBase64, 'base64');
const ecdh = crypto.createECDH('prime256v1'); const ecdh = crypto.createECDH('prime256v1');
ecdh.setPrivateKey(vapidPrivateKeyBuffer); ecdh.setPrivateKey(vapidPrivateKeyBuffer);
const publicKeyBuffer: Buffer = Buffer.from(publicKey, 'base64'); const publicKeyBuffer: Buffer = Buffer.from(p256dh, 'base64');
// const authBuffer: Buffer = Buffer.from(auth, 'base64');
console.log("1: ", payload);
console.log("3: ", publicKeyBuffer);
console.log("4: ", auth);
return http_ece.encrypt(payload, { return http_ece.encrypt(payload, {
'salt': this.generateSalt(), 'privateKey': ecdh,
'privateKey': ecdh, // Your VAPID private key 'dh': publicKeyBuffer,
'publicKey': publicKeyBuffer, // Client's public key from the subscription object subscription.keys.p256dh 'authSecret': Buffer.from(auth)
'authSecret': auth // Client's auth secret from the subscription object subscription.keys.auth
}); });
} catch (error) { } catch (error) {
console.error('Error encrypting payload:', error); console.error('Error encrypting payload:', error);

12
src/vapidService.ts

@ -53,20 +53,8 @@ class VapidService {
return result; return result;
} }
/*
private async addVapidKeys(vapidkeys: VapidKeyData): Promise<VapidKeys> {
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<VapidKeys[]> { async getVapidKeys(): Promise<VapidKeys[]> {
console.log("getVapidKeys");
let result = await this.dbService.getVapidKeys(); let result = await this.dbService.getVapidKeys();
return result; return result;
} }

Loading…
Cancel
Save