Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix too early UserManager.events().unload() event before user is actually signed out #1342

Merged
merged 1 commit into from
Jan 15, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea

# build artifacts
*.tsbuildinfo
Expand Down
56 changes: 55 additions & 1 deletion src/UserManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { once } from "events";
import { RedirectNavigator, type PopupWindow, PopupNavigator, IFrameNavigator } from "./navigators";
import {
RedirectNavigator,
type PopupWindow,
PopupNavigator,
IFrameNavigator,
type NavigateResponse,
} from "./navigators";
import type { SigninResponse } from "./SigninResponse";
import type { SignoutResponse } from "./SignoutResponse";
import { UserManager, type SigninPopupArgs, type SigninRedirectArgs, type SigninSilentArgs, type SignoutSilentArgs } from "./UserManager";
Expand Down Expand Up @@ -33,6 +39,7 @@ describe("UserManager", () => {
userStore: userStoreMock,
metadata: {
authorization_endpoint: "http://sts/oidc/authorize",
end_session_endpoint: "http://sts/oidc/logout",
token_endpoint: "http://sts/oidc/token",
revocation_endpoint: "http://sts/oidc/revoke",
},
Expand Down Expand Up @@ -838,6 +845,53 @@ describe("UserManager", () => {
});
});

describe("signoutRedirect", () => {
it("should not unload user to avoid race condition between actual signout and signout event handlers", async () => {
// arrange
const navigateMock = jest.fn().mockReturnValue(Promise.resolve({
url: "http://localhost:8080",
} as NavigateResponse));
jest.spyOn(subject["_redirectNavigator"], "prepare").mockReturnValue(Promise.resolve({
navigate: navigateMock,
close: () => {},
}));
const user = new User({
access_token: "access_token",
token_type: "token_type",
profile: {} as UserProfile,
});
await subject.storeUser(user);

// act
await subject.signoutRedirect();

// assert
expect(navigateMock).toHaveBeenCalledTimes(1);
const storageString = await subject.settings.userStore.get(subject["_userStoreKey"]);
expect(storageString).not.toBeNull();
});
});

describe("signoutRedirectCallback", () => {
it("should unload user", async () => {
// arrange
const user = new User({
access_token: "access_token",
token_type: "token_type",
profile: {} as UserProfile,
});
await subject.storeUser(user);

expect(await subject.settings.userStore.get(subject["_userStoreKey"])).not.toBeNull();

// act
await subject.signoutRedirectCallback();

// assert
expect(await subject.settings.userStore.get(subject["_userStoreKey"])).toBeNull();
});
});

describe("storeUser", () => {
it("should add user to store", async () => {
// arrange
Expand Down
7 changes: 4 additions & 3 deletions src/UserManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,6 @@ export class UserManager {
args.id_token_hint = id_token;
}

await this.removeUser();
logger.debug("user removed, creating signout request");

const signoutRequest = await this._client.createSignoutRequest(args);
logger.debug("got signout request");

Expand All @@ -638,11 +635,15 @@ export class UserManager {
throw err;
}
}

protected async _signoutEnd(url: string): Promise<SignoutResponse> {
const logger = this._logger.create("_signoutEnd");
const signoutResponse = await this._client.processSignoutResponse(url);
logger.debug("got signout response");

await this.removeUser();
logger.debug("user removed");

return signoutResponse;
}

Expand Down