Skip to content

Commit

Permalink
Merge pull request #14 from maxholman/github13
Browse files Browse the repository at this point in the history
fix: edge browser "Zero or duplicate tokens or token signing keys" error
  • Loading branch information
maxholman authored Aug 1, 2024
2 parents 9eac0da + d3fc2d0 commit 02d16ac
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 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=BQYAAAALwNoB%2fBFE0R6H%2bayc5WRfMVkJlB3mHXSmsCTKWeZZM%2faOvneI8DaE%2fQeCC6uE17dj%2bPNz9drmi5bEvrNRTs8hzCS2abTpc4lfS9XbebI8lqe%2bXcPerho8q5SnWcTk4GURFakuZUYI6e0xMnTG%2bpvFxFatlhq4CELEEyo9%2fAP%2fHMx8tPCSXsh3sSFDtx6MkDnMHmR%2fUvcCrnLbXCDiseZ40bQI6a5tRmOM0izm0Gfc%2foKWmce8JUk2TRtm%2fecPR1F0X%2bVj4JQsDAR7Kx56HUCONLNMzuVm%2b9O6Yp1bLUfnp1QJyVOVyyj6DQfG%2bOOsikh%2bd2foJpWHggpFenW3NiT3',
expirationTime: null,
keys: {
p256dh:
'BDPQRxcMhoXtPqJvQ9Kp-FGsQexasWXvh0RTE15Y0qbxKLFZ6eG3U90RJQFkb_SdAB_YzbIm7hcvzn1yAzNZbxs',
auth: 'XGCMxdgIGIp7SNs3kb4x7g',
},
},
} 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
32 changes: 29 additions & 3 deletions packages/web-push/test/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { describe, expect, test } from 'vitest';
import { buildPushPayload, type PushMessage } from '../lib/main.js';
import { fakeSubscriptions, fakeVapid } from './fixtures.js';

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

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

const init = await buildPushPayload(message, subscription, fakeVapid);
const res = await fetch(subscription.endpoint, init);
Expand All @@ -24,4 +24,30 @@ describe('Payload', () => {
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 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);

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

// seemingly Edge specific headers, static so we can check them here
expect(res.headers.get('x-wns-notificationstatus')).toBe('received');
expect(res.headers.get('x-wns-status')).toBe('received');
});
});
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 02d16ac

Please sign in to comment.