saleor-app-sdk-REDIS_APL/src/middleware.ts

190 lines
5.2 KiB
TypeScript
Raw Normal View History

import crypto from "crypto";
import * as jose from "jose";
2022-08-03 18:51:37 +00:00
import type { Middleware, Request } from "retes";
import { Response } from "retes/response";
2022-05-26 12:14:13 +00:00
2022-08-08 09:01:02 +00:00
import { SALEOR_AUTHORIZATION_BEARER_HEADER, SALEOR_SIGNATURE_HEADER } from "./const";
import { getSaleorHeaders } from "./headers";
2022-08-08 14:55:57 +00:00
import { getJwksUrl } from "./urls";
2022-05-26 12:14:13 +00:00
export const withBaseURL: Middleware = (handler) => async (request) => {
const { host, "x-forwarded-proto": protocol = "http" } = request.headers;
request.context.baseURL = `${protocol}://${host}`;
2022-08-08 15:10:42 +00:00
return handler(request);
};
2022-05-26 12:14:13 +00:00
2022-07-20 14:04:38 +00:00
export const withSaleorDomainPresent: Middleware = (handler) => async (request) => {
const { domain } = getSaleorHeaders(request.headers);
2022-05-26 12:14:13 +00:00
if (!domain) {
2022-07-20 14:04:38 +00:00
return Response.BadRequest({
success: false,
message: "Missing Saleor domain header.",
});
}
2022-05-26 12:14:13 +00:00
2022-07-20 14:04:38 +00:00
return handler(request);
};
2022-05-26 12:14:13 +00:00
export const withSaleorEventMatch =
<E extends string>(expectedEvent: `${Lowercase<E>}`): Middleware =>
(handler) =>
async (request) => {
const { event } = getSaleorHeaders(request.headers);
if (event !== expectedEvent) {
return Response.BadRequest({
success: false,
message: `Invalid Saleor event. Expecting ${expectedEvent}.`,
});
}
2022-05-26 12:14:13 +00:00
return handler(request);
};
2022-07-20 14:04:38 +00:00
export const withAuthTokenRequired: Middleware = (handler) => async (request) => {
const authToken = request.params.auth_token;
if (!authToken) {
return Response.BadRequest({
success: false,
message: "Missing auth token.",
});
}
2022-07-20 14:04:38 +00:00
return handler(request);
};
2022-05-26 12:14:13 +00:00
2022-07-20 14:04:38 +00:00
export const withWebhookSignatureVerified =
(secretKey: string | undefined = undefined): Middleware =>
(handler) =>
async (request) => {
const ERROR_MESSAGE = "Webhook signature verification failed:";
if (request.rawBody === undefined) {
return Response.InternalServerError({
success: false,
message: `${ERROR_MESSAGE} Request payload already parsed.`,
});
}
2022-05-26 12:14:13 +00:00
const { domain: saleorDomain, signature: payloadSignature } = getSaleorHeaders(request.headers);
if (!payloadSignature) {
return Response.BadRequest({
success: false,
2022-08-08 09:01:02 +00:00
message: `${ERROR_MESSAGE} Missing ${SALEOR_SIGNATURE_HEADER} header.`,
});
}
if (secretKey !== undefined) {
const calculatedSignature = crypto
.createHmac("sha256", secretKey)
.update(request.rawBody)
.digest("hex");
if (calculatedSignature !== payloadSignature) {
return Response.BadRequest({
success: false,
message: `${ERROR_MESSAGE} Verification using secret key has failed.`,
});
}
} else {
2022-07-20 14:05:00 +00:00
const [header, , signature] = payloadSignature.split(".");
const jws = {
protected: header,
payload: request.rawBody,
signature,
};
2022-08-03 18:51:37 +00:00
const remoteJwks = jose.createRemoteJWKSet(
2022-08-08 14:55:57 +00:00
new URL(getJwksUrl(saleorDomain))
) as jose.FlattenedVerifyGetKey;
try {
2022-08-03 18:51:37 +00:00
await jose.flattenedVerify(jws, remoteJwks);
} catch {
return Response.BadRequest({
success: false,
message: `${ERROR_MESSAGE} Verification using public key has failed.`,
});
}
}
return handler(request);
};
2022-08-03 18:51:37 +00:00
export interface DashboardTokenPayload extends jose.JWTPayload {
2022-08-03 18:51:37 +00:00
app: string;
}
export const withJWTVerified =
(getAppId: (request: Request) => Promise<string | undefined>): Middleware =>
(handler) =>
async (request) => {
const { domain, authorizationBearer: token } = getSaleorHeaders(request.headers);
const ERROR_MESSAGE = "JWT verification failed:";
2022-08-03 18:51:37 +00:00
if (token === undefined) {
return Response.BadRequest({
success: false,
message: `${ERROR_MESSAGE} Missing ${SALEOR_AUTHORIZATION_BEARER_HEADER} header.`,
2022-08-03 18:51:37 +00:00
});
}
let tokenClaims: DashboardTokenPayload;
2022-08-03 18:51:37 +00:00
try {
tokenClaims = jose.decodeJwt(token as string) as DashboardTokenPayload;
2022-08-03 18:51:37 +00:00
} catch (e) {
return Response.BadRequest({
success: false,
message: `${ERROR_MESSAGE} Could not decode authorization token.`,
2022-08-03 18:51:37 +00:00
});
}
if (tokenClaims.iss !== domain) {
2022-08-03 18:51:37 +00:00
return Response.BadRequest({
success: false,
message: `${ERROR_MESSAGE} Token iss property is different than domain header.`,
2022-08-03 18:51:37 +00:00
});
}
let appId: string | undefined;
try {
appId = await getAppId(request);
} catch (error) {
return Response.InternalServerError({
2022-08-03 18:51:37 +00:00
success: false,
message: `${ERROR_MESSAGE} Could not obtain the app ID.`,
2022-08-03 18:51:37 +00:00
});
}
if (!appId) {
return Response.InternalServerError({
2022-08-03 18:51:37 +00:00
success: false,
message: `${ERROR_MESSAGE} No value for app ID.`,
2022-08-03 18:51:37 +00:00
});
}
if (tokenClaims.app !== appId) {
return Response.BadRequest({
success: false,
message: `${ERROR_MESSAGE} Token's app property is different than app ID.`,
});
}
2022-08-03 18:51:37 +00:00
try {
2022-08-08 14:55:57 +00:00
const JWKS = jose.createRemoteJWKSet(new URL(getJwksUrl(domain)));
await jose.jwtVerify(token, JWKS);
2022-08-03 18:51:37 +00:00
} catch (e) {
console.error(e);
return Response.BadRequest({
success: false,
message: `${ERROR_MESSAGE} JWT signature verification failed.`,
2022-08-03 18:51:37 +00:00
});
}
return handler(request);
};