diff --git a/.eslintrc b/.eslintrc index 3f351a7..8541367 100644 --- a/.eslintrc +++ b/.eslintrc @@ -12,6 +12,7 @@ "ignorePatterns": ["pnpm-lock.yaml"], "plugins": ["simple-import-sort", "@typescript-eslint"], "rules": { + "import/no-extraneous-dependencies": ["error", {"devDependencies": true}], "quotes": ["error", "double"], "react/react-in-jsx-scope": "off", // next does not require react imports "import/extensions": "off", // file extension not required when importing diff --git a/src/middleware.ts b/src/middleware.ts index 5eb6c39..8442042 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -5,7 +5,7 @@ import { Response } from "retes/response"; import { SALEOR_AUTHORIZATION_BEARER_HEADER, SALEOR_SIGNATURE_HEADER } from "./const"; import { getSaleorHeaders } from "./headers"; -import { jwksUrl } from "./urls"; +import { getJwksUrl } from "./urls"; export const withBaseURL: Middleware = (handler) => async (request) => { const { host, "x-forwarded-proto": protocol = "http" } = request.headers; @@ -100,7 +100,7 @@ export const withWebhookSignatureVerified = }; const remoteJwks = jose.createRemoteJWKSet( - new URL(jwksUrl(saleorDomain)) + new URL(getJwksUrl(saleorDomain)) ) as jose.FlattenedVerifyGetKey; try { @@ -176,7 +176,7 @@ export const withJWTVerified = } try { - const JWKS = jose.createRemoteJWKSet(new URL(jwksUrl(domain))); + const JWKS = jose.createRemoteJWKSet(new URL(getJwksUrl(domain))); await jose.jwtVerify(token, JWKS); } catch (e) { console.error(e); diff --git a/src/urls.test.ts b/src/urls.test.ts new file mode 100644 index 0000000..be79d6a --- /dev/null +++ b/src/urls.test.ts @@ -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); + }); + }); +}); diff --git a/src/urls.ts b/src/urls.ts index 4dab213..3386940 100644 --- a/src/urls.ts +++ b/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"; -export const jwksUrl = (saleorDomain: string): string => - `${urlProtocol(saleorDomain)}://${saleorDomain}/.well-known/jwks.json`; +export const getJwksUrl = (saleorDomain: string): string => + `${resolveUrlProtocol(saleorDomain)}://${saleorDomain}/.well-known/jwks.json`; -export const graphQLUrl = (saleorDomain: string): string => - `${urlProtocol(saleorDomain)}://${saleorDomain}/graphql/`; +export const getGraphQLUrl = (saleorDomain: string): string => + `${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;