Update types on headers util
This commit is contained in:
parent
0bd2be9a5f
commit
eb062c6b10
4 changed files with 32 additions and 7 deletions
|
@ -5,9 +5,14 @@ import {
|
||||||
SALEOR_SIGNATURE_HEADER,
|
SALEOR_SIGNATURE_HEADER,
|
||||||
} from "./const";
|
} from "./const";
|
||||||
|
|
||||||
export const getSaleorHeaders = (headers: { [name: string]: any }) => ({
|
const toStringOrUndefined = (value: string | string[] | undefined) =>
|
||||||
domain: headers[SALEOR_DOMAIN_HEADER],
|
value ? value.toString() : undefined;
|
||||||
authorizationBearer: headers[SALEOR_AUTHORIZATION_BEARER_HEADER],
|
|
||||||
signature: headers[SALEOR_SIGNATURE_HEADER],
|
export const getSaleorHeaders = (headers: {
|
||||||
event: headers[SALEOR_EVENT_HEADER],
|
[name: string]: string | string[] | undefined;
|
||||||
|
}): Record<string, string | undefined> => ({
|
||||||
|
domain: toStringOrUndefined(headers[SALEOR_DOMAIN_HEADER]),
|
||||||
|
authorizationBearer: toStringOrUndefined(headers[SALEOR_AUTHORIZATION_BEARER_HEADER]),
|
||||||
|
signature: toStringOrUndefined(headers[SALEOR_SIGNATURE_HEADER]),
|
||||||
|
event: toStringOrUndefined(headers[SALEOR_EVENT_HEADER]),
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as jose from "jose";
|
||||||
import type { Middleware, Request } from "retes";
|
import type { Middleware, Request } from "retes";
|
||||||
import { Response } from "retes/response";
|
import { Response } from "retes/response";
|
||||||
|
|
||||||
import { SALEOR_AUTHORIZATION_BEARER_HEADER } from "../const";
|
import { SALEOR_AUTHORIZATION_BEARER_HEADER, SALEOR_DOMAIN_HEADER } from "../const";
|
||||||
import { getSaleorHeaders } from "../headers";
|
import { getSaleorHeaders } from "../headers";
|
||||||
import { getJwksUrl } from "../urls";
|
import { getJwksUrl } from "../urls";
|
||||||
|
|
||||||
|
@ -24,6 +24,13 @@ export const withJWTVerified =
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (domain === undefined) {
|
||||||
|
return Response.BadRequest({
|
||||||
|
success: false,
|
||||||
|
message: `${ERROR_MESSAGE} Missing ${SALEOR_DOMAIN_HEADER} header.`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let tokenClaims: DashboardTokenPayload;
|
let tokenClaims: DashboardTokenPayload;
|
||||||
try {
|
try {
|
||||||
tokenClaims = jose.decodeJwt(token as string) as DashboardTokenPayload;
|
tokenClaims = jose.decodeJwt(token as string) as DashboardTokenPayload;
|
||||||
|
|
|
@ -9,6 +9,12 @@ export const withRegisteredSaleorDomainHeader =
|
||||||
(handler) =>
|
(handler) =>
|
||||||
async (request) => {
|
async (request) => {
|
||||||
const { domain: saleorDomain } = getSaleorHeaders(request.headers);
|
const { domain: saleorDomain } = getSaleorHeaders(request.headers);
|
||||||
|
if (!saleorDomain) {
|
||||||
|
return Response.BadRequest({
|
||||||
|
success: false,
|
||||||
|
message: "Domain header missing.",
|
||||||
|
});
|
||||||
|
}
|
||||||
const authData = await apl.get(saleorDomain);
|
const authData = await apl.get(saleorDomain);
|
||||||
if (!authData) {
|
if (!authData) {
|
||||||
return Response.Forbidden({
|
return Response.Forbidden({
|
||||||
|
|
|
@ -3,7 +3,7 @@ import * as jose from "jose";
|
||||||
import { Middleware } from "retes";
|
import { Middleware } from "retes";
|
||||||
import { Response } from "retes/response";
|
import { Response } from "retes/response";
|
||||||
|
|
||||||
import { SALEOR_SIGNATURE_HEADER } from "../const";
|
import { SALEOR_DOMAIN_HEADER, SALEOR_SIGNATURE_HEADER } from "../const";
|
||||||
import { getSaleorHeaders } from "../headers";
|
import { getSaleorHeaders } from "../headers";
|
||||||
import { getJwksUrl } from "../urls";
|
import { getJwksUrl } from "../urls";
|
||||||
|
|
||||||
|
@ -29,6 +29,13 @@ export const withWebhookSignatureVerified =
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!saleorDomain) {
|
||||||
|
return Response.BadRequest({
|
||||||
|
success: false,
|
||||||
|
message: `${ERROR_MESSAGE} Missing ${SALEOR_DOMAIN_HEADER} header.`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (secretKey !== undefined) {
|
if (secretKey !== undefined) {
|
||||||
const calculatedSignature = crypto
|
const calculatedSignature = crypto
|
||||||
.createHmac("sha256", secretKey)
|
.createHmac("sha256", secretKey)
|
||||||
|
|
Loading…
Reference in a new issue