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

feat: async signingSecret on ExpressReceiver #877

Merged
merged 1 commit into from
Apr 14, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/receivers/ExpressReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,19 @@ describe('ExpressReceiver', function () {
// ----------------------------
// runWithValidRequest

async function runWithValidRequest(req: Request, state: any): Promise<void> {
async function runWithValidRequest(
req: Request,
state: any,
signingSecretFn?: () => PromiseLike<string>,
): Promise<void> {
// Arrange
const resp = buildResponseToVerify(state);
const next = (error: any) => {
state.error = error;
};

// Act
const verifier = verifySignatureAndParseRawBody(noopLogger, signingSecret);
const verifier = verifySignatureAndParseRawBody(noopLogger, signingSecretFn || signingSecret);
// eslint-disable-next-line @typescript-eslint/await-thenable
await verifier(req, resp, next);
}
Expand All @@ -410,6 +414,13 @@ describe('ExpressReceiver', function () {
assert.isUndefined(state.error);
});

it('should verify requests on GCP using async signingSecret', async () => {
const state: any = {};
await runWithValidRequest(buildGCPRequest(), state, () => Promise.resolve(signingSecret));
// Assert
assert.isUndefined(state.error);
});

// ----------------------------
// parse error

Expand Down
13 changes: 10 additions & 3 deletions src/receivers/ExpressReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { renderHtmlForInstallPath } from './render-html-for-install-path';
// TODO: we throw away the key names for endpoints, so maybe we should use this interface. is it better for migrations?
// if that's the reason, let's document that with a comment.
export interface ExpressReceiverOptions {
signingSecret: string;
signingSecret: string | (() => PromiseLike<string>);
logger?: Logger;
logLevel?: LogLevel;
endpoints?:
Expand Down Expand Up @@ -319,7 +319,10 @@ export const respondToUrlVerification: RequestHandler = (req, res, next) => {
* - Verify the request signature
* - Parse request.body and assign the successfully parsed object to it.
*/
export function verifySignatureAndParseRawBody(logger: Logger, signingSecret: string): RequestHandler {
export function verifySignatureAndParseRawBody(
logger: Logger,
signingSecret: string | (() => PromiseLike<string>),
): RequestHandler {
return async (req, res, next) => {
let stringBody: string;
// On some environments like GCP (Google Cloud Platform),
Expand All @@ -338,7 +341,11 @@ export function verifySignatureAndParseRawBody(logger: Logger, signingSecret: st
try {
// This handler parses `req.body` or `req.rawBody`(on Google Could Platform)
// and overwrites `req.body` with the parsed JS object.
req.body = verifySignatureAndParseBody(signingSecret, stringBody, req.headers);
req.body = verifySignatureAndParseBody(
typeof signingSecret === 'string' ? signingSecret : await signingSecret(),
stringBody,
req.headers,
);
} catch (error) {
if (error) {
if (error instanceof ReceiverAuthenticityError) {
Expand Down