Skip to content

Commit

Permalink
feat(sso): add domain configured for sso check before login
Browse files Browse the repository at this point in the history
  • Loading branch information
marc.sirisak committed Oct 10, 2024
1 parent 0119114 commit 09a943b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
4 changes: 4 additions & 0 deletions modules/tchap-translations/tchap_translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -873,5 +873,9 @@
"auth|proconnect|or": {
"en": "or",
"fr": "ou"
},
"auth|proconnect|error_sso_inactive": {
"en": "ProConnect is not activated for your domain",
"fr": "Vous ne pouvez pas vous connecter avec ProConnect avec votre domaine"
}
}
24 changes: 18 additions & 6 deletions src/tchap/components/views/sso/EmailVerificationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ export default function EmailVerificationPage() {

}

const isSSOFlowActive = async (login: Login): Promise<boolean> => {
const flows = await login.getFlows();
return !!flows?.find((flow: Record<string, any>) => flow.type === "m.login.sso");
}

const onSubmit = async (event: React.FormEvent): Promise<void> => {
event.preventDefault();
setLoading(true);
const isFieldCorrect = await emailFieldRef.current?.validate({ allowEmpty: false });

if (!isFieldCorrect) {
displayError(_td("auth|proconnect|error_email"));
displayError(_t("auth|proconnect|error_email"));
return;
}

Expand All @@ -81,24 +86,31 @@ export default function EmailVerificationPage() {
return;
}

const login = new Login(hs.base_url, hs.base_url, null, {});

const matrixClient= login.createTemporaryClient();

const validatedServerConfig = await setUpCurrentHs(hs);

if (!validatedServerConfig) {
displayError(_td("auth|proconnect|error_homeserver"));
displayError(_t("auth|proconnect|error_homeserver"));
return
}

const login = new Login(hs.base_url, hs.base_url, null, {});

const matrixClient= login.createTemporaryClient();
// check if oidc is activated on HS
const canSSO = await isSSOFlowActive(login);
if (!canSSO) {
displayError(_t("auth|proconnect|error_sso_inactive"));
return
}

// start SSO flow since we got the homeserver
PlatformPeg.get()?.startSingleSignOn(matrixClient, "sso", "/home", "", SSOAction.LOGIN);

setLoading(false);

} catch(err) {
displayError(_td("auth|proconnect|error"));
displayError(_t("auth|proconnect|error"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ describe("<EmailVerificationPage />", () => {
const PlatformPegMocked: MockedObject<BasePlatform> = mockPlatformPeg();
const mockedClient: MatrixClient = stubClient();
const mockedTchapUtils = mocked(TchapUtils);

const mockLoginObject = (hs: string = defaultHsUrl) => {
const mockLoginObject = mocked(new Login(hs, hs, null, {}));
mockLoginObject.createTemporaryClient.mockImplementation(() => mockedClient);
return mockLoginObject;
};
const mockedLogin = Login as jest.Mock;

const mockedFetchHomeserverFromEmail = (hs: string = defaultHsUrl) => {
mockedTchapUtils.fetchHomeserverForEmail.mockImplementation(() =>
Expand Down Expand Up @@ -68,7 +63,11 @@ describe("<EmailVerificationPage />", () => {
const renderEmailVerificationPage = () => render(<EmailVerificationPage />);

beforeEach(() => {
mockLoginObject(defaultHsUrl);
mockedLogin.mockImplementation(() => ({
hsUrl: defaultHsUrl,
createTemporaryClient: jest.fn().mockReturnValue(mockedClient),
getFlows: jest.fn().mockResolvedValue([{ type: "m.login.sso" }]),
}));
});

afterEach(() => {
Expand Down Expand Up @@ -207,4 +206,31 @@ describe("<EmailVerificationPage />", () => {
});
expect(PlatformPegMocked.startSingleSignOn).toHaveBeenCalledTimes(1);
});

it("should display error when sso is not configured in homeserer", async () => {
const { container } = renderEmailVerificationPage();

// Mock the implementation without error, what we want is to be sure they are called with the correct parameters
mockedFetchHomeserverFromEmail(secondHsUrl);
mockedValidatedServerConfig(false, secondHsUrl);
mockedPlatformPegStartSSO(false);
// get flow without sso configured on homeserver
mockedLogin.mockImplementation(() => ({
hsUrl: secondHsUrl,
createTemporaryClient: jest.fn().mockReturnValue(mockedClient),
getFlows: jest.fn().mockResolvedValue([{ type: "m.login.password" }]),
}));
// Put text in email field
const emailField = screen.getByRole("textbox");
fireEvent.focus(emailField);
fireEvent.change(emailField, { target: { value: userEmail } });

// click on proconnect button
const proconnectButton = screen.getByTestId("proconnect-submit");
await act(async () => {
await fireEvent.click(proconnectButton);
});

expect(container.getElementsByClassName("mx_ErrorMessage").length).toBe(1);
});
});

0 comments on commit 09a943b

Please sign in to comment.