OIDC: use delegated auth account URL from OidcClientStore
(#11723)
* test persistCredentials without a pickle key * test setLoggedIn with pickle key * lint * type error * extract token persisting code into function, persist refresh token * store has_refresh_token too * pass refreshToken from oidcAuthGrant into credentials * rest restore session with pickle key * retreive stored refresh token and add to credentials * extract token decryption into function * remove TODO * very messy poc * utils to persist clientId and issuer after oidc authentication * add dep oidc-client-ts * persist issuer and clientId after successful oidc auth * add OidcClientStore * comments and tidy * expose getters for stored refresh and access tokens in Lifecycle * revoke tokens with oidc provider * test logout action in MatrixChat * comments * prettier * test OidcClientStore.revokeTokens * put pickle key destruction back * comment pedantry * working refresh without persistence * extract token persistence functions to utils * add sugar * implement TokenRefresher class with persistence * tidying * persist idTokenClaims * persist idTokenClaims * tests * remove unused cde * create token refresher during doSetLoggedIn * tidying * also tidying * OidcClientStore.initClient use stored issuer when client well known unavailable * test Lifecycle.logout * update Lifecycle test replaceUsingCreds calls * fix test * add sdkContext to UserSettingsDialog * use sdkContext and oidcClientStore in session manager * use sdkContext and OidcClientStore in generalusersettingstab * tidy * test tokenrefresher creation in login flow * test token refresher * Update src/utils/oidc/TokenRefresher.ts Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * use literal value for m.authentication Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> * improve comments * fix test mock, comment * typo * add sdkContext to SoftLogout, pass oidcClientStore to logout * fullstops * comments * fussy comment formatting --------- Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
This commit is contained in:
parent
84ca519b3f
commit
d9d52fba8c
10 changed files with 96 additions and 179 deletions
|
@ -13,11 +13,11 @@ limitations under the License.
|
|||
|
||||
import { fireEvent, render, screen, within } from "@testing-library/react";
|
||||
import React from "react";
|
||||
import { M_AUTHENTICATION, ThreepidMedium } from "matrix-js-sdk/src/matrix";
|
||||
import { ThreepidMedium } from "matrix-js-sdk/src/matrix";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
import GeneralUserSettingsTab from "../../../../../../src/components/views/settings/tabs/user/GeneralUserSettingsTab";
|
||||
import MatrixClientContext from "../../../../../../src/contexts/MatrixClientContext";
|
||||
import { SdkContextClass, SDKContext } from "../../../../../../src/contexts/SDKContext";
|
||||
import SettingsStore from "../../../../../../src/settings/SettingsStore";
|
||||
import {
|
||||
getMockClientWithEventEmitter,
|
||||
|
@ -28,6 +28,7 @@ import {
|
|||
} from "../../../../../test-utils";
|
||||
import { UIFeature } from "../../../../../../src/settings/UIFeature";
|
||||
import { SettingLevel } from "../../../../../../src/settings/SettingLevel";
|
||||
import { OidcClientStore } from "../../../../../../src/stores/oidc/OidcClientStore";
|
||||
|
||||
describe("<GeneralUserSettingsTab />", () => {
|
||||
const defaultProps = {
|
||||
|
@ -44,19 +45,18 @@ describe("<GeneralUserSettingsTab />", () => {
|
|||
deleteThreePid: jest.fn(),
|
||||
});
|
||||
|
||||
const getComponent = () => (
|
||||
<MatrixClientContext.Provider value={mockClient}>
|
||||
<GeneralUserSettingsTab {...defaultProps} />
|
||||
</MatrixClientContext.Provider>
|
||||
);
|
||||
let stores: SdkContextClass;
|
||||
|
||||
const clientWellKnownSpy = jest.spyOn(mockClient, "getClientWellKnown");
|
||||
const getComponent = () => (
|
||||
<SDKContext.Provider value={stores}>
|
||||
<GeneralUserSettingsTab {...defaultProps} />
|
||||
</SDKContext.Provider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
|
||||
mockPlatformPeg();
|
||||
jest.clearAllMocks();
|
||||
clientWellKnownSpy.mockReturnValue({});
|
||||
jest.spyOn(SettingsStore, "getValue").mockRestore();
|
||||
jest.spyOn(logger, "error").mockRestore();
|
||||
|
||||
|
@ -67,6 +67,12 @@ describe("<GeneralUserSettingsTab />", () => {
|
|||
mockClient.deleteThreePid.mockResolvedValue({
|
||||
id_server_unbind_result: "success",
|
||||
});
|
||||
|
||||
stores = new SdkContextClass();
|
||||
stores.client = mockClient;
|
||||
// stub out this store completely to avoid mocking initialisation
|
||||
const mockOidcClientStore = {} as unknown as OidcClientStore;
|
||||
jest.spyOn(stores, "oidcClientStore", "get").mockReturnValue(mockOidcClientStore);
|
||||
});
|
||||
|
||||
it("does not show account management link when not available", () => {
|
||||
|
@ -78,12 +84,11 @@ describe("<GeneralUserSettingsTab />", () => {
|
|||
|
||||
it("show account management link in expected format", async () => {
|
||||
const accountManagementLink = "https://id.server.org/my-account";
|
||||
clientWellKnownSpy.mockReturnValue({
|
||||
[M_AUTHENTICATION.name]: {
|
||||
issuer: "https://id.server.org",
|
||||
account: accountManagementLink,
|
||||
},
|
||||
});
|
||||
const mockOidcClientStore = {
|
||||
accountManagementEndpoint: accountManagementLink,
|
||||
} as unknown as OidcClientStore;
|
||||
jest.spyOn(stores, "oidcClientStore", "get").mockReturnValue(mockOidcClientStore);
|
||||
|
||||
const { getByTestId } = render(getComponent());
|
||||
|
||||
// wait for well-known call to settle
|
||||
|
@ -167,12 +172,11 @@ describe("<GeneralUserSettingsTab />", () => {
|
|||
(settingName) => settingName === UIFeature.Deactivate,
|
||||
);
|
||||
// account is managed externally when we have delegated auth configured
|
||||
mockClient.getClientWellKnown.mockReturnValue({
|
||||
[M_AUTHENTICATION.name]: {
|
||||
issuer: "https://issuer.org",
|
||||
account: "https://issuer.org/account",
|
||||
},
|
||||
});
|
||||
const accountManagementLink = "https://id.server.org/my-account";
|
||||
const mockOidcClientStore = {
|
||||
accountManagementEndpoint: accountManagementLink,
|
||||
} as unknown as OidcClientStore;
|
||||
jest.spyOn(stores, "oidcClientStore", "get").mockReturnValue(mockOidcClientStore);
|
||||
render(getComponent());
|
||||
|
||||
await flushPromises();
|
||||
|
|
|
@ -32,9 +32,9 @@ import {
|
|||
CryptoApi,
|
||||
DeviceVerificationStatus,
|
||||
MatrixError,
|
||||
M_AUTHENTICATION,
|
||||
MatrixClient,
|
||||
} from "matrix-js-sdk/src/matrix";
|
||||
import { mocked } from "jest-mock";
|
||||
import { mocked, MockedObject } from "jest-mock";
|
||||
|
||||
import {
|
||||
clearAllModals,
|
||||
|
@ -45,13 +45,14 @@ import {
|
|||
mockPlatformPeg,
|
||||
} from "../../../../../test-utils";
|
||||
import SessionManagerTab from "../../../../../../src/components/views/settings/tabs/user/SessionManagerTab";
|
||||
import MatrixClientContext from "../../../../../../src/contexts/MatrixClientContext";
|
||||
import Modal from "../../../../../../src/Modal";
|
||||
import LogoutDialog from "../../../../../../src/components/views/dialogs/LogoutDialog";
|
||||
import { DeviceSecurityVariation, ExtendedDevice } from "../../../../../../src/components/views/settings/devices/types";
|
||||
import { INACTIVE_DEVICE_AGE_MS } from "../../../../../../src/components/views/settings/devices/filter";
|
||||
import SettingsStore from "../../../../../../src/settings/SettingsStore";
|
||||
import { getClientInformationEventType } from "../../../../../../src/utils/device/clientInformation";
|
||||
import { SDKContext, SdkContextClass } from "../../../../../../src/contexts/SDKContext";
|
||||
import { OidcClientStore } from "../../../../../../src/stores/oidc/OidcClientStore";
|
||||
|
||||
mockPlatformPeg();
|
||||
|
||||
|
@ -91,31 +92,14 @@ describe("<SessionManagerTab />", () => {
|
|||
requestDeviceVerification: jest.fn().mockResolvedValue(mockVerificationRequest),
|
||||
} as unknown as CryptoApi);
|
||||
|
||||
let mockClient = getMockClientWithEventEmitter({
|
||||
...mockClientMethodsUser(aliceId),
|
||||
getCrypto: jest.fn().mockReturnValue(mockCrypto),
|
||||
getDevices: jest.fn(),
|
||||
getStoredDevice: jest.fn(),
|
||||
getDeviceId: jest.fn().mockReturnValue(deviceId),
|
||||
deleteMultipleDevices: jest.fn(),
|
||||
generateClientSecret: jest.fn(),
|
||||
setDeviceDetails: jest.fn(),
|
||||
getAccountData: jest.fn(),
|
||||
deleteAccountData: jest.fn(),
|
||||
doesServerSupportUnstableFeature: jest.fn().mockResolvedValue(true),
|
||||
getPushers: jest.fn(),
|
||||
setPusher: jest.fn(),
|
||||
setLocalNotificationSettings: jest.fn(),
|
||||
getVersions: jest.fn().mockResolvedValue({}),
|
||||
getCapabilities: jest.fn().mockResolvedValue({}),
|
||||
getClientWellKnown: jest.fn().mockReturnValue({}),
|
||||
});
|
||||
let mockClient!: MockedObject<MatrixClient>;
|
||||
let sdkContext: SdkContextClass;
|
||||
|
||||
const defaultProps = {};
|
||||
const getComponent = (props = {}): React.ReactElement => (
|
||||
<MatrixClientContext.Provider value={mockClient}>
|
||||
<SDKContext.Provider value={sdkContext}>
|
||||
<SessionManagerTab {...defaultProps} {...props} />
|
||||
</MatrixClientContext.Provider>
|
||||
</SDKContext.Provider>
|
||||
);
|
||||
|
||||
const toggleDeviceDetails = (
|
||||
|
@ -230,6 +214,9 @@ describe("<SessionManagerTab />", () => {
|
|||
}
|
||||
});
|
||||
|
||||
sdkContext = new SdkContextClass();
|
||||
sdkContext.client = mockClient;
|
||||
|
||||
// @ts-ignore allow delete of non-optional prop
|
||||
delete window.location;
|
||||
// @ts-ignore ugly mocking
|
||||
|
@ -1051,12 +1038,11 @@ describe("<SessionManagerTab />", () => {
|
|||
|
||||
describe("for an OIDC-aware server", () => {
|
||||
beforeEach(() => {
|
||||
mockClient.getClientWellKnown.mockReturnValue({
|
||||
[M_AUTHENTICATION.name]: {
|
||||
issuer: "https://issuer.org",
|
||||
account: "https://issuer.org/account",
|
||||
},
|
||||
});
|
||||
// just do an ugly mock here to avoid mocking initialisation
|
||||
const mockOidcClientStore = {
|
||||
accountManagementEndpoint: "https://issuer.org/account",
|
||||
} as unknown as OidcClientStore;
|
||||
jest.spyOn(sdkContext, "oidcClientStore", "get").mockReturnValue(mockOidcClientStore);
|
||||
});
|
||||
|
||||
// signing out the current device works as usual
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue