Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generateTestHeaderStringAsync function to Webhooks.ts #2048

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 56 additions & 22 deletions src/Webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
receivedAt: number
) => Promise<WebhookEvent>;
generateTestHeaderString: (opts: WebhookTestHeaderOptions) => string;
generateTestHeaderStringAsync: (
opts: WebhookTestHeaderOptions
) => Promise<string>;
};

export function createWebhooks(
Expand Down Expand Up @@ -142,31 +145,30 @@
* @property {CryptoProvider} cryptoProvider - Crypto provider to use for computing the signature if none was provided. Defaults to NodeCryptoProvider.
*/
generateTestHeaderString: function(opts: WebhookTestHeaderOptions): string {
if (!opts) {
throw new StripeError({
message: 'Options are required',
});
}

opts.timestamp =
Math.floor(opts.timestamp) || Math.floor(Date.now() / 1000);
opts.scheme = opts.scheme || signature.EXPECTED_SCHEME;

opts.cryptoProvider = opts.cryptoProvider || getCryptoProvider();
const preparedOpts = prepareOptions(opts);

opts.signature =
opts.signature ||
opts.cryptoProvider.computeHMACSignature(
opts.timestamp + '.' + opts.payload,
opts.secret
const signature =
preparedOpts.signature ||
preparedOpts.cryptoProvider.computeHMACSignature(
preparedOpts.payloadString,
preparedOpts.secret
);

const generatedHeader = [
't=' + opts.timestamp,
opts.scheme + '=' + opts.signature,
].join(',');

return generatedHeader;
return preparedOpts.generateHeaderString(signature);
},
generateTestHeaderStringAsync: async function(
opts: WebhookTestHeaderOptions
) {
const preparedOpts = prepareOptions(opts);

const signature =
preparedOpts.signature ||
(await preparedOpts.cryptoProvider.computeHMACSignatureAsync(
preparedOpts.payloadString,
preparedOpts.secret
));

return preparedOpts.generateHeaderString(signature);
},
};

Expand Down Expand Up @@ -440,9 +442,41 @@
if (!webhooksCryptoProviderInstance) {
webhooksCryptoProviderInstance = platformFunctions.createDefaultCryptoProvider();
}
return webhooksCryptoProviderInstance!;

Check warning on line 445 in src/Webhooks.ts

View workflow job for this annotation

GitHub Actions / Build

Forbidden non-null assertion
}

function prepareOptions(
opts: WebhookTestHeaderOptions
): WebhookTestHeaderOptions & {
payloadString: string;
generateHeaderString: (signature: string) => string;
} {
if (!opts) {
throw new StripeError({
message: 'Options are required',
});
}

const timestamp =
Math.floor(opts.timestamp) || Math.floor(Date.now() / 1000);
const scheme = opts.scheme || signature.EXPECTED_SCHEME;
const cryptoProvider = opts.cryptoProvider || getCryptoProvider();
const payloadString = `${timestamp}.${opts.payload}`;

const generateHeaderString = (signature: string): string => {
return `t=${timestamp},${scheme}=${signature}`;
};

return {
...opts,
timestamp,
scheme,
cryptoProvider,
payloadString,
generateHeaderString,
};
}

Webhook.signature = signature;

return Webhook;
Expand Down
24 changes: 24 additions & 0 deletions test/Webhook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ function createWebhooksTestSuite(stripe) {
});
});

describe('.generateTestHeaderStringAsync', () => {
it('should throw when no opts are passed', async () => {
await expect(
stripe.webhooks.generateTestHeaderStringAsync()
).to.be.rejectedWith('Options are required');
});

it('should correctly construct a webhook header', async () => {
const header = await stripe.webhooks.generateTestHeaderStringAsync({
payload: EVENT_PAYLOAD_STRING,
secret: SECRET,
});

expect(header).to.not.be.undefined;
expect(header.split(',')).to.have.lengthOf(2);
expect(header).to.equal(
stripe.webhooks.generateTestHeaderString({
xavdid-stripe marked this conversation as resolved.
Show resolved Hide resolved
payload: EVENT_PAYLOAD_STRING,
secret: SECRET,
})
);
});
});

const makeConstructEventTests = (
constructEventFn: typeof stripe.webhooks.construct
) => {
Expand Down
Loading