Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ dist-ssr
*.sw?
coverage

api_
api_
6 changes: 3 additions & 3 deletions lib/sessionManager/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export type StorageSettingsType = {
onRefreshHandler?: (refreshType: RefreshType) => Promise<RefreshTokenResult>;
};

export abstract class SessionBase<V extends string = StorageKeys>
implements SessionManager<V>
{
export abstract class SessionBase<
V extends string = StorageKeys,
> implements SessionManager<V> {
abstract asyncStore: boolean;
private listeners: Set<StoreListener> = new Set();
private notificationScheduled = false;
Expand Down
65 changes: 65 additions & 0 deletions lib/utils/exchangeAuthCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,69 @@ describe("exchangeAuthCode", () => {
),
});
});

it("sends a sanitized redirect_uri so /token matches the /authorize value", async () => {
const store = new MemoryStorage();
setActiveStorage(store);

await store.setItems({
[StorageKeys.state]: "abc",
[StorageKeys.codeVerifier]: "verifier",
});

const urlParams = new URLSearchParams({ state: "abc", code: "xyz" });

fetchMock.mockResponseOnce(
JSON.stringify({
access_token: "access_token",
refresh_token: "refresh_token",
id_token: "id_token",
}),
);

await exchangeAuthCode({
urlParams,
domain: "https://example.kinde.com",
clientId: "test-client",
redirectURL: "https://app.example.com/",
});

const [, requestInit] = fetchMock.mock.calls.at(-1)!;
const body = requestInit?.body as URLSearchParams;

expect(body.get("redirect_uri")).toBe("https://app.example.com");
});

it("preserves raw redirect_uri when disableUrlSanitization is true", async () => {
const store = new MemoryStorage();
setActiveStorage(store);

await store.setItems({
[StorageKeys.state]: "abc",
[StorageKeys.codeVerifier]: "verifier",
});

const urlParams = new URLSearchParams({ state: "abc", code: "xyz" });

fetchMock.mockResponseOnce(
JSON.stringify({
access_token: "access_token",
refresh_token: "refresh_token",
id_token: "id_token",
}),
);

await exchangeAuthCode({
urlParams,
domain: "https://example.kinde.com",
clientId: "test-client",
redirectURL: "https://app.example.com/",
disableUrlSanitization: true,
});

const [, requestInit] = fetchMock.mock.calls.at(-1)!;
const body = requestInit?.body as URLSearchParams;

expect(body.get("redirect_uri")).toBe("https://app.example.com/");
});
});
7 changes: 6 additions & 1 deletion lib/utils/exchangeAuthCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { isCustomDomain } from ".";
import { clearRefreshTimer, setRefreshTimer } from "./refreshTimer";
import { isClient } from "./isClient";
import { sanitizeUrl } from "./sanitizeUrl";

export const frameworkSettings: {
framework: string;
Expand All @@ -28,6 +29,7 @@ interface ExchangeAuthCodeParams {
redirectURL: string;
autoRefresh?: boolean;
onRefresh?: (data: RefreshTokenResult) => void;
disableUrlSanitization?: boolean;
}

type ExchangeAuthCodeResultSuccess = {
Expand Down Expand Up @@ -72,6 +74,7 @@ export const exchangeAuthCode = async ({
redirectURL,
autoRefresh = false,
onRefresh,
disableUrlSanitization = false,
}: ExchangeAuthCodeParams): Promise<ExchangeAuthCodeResult> => {
const state = urlParams.get("state");
const code = urlParams.get("code");
Expand Down Expand Up @@ -143,7 +146,9 @@ export const exchangeAuthCode = async ({
code,
code_verifier: codeVerifier,
grant_type: "authorization_code",
redirect_uri: redirectURL,
redirect_uri: disableUrlSanitization
? redirectURL
: sanitizeUrl(redirectURL),
});

if (clientSecret) {
Expand Down