Add test for urls
This commit is contained in:
parent
758e8646fc
commit
cba596c910
4 changed files with 48 additions and 8 deletions
|
@ -12,6 +12,7 @@
|
||||||
"ignorePatterns": ["pnpm-lock.yaml"],
|
"ignorePatterns": ["pnpm-lock.yaml"],
|
||||||
"plugins": ["simple-import-sort", "@typescript-eslint"],
|
"plugins": ["simple-import-sort", "@typescript-eslint"],
|
||||||
"rules": {
|
"rules": {
|
||||||
|
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}],
|
||||||
"quotes": ["error", "double"],
|
"quotes": ["error", "double"],
|
||||||
"react/react-in-jsx-scope": "off", // next does not require react imports
|
"react/react-in-jsx-scope": "off", // next does not require react imports
|
||||||
"import/extensions": "off", // file extension not required when importing
|
"import/extensions": "off", // file extension not required when importing
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { Response } from "retes/response";
|
||||||
|
|
||||||
import { SALEOR_AUTHORIZATION_BEARER_HEADER, SALEOR_SIGNATURE_HEADER } from "./const";
|
import { SALEOR_AUTHORIZATION_BEARER_HEADER, SALEOR_SIGNATURE_HEADER } from "./const";
|
||||||
import { getSaleorHeaders } from "./headers";
|
import { getSaleorHeaders } from "./headers";
|
||||||
import { jwksUrl } from "./urls";
|
import { getJwksUrl } from "./urls";
|
||||||
|
|
||||||
export const withBaseURL: Middleware = (handler) => async (request) => {
|
export const withBaseURL: Middleware = (handler) => async (request) => {
|
||||||
const { host, "x-forwarded-proto": protocol = "http" } = request.headers;
|
const { host, "x-forwarded-proto": protocol = "http" } = request.headers;
|
||||||
|
@ -100,7 +100,7 @@ export const withWebhookSignatureVerified =
|
||||||
};
|
};
|
||||||
|
|
||||||
const remoteJwks = jose.createRemoteJWKSet(
|
const remoteJwks = jose.createRemoteJWKSet(
|
||||||
new URL(jwksUrl(saleorDomain))
|
new URL(getJwksUrl(saleorDomain))
|
||||||
) as jose.FlattenedVerifyGetKey;
|
) as jose.FlattenedVerifyGetKey;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -176,7 +176,7 @@ export const withJWTVerified =
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const JWKS = jose.createRemoteJWKSet(new URL(jwksUrl(domain)));
|
const JWKS = jose.createRemoteJWKSet(new URL(getJwksUrl(domain)));
|
||||||
await jose.jwtVerify(token, JWKS);
|
await jose.jwtVerify(token, JWKS);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|
26
src/urls.test.ts
Normal file
26
src/urls.test.ts
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
import { getGraphQLUrl, getJwksUrl } from "./urls";
|
||||||
|
|
||||||
|
describe("urls.ts", () => {
|
||||||
|
describe("jwksUrl function", () => {
|
||||||
|
test.each([
|
||||||
|
["localhost:8000", "http://localhost:8000/.well-known/jwks.json"],
|
||||||
|
[
|
||||||
|
"https://my-saleor.saleor.cloud",
|
||||||
|
"https://https://my-saleor.saleor.cloud/.well-known/jwks.json",
|
||||||
|
],
|
||||||
|
])("resolves %s to be %s", (input, expectedOutput) => {
|
||||||
|
expect(getJwksUrl(input)).toBe(expectedOutput);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("graphQLUrl function", () => {
|
||||||
|
test.each([
|
||||||
|
["localhost:8000", "http://localhost:8000/graphql/"],
|
||||||
|
["https://my-saleor.saleor.cloud", "https://https://my-saleor.saleor.cloud/graphql/"],
|
||||||
|
])("resolves %s to be %s", (input, expectedOutput) => {
|
||||||
|
expect(getGraphQLUrl(input)).toBe(expectedOutput);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
23
src/urls.ts
23
src/urls.ts
|
@ -1,8 +1,21 @@
|
||||||
const urlProtocol = (saleorDomain: string): string =>
|
/**
|
||||||
|
* TODO: Validate proper URL
|
||||||
|
*/
|
||||||
|
const resolveUrlProtocol = (saleorDomain: string): string =>
|
||||||
saleorDomain === "localhost:8000" ? "http" : "https";
|
saleorDomain === "localhost:8000" ? "http" : "https";
|
||||||
|
|
||||||
export const jwksUrl = (saleorDomain: string): string =>
|
export const getJwksUrl = (saleorDomain: string): string =>
|
||||||
`${urlProtocol(saleorDomain)}://${saleorDomain}/.well-known/jwks.json`;
|
`${resolveUrlProtocol(saleorDomain)}://${saleorDomain}/.well-known/jwks.json`;
|
||||||
|
|
||||||
export const graphQLUrl = (saleorDomain: string): string =>
|
export const getGraphQLUrl = (saleorDomain: string): string =>
|
||||||
`${urlProtocol(saleorDomain)}://${saleorDomain}/graphql/`;
|
`${resolveUrlProtocol(saleorDomain)}://${saleorDomain}/graphql/`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Remove in v1, left for compatibility
|
||||||
|
*/
|
||||||
|
export const jwksUrl = getJwksUrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Remove in v1, left for compatibility
|
||||||
|
*/
|
||||||
|
export const graphQLUrl = getGraphQLUrl;
|
||||||
|
|
Loading…
Reference in a new issue