|
|
@ -42,11 +42,13 @@ export class NotificationService { |
|
|
|
private async pushToEndpoint(subscription: BrowserSubscription, message: Message): Promise<void> { |
|
|
|
const payloadString = JSON.stringify(message); |
|
|
|
const payloadBuffer = Buffer.from(payloadString, 'utf-8'); |
|
|
|
const vapidKeys: VapidKeys[] = await this.vapidService.getVapidKeys(); |
|
|
|
const vapidkey: VapidKeys = vapidKeys[0]; |
|
|
|
|
|
|
|
const encrypted = await this.encrypt(subscription.keys.p256dh, subscription.keys.auth, payloadBuffer); |
|
|
|
const encrypted = await this.encrypt(vapidkey, subscription.keys.p256dh, subscription.keys.auth, payloadBuffer); |
|
|
|
const endpoint = subscription.endpoint; |
|
|
|
|
|
|
|
const vapidHeaders = await this.vapidService.createVapidAuthHeader(endpoint, 12 * 60 * 60, 'mailto:example@example.com'); |
|
|
|
const vapidHeaders = await this.vapidService.createVapidAuthHeader(endpoint, 12 * 60 * 60, 'mailto:example@example.com', vapidkey); |
|
|
|
|
|
|
|
const parsedUrl = new URL(subscription.endpoint); |
|
|
|
const options: https.RequestOptions = { |
|
|
@ -63,6 +65,9 @@ export class NotificationService { |
|
|
|
}, |
|
|
|
}; |
|
|
|
|
|
|
|
console.log(parsedUrl); |
|
|
|
console.log(options); |
|
|
|
|
|
|
|
return new Promise<void>((resolve, reject) => { |
|
|
|
const req = https.request(options, (res) => { |
|
|
|
if (res.statusCode! >= 200 && res.statusCode! < 300) { |
|
|
@ -82,34 +87,22 @@ export class NotificationService { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async encrypt(p256dh: string, auth: string, payload: Buffer): Promise<Buffer> { |
|
|
|
private async encrypt(appKeys: VapidKeys, p256dh: string, auth: string, payload: Buffer): Promise<Buffer> { |
|
|
|
try { |
|
|
|
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: ", vapidPrivateKeyBase64); |
|
|
|
|
|
|
|
const vapidPrivateKeyBase64: string = appKeys['privateKey']; |
|
|
|
const vapidPrivateKeyBuffer: Buffer = Buffer.from(vapidPrivateKeyBase64, 'base64'); |
|
|
|
const ecdh = crypto.createECDH('prime256v1'); |
|
|
|
ecdh.setPrivateKey(vapidPrivateKeyBuffer); |
|
|
|
const publicKeyBuffer: Buffer = Buffer.from(p256dh, 'base64'); |
|
|
|
|
|
|
|
console.log("1: ", payload); |
|
|
|
console.log("3: ", publicKeyBuffer); |
|
|
|
console.log("4: ", auth); |
|
|
|
|
|
|
|
return http_ece.encrypt(payload, { |
|
|
|
'privateKey': ecdh, |
|
|
|
'dh': publicKeyBuffer, |
|
|
|
'authSecret': Buffer.from(auth) |
|
|
|
}); |
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
console.error('Error encrypting payload:', error); |
|
|
|
// Handle the error as appropriate for your application
|
|
|
|
throw error; |
|
|
|
} |
|
|
|
} |
|
|
|