Skip to content

Commit

Permalink
Support for member session authZ for RBAC
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-stytch committed Nov 14, 2023
1 parent 6c88103 commit 274d789
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type requestConfig = {
params?: Record<string, string | number>;
data?: unknown;
dataRaw?: BodyInit;
headers?: Record<string, string>;
};

export async function request<T>(
Expand All @@ -27,17 +28,20 @@ export async function request<T>(
);
}

const finalHeaders = { ...fetchConfig.headers, ...requestConfig.headers };

let response: Response;
try {
const body: BodyInit | undefined = requestConfig.data
? JSON.stringify(requestConfig.data)
: requestConfig.dataRaw;

response = await fetch(url.toString(), {
...fetchConfig,
method: requestConfig.method,
body: body,
headers: finalHeaders,
cache: "no-store",
...fetchConfig,
});
} catch (e) {
const err = e as Error;
Expand Down
18 changes: 18 additions & 0 deletions lib/shared/method_options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface Authorization {
// A secret token for a given Stytch Session.
session_token?: string;
// The JSON Web Token (JWT) for a given Stytch Session.
session_jwt?: string;
}

export function addAuthorizationHeaders(
headers: Record<string, string>,
authorization: Authorization,
): void {
if (authorization.session_token) {
headers["X-Stytch-Member-Session"] = authorization.session_token;
}
if (authorization.session_jwt) {
headers["X-Stytch-Member-SessionJWT"] = authorization.session_jwt;
}
}
8 changes: 4 additions & 4 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { request, requestConfig } from "../lib/shared";
import * as http from "http";
import { request, fetchConfig, requestConfig } from "../lib/shared";
import * as undici from "undici";

export type Response = {
status: number;
Expand All @@ -13,13 +13,13 @@ export type Request = {
data?: unknown;
};

export const MOCK_FETCH_CONFIG = {
export const MOCK_FETCH_CONFIG: fetchConfig = {
baseURL: "https://example.net",
headers: {
"User-Agent": `Stytch Node vTEST`,
},
timeout: 100,
agent: { mock: "agent" } as unknown as http.Agent,
dispatcher: { mock: "agent" } as unknown as undici.Dispatcher,
};

export function mockRequest(
Expand Down
18 changes: 18 additions & 0 deletions test/shared/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ describe("request", () => {
});
});

test("Merges headers passed through as options", async () => {
mockResponse({ key: "value" }, 200);
await request(MOCK_FETCH_CONFIG, {
url: "http://localhost:8000/hello",
method: "POST",
headers: { "app-header": "passed through" }
});

expect(fetchMock).toHaveBeenCalledWith("http://localhost:8000/hello", {
method: "POST",
...MOCK_FETCH_CONFIG,
headers: {
...MOCK_FETCH_CONFIG.headers,
"app-header": "passed through",
}
});
});

test("error response throws inspectable error", async () => {
expect.assertions(2);

Expand Down

0 comments on commit 274d789

Please sign in to comment.