Skip to content

Commit

Permalink
fix: edge browser "Zero or duplicate tokens or token signing keys" error
Browse files Browse the repository at this point in the history
fixes #13
  • Loading branch information
maxholman committed Jul 28, 2024
1 parent 9eac0da commit f8a0b8c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/web-push/lib/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export async function buildPushPayload(
headers: {
...headers,

'crypto-key': `keyid=p256dh;dh=${encodeBase64Url(
'crypto-key': `dh=${encodeBase64Url(
encrypted.localPublicKeyBytes,
)};${headers['crypto-key']}`,

encryption: `keyid=p256dh;salt=${encodeBase64Url(encrypted.salt)}`,
encryption: `salt=${encodeBase64Url(encrypted.salt)}`,

ttl: (message.options?.ttl || 60).toString(),
...(message.options?.urgency && {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ exports[`buildPushPayload 3`] = `
"content-encoding": "aesgcm",
"content-length": "36",
"content-type": "application/octet-stream",
"crypto-key": "keyid=p256dh;dh=BO6-TCHINeAfMhq44SQLnR-3NZnAfwe2n-CjcESbHlOcJkx_8JqbcwRJMd5zrZouJbIUe2AkFnhoDavSymNyOA0;p256ecdsa=BKKXE3jJV5UJ6c8HVPam6DvMPGZK26r-M7ojsO2T_KdjdeMT2d7oQpaO-VI3o3wn33mQ8JlHta3OSJ5f67Ac5ZY",
"encryption": "keyid=p256dh;salt=4CQCKEyyOT_LysC17rsMXQ",
"crypto-key": "dh=BO6-TCHINeAfMhq44SQLnR-3NZnAfwe2n-CjcESbHlOcJkx_8JqbcwRJMd5zrZouJbIUe2AkFnhoDavSymNyOA0;p256ecdsa=BKKXE3jJV5UJ6c8HVPam6DvMPGZK26r-M7ojsO2T_KdjdeMT2d7oQpaO-VI3o3wn33mQ8JlHta3OSJ5f67Ac5ZY",
"encryption": "salt=4CQCKEyyOT_LysC17rsMXQ",
"ttl": "60",
}
`;
Expand Down
12 changes: 11 additions & 1 deletion packages/web-push/test/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PushSubscription } from '../lib/types.js';

export const fakeSubscriptions = {
test: {
chrome: {
endpoint:
'https://fcm.googleapis.com/fcm/send/fL4MGn77FGc:APA91bE9RjT5iS_lfuBIm7PeOS2789EzyWGbUrh-viIAgsGbJIG-Rc65ipPt8hFS6aLwiyvyfXsSIVTZTuISxPUo3kcaklfv_WYpZ4g1g8jY6wChNoHkRmDpGN7qFgI2SkrV2SxYlL-r',
expirationTime: null,
Expand All @@ -11,6 +11,16 @@ export const fakeSubscriptions = {
auth: 'lzqdvQzre8BPRpJTvFJZng',
},
},
edge: {
endpoint:
'https://wns2-sg2p.notify.windows.com/w/?token=BQYAAAAOqpMScCyZs07b3Rhi7WoqEBYPH9pHcOxdJ7lGF0E9YcvLHxBniuEp3FT3%2fHyZcyP%2fCPuqaZSQcIs%2bHn%2f5aQAk9l1BwojHAE2ZRuzMWEClZzdm3A661RAujyNyEoVeKjeTL872YTFMxb0AeEWrAMhQ969Pm9Pq93yunKqL8N0vcCwJ%2bSuznNCzDZgbAst2l2EWbDqRUeUClJxozU9SFowOV0ypMdluq%2bionNVEpJYo2JmnJs%2bQbVUv4MAH%2fKx3pe4CftTkDkgqWWcMhBkVwpexn6%2bNo7F5Ae1kM1vFhruf6%2f4Rc47cqvwSsbmcxj3MhGI%3d',
expirationTime: null,
keys: {
p256dh:
'BOHAcPFrsyWQchMJfijwJbLMV2HVBZHumzQPcgj_hhuXpbqaQunE09dRbWOasPW13e2K7RzQNVoJ7z1iXAFrypY',
auth: 'KxoJGb-hSSi8QFDyld-P7Q',
},
},
} satisfies Record<string, PushSubscription>;

// WARN: these keys are not secure, generate your own!
Expand Down
2 changes: 1 addition & 1 deletion packages/web-push/test/notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('', () => {
test('buildPushPayload', async () => {
vi.setSystemTime(Date.UTC(2000, 1, 1, 13, 0, 0, 0));

const subscription = fakeSubscriptions.test;
const subscription = fakeSubscriptions.chrome;

const requestInfo = await buildPushPayload(
{
Expand Down
26 changes: 24 additions & 2 deletions packages/web-push/test/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { buildPushPayload, type PushMessage } from '../lib/main.js';
import { fakeSubscriptions, fakeVapid } from './fixtures.js';

describe('Payload', () => {
test('Fake Subscription', async () => {
test('Fake Chrome Subscription', async () => {
const message: PushMessage = {
data: 'Some text',
options: {
Expand All @@ -15,7 +15,29 @@ describe('Payload', () => {
},
};

const subscription = fakeSubscriptions.test;
const subscription = fakeSubscriptions.chrome;

const init = await buildPushPayload(message, subscription, fakeVapid);
const res = await fetch(subscription.endpoint, init);

await expect(res.text()).resolves.toMatchInlineSnapshot('""');
expect(res.statusText).toMatchInlineSnapshot('"Created"');
expect(res.status).toMatchInlineSnapshot('201');
});

test('Fake Edge Subscription', async () => {
const message: PushMessage = {
data: 'Some text',
options: {
ttl: 60,
// Topics are strings that can be used to replace a pending messages with
// a new message if they have matching topic names
topic: 'from-test-env',
urgency: 'high',
},
};

const subscription = fakeSubscriptions.edge;

const init = await buildPushPayload(message, subscription, fakeVapid);
const res = await fetch(subscription.endpoint, init);
Expand Down
2 changes: 1 addition & 1 deletion packages/web-push/test/vapid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ describe('VAPID', () => {
test('Headers', async () => {
vi.setSystemTime(new Date(2000, 1, 1, 13));

await vapidHeaders(fakeSubscriptions.test, fakeVapid);
await vapidHeaders(fakeSubscriptions.chrome, fakeVapid);
});
});

0 comments on commit f8a0b8c

Please sign in to comment.