|
|
@ -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<Buffer> { |
|
|
|
private async encrypt(p256dh: string, auth: string, payload: Buffer): Promise<Buffer> { |
|
|
|
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); |
|
|
|