Updates and a remaining authentication error to the push service at Mozilla
This commit is contained in:
14
package-lock.json
generated
14
package-lock.json
generated
@@ -10,6 +10,7 @@
|
|||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
|
"eckey-utils": "^0.7.13",
|
||||||
"elliptic": "^6.5.4",
|
"elliptic": "^6.5.4",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"http_ece": "^1.1.0",
|
"http_ece": "^1.1.0",
|
||||||
@@ -2471,6 +2472,14 @@
|
|||||||
"node": ">=8"
|
"node": ">=8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/asn1-ts": {
|
||||||
|
"version": "8.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/asn1-ts/-/asn1-ts-8.0.2.tgz",
|
||||||
|
"integrity": "sha512-M9btvRJRhMhPsUFzAfuqkmQPaLLw1KZNl8xtIBpC5fvbAmlpgJcsLKMP/hxKMAUcH52UUTViEQ/cm6/whkYb+Q==",
|
||||||
|
"dependencies": {
|
||||||
|
"tslib": "^2.4.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/babel-jest": {
|
"node_modules/babel-jest": {
|
||||||
"version": "29.6.2",
|
"version": "29.6.2",
|
||||||
"resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.6.2.tgz",
|
||||||
@@ -3590,6 +3599,11 @@
|
|||||||
"safe-buffer": "^5.0.1"
|
"safe-buffer": "^5.0.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/eckey-utils": {
|
||||||
|
"version": "0.7.13",
|
||||||
|
"resolved": "https://registry.npmjs.org/eckey-utils/-/eckey-utils-0.7.13.tgz",
|
||||||
|
"integrity": "sha512-medLrbDAa4beQTwcoh2FwRti1d+2b86Op7LtBSaAFNmIRh1MSORt2YwEmAYgp6wf8QEROwJNAoVoC9Rg2Wgbfw=="
|
||||||
|
},
|
||||||
"node_modules/ee-first": {
|
"node_modules/ee-first": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
|
||||||
|
|||||||
@@ -41,6 +41,7 @@
|
|||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"body-parser": "^1.20.2",
|
"body-parser": "^1.20.2",
|
||||||
|
"eckey-utils": "^0.7.13",
|
||||||
"elliptic": "^6.5.4",
|
"elliptic": "^6.5.4",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"http_ece": "^1.1.0",
|
"http_ece": "^1.1.0",
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ class Server {
|
|||||||
this.app.post('/web-push/subscribe', async (req: Request, res: Response) => {
|
this.app.post('/web-push/subscribe', async (req: Request, res: Response) => {
|
||||||
const subscription = req.body as BrowserSubscription;
|
const subscription = req.body as BrowserSubscription;
|
||||||
const message = { "title": "You are subscribed." } as Message;
|
const message = { "title": "You are subscribed." } as Message;
|
||||||
console.log("/web-push/subscribe", req.body);
|
|
||||||
await this.subscriptionService.addSubscription(subscription);
|
await this.subscriptionService.addSubscription(subscription);
|
||||||
await this.notificationService.sendNotification(subscription, message);
|
await this.notificationService.sendNotification(subscription, message);
|
||||||
res.status(201).send();
|
res.status(201).send();
|
||||||
|
|||||||
@@ -42,11 +42,13 @@ export class NotificationService {
|
|||||||
private async pushToEndpoint(subscription: BrowserSubscription, message: Message): Promise<void> {
|
private async pushToEndpoint(subscription: BrowserSubscription, message: Message): Promise<void> {
|
||||||
const payloadString = JSON.stringify(message);
|
const payloadString = JSON.stringify(message);
|
||||||
const payloadBuffer = Buffer.from(payloadString, 'utf-8');
|
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 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 parsedUrl = new URL(subscription.endpoint);
|
||||||
const options: https.RequestOptions = {
|
const options: https.RequestOptions = {
|
||||||
@@ -63,6 +65,9 @@ export class NotificationService {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.log(parsedUrl);
|
||||||
|
console.log(options);
|
||||||
|
|
||||||
return new Promise<void>((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
const req = https.request(options, (res) => {
|
const req = https.request(options, (res) => {
|
||||||
if (res.statusCode! >= 200 && res.statusCode! < 300) {
|
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 {
|
try {
|
||||||
console.log('Public Key:', p256dh);
|
const vapidPrivateKeyBase64: string = appKeys['privateKey'];
|
||||||
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 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(p256dh, '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, {
|
return http_ece.encrypt(payload, {
|
||||||
'privateKey': ecdh,
|
'privateKey': ecdh,
|
||||||
'dh': publicKeyBuffer,
|
'dh': publicKeyBuffer,
|
||||||
'authSecret': Buffer.from(auth)
|
'authSecret': Buffer.from(auth)
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error encrypting payload:', error);
|
console.error('Error encrypting payload:', error);
|
||||||
// Handle the error as appropriate for your application
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
|
import ecKeyUtils from "eckey-utils";
|
||||||
import { VapidKeys } from './VapidKeys.js';
|
import { VapidKeys } from './VapidKeys.js';
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
@@ -16,7 +16,6 @@ class VapidService {
|
|||||||
private static instance: VapidService;
|
private static instance: VapidService;
|
||||||
private dbService: DBService = DBService.getInstance();
|
private dbService: DBService = DBService.getInstance();
|
||||||
|
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,16 +59,22 @@ class VapidService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async createVapidAuthHeader(endpoint: string, expiration: number, subject: string): Promise<{ 'Authorization': string, 'Crypto-Key': string }> {
|
async createVapidAuthHeader(endpoint: string, expiration: number, subject: string, appKeys: VapidKeys): Promise<{ 'Authorization': string, 'Crypto-Key': string }> {
|
||||||
const { publicKey, privateKey } = await this.getVapidKeys()[0];
|
const { publicKey, privateKey } = appKeys;
|
||||||
|
|
||||||
const jwtInfo = {
|
const jwtInfo = {
|
||||||
aud: new URL(endpoint).origin,
|
aud: new URL(endpoint).origin,
|
||||||
exp: Math.floor((Date.now() / 1000) + expiration),
|
exp: Math.floor((Date.now() / 1000) + expiration),
|
||||||
sub: subject
|
sub: subject
|
||||||
};
|
};
|
||||||
|
const curveName = 'prime256v1';
|
||||||
|
const ecdh = crypto.createECDH(curveName);
|
||||||
|
const privateKeyBuffer = Buffer.from(privateKey, 'base64');
|
||||||
|
ecdh.setPrivateKey(privateKeyBuffer);
|
||||||
|
|
||||||
const jwtToken = jwt.sign(jwtInfo, privateKey, { algorithm: 'ES256' });
|
const pems = ecKeyUtils.generatePem({curveName, privateKey: ecdh.getPrivateKey(), publicKey: ecdh.getPublicKey() });
|
||||||
|
|
||||||
|
const jwtToken = jwt.sign(jwtInfo, pems.privateKey, { algorithm: 'ES256' });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'Authorization': `vapid t=${jwtToken}, k=${publicKey}`,
|
'Authorization': `vapid t=${jwtToken}, k=${publicKey}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user