-
Notifications
You must be signed in to change notification settings - Fork 13.8k
feat: import SAML IdP configuration from a metadata URL #41481
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f50f945
feat: import SAML IdP configuration from a metadata URL
ricardogarim 8778ffe
fix: import SAML SLO url only from HTTP-Redirect bindings
ricardogarim 4c12045
refactor: address review feedback on SAML metadata import
ricardogarim 17329c5
fix: pick the SAML 2.0 IdP role when importing metadata
ricardogarim 98134a5
refactor: move the SAML metadata response validator and use the endpo…
ricardogarim 928d6de
refactor: check FetchError instead of casting the SAML metadata error
ricardogarim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@rocket.chat/rest-typings': patch | ||
| '@rocket.chat/i18n': patch | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Adds an Import IdP metadata option to SAML settings that fetches the Identity Provider metadata from a URL and prefills the matching setting fields — certificate, entry point and IDP SLO redirect URL, plus identifier format on Enterprise — for the admin to review before saving. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
apps/meteor/client/views/admin/settings/groups/SAMLGroupPage/SAMLGroupPage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import type { ISetting } from '@rocket.chat/core-typings'; | ||
| import { Button } from '@rocket.chat/fuselage'; | ||
| import { useStableCallback } from '@rocket.chat/fuselage-hooks'; | ||
| import { useEndpoint, useSetModal, useToastMessageDispatch, useSettingStructure } from '@rocket.chat/ui-contexts'; | ||
| import { memo, useMemo } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import SamlMetadataModal from './SamlMetadataModal'; | ||
| import type { SamlMetadataValues } from './SamlMetadataModal'; | ||
| import { useEditableSettings, useEditableSettingsDispatch } from '../../../EditableSettingsContext'; | ||
| import BaseGroupPage from '../BaseGroupPage'; | ||
|
|
||
| type SAMLGroupPageProps = ISetting & { | ||
| onClickBack?: () => void; | ||
| }; | ||
|
|
||
| function SAMLGroupPage({ _id, i18nLabel, onClickBack, ...group }: SAMLGroupPageProps) { | ||
| const { t } = useTranslation(); | ||
| const dispatchToastMessage = useToastMessageDispatch(); | ||
| const parseMetadata = useEndpoint('POST', '/v1/saml.parseMetadata'); | ||
| const setModal = useSetModal(); | ||
| const closeModal = useStableCallback(() => setModal()); | ||
| const dispatch = useEditableSettingsDispatch(); | ||
|
|
||
| const certSetting = useSettingStructure('SAML_Custom_Default_cert'); | ||
| const entryPointSetting = useSettingStructure('SAML_Custom_Default_entry_point'); | ||
| const sloSetting = useSettingStructure('SAML_Custom_Default_idp_slo_redirect_url'); | ||
| const identifierFormatSetting = useSettingStructure('SAML_Custom_Default_identifier_format'); | ||
|
|
||
| const editableSettings = useEditableSettings(useMemo(() => ({ group: _id }), [_id])); | ||
| const changed = useMemo(() => editableSettings.some(({ changed }) => changed), [editableSettings]); | ||
|
|
||
| const handleApply = useStableCallback((values: SamlMetadataValues) => { | ||
| // identifier_format is only registered on Enterprise installs; skip any setting that isn't present. | ||
| const add = (setting: ISetting | undefined, value?: string) => | ||
| setting && value !== undefined ? [{ _id: setting._id, value, changed: JSON.stringify(setting.value) !== JSON.stringify(value) }] : []; | ||
|
|
||
| const changes = [ | ||
| ...add(certSetting, values.cert), | ||
| ...add(entryPointSetting, values.entryPoint), | ||
| ...add(sloSetting, values.idpSLORedirectURL), | ||
| ...add(identifierFormatSetting, values.identifierFormat), | ||
| ]; | ||
|
|
||
| dispatch(changes); | ||
| closeModal(); | ||
|
|
||
| if (changes.length === 0) { | ||
| dispatchToastMessage({ type: 'warning', message: t('SAML_Metadata_no_values') }); | ||
| return; | ||
| } | ||
|
|
||
| dispatchToastMessage({ type: 'success', message: t('SAML_Metadata_applied') }); | ||
| }); | ||
|
|
||
| const handleImportClick = () => | ||
| setModal( | ||
| <SamlMetadataModal | ||
| onClose={closeModal} | ||
| onFetch={(url) => parseMetadata({ url })} | ||
| onApply={handleApply} | ||
| showIdentifierFormat={identifierFormatSetting !== undefined} | ||
| />, | ||
| ); | ||
|
|
||
| return ( | ||
| <BaseGroupPage | ||
| _id={_id} | ||
| i18nLabel={i18nLabel} | ||
| onClickBack={onClickBack} | ||
| {...group} | ||
| headerButtons={ | ||
| <Button disabled={changed} onClick={handleImportClick}> | ||
| {t('SAML_Import_metadata')} | ||
| </Button> | ||
| } | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export default memo(SAMLGroupPage); | ||
122 changes: 122 additions & 0 deletions
122
apps/meteor/client/views/admin/settings/groups/SAMLGroupPage/SamlMetadataModal.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
|
|
||
| import SamlMetadataModal from './SamlMetadataModal'; | ||
|
|
||
| const setup = (overrides: Partial<Parameters<typeof SamlMetadataModal>[0]> = {}) => { | ||
| const props = { | ||
| onClose: jest.fn(), | ||
| onFetch: jest.fn().mockResolvedValue({ | ||
| cert: 'CERTDATA', | ||
| entryPoint: 'https://idp.test/sso', | ||
| idpSLORedirectURL: 'https://idp.test/slo', | ||
| warnings: [], | ||
| }), | ||
| onApply: jest.fn(), | ||
| ...overrides, | ||
| }; | ||
| render(<SamlMetadataModal {...props} />, { wrapper: mockAppRoot().build() }); | ||
| return props; | ||
| }; | ||
|
|
||
| it('fetches metadata and shows the preview, then applies edited values', async () => { | ||
| const props = setup(); | ||
|
|
||
| await userEvent.type(screen.getByLabelText('SAML_Metadata_url'), 'https://idp.test/metadata.xml'); | ||
| await userEvent.click(screen.getByText('SAML_Metadata_fetch')); | ||
|
|
||
| await waitFor(() => expect(props.onFetch).toHaveBeenCalledWith('https://idp.test/metadata.xml')); | ||
|
|
||
| const entryPointInput = await screen.findByLabelText('SAML_Custom_Entry_point'); | ||
| expect(entryPointInput).toHaveValue('https://idp.test/sso'); | ||
| expect(screen.getByLabelText('SAML_Custom_Cert')).toHaveValue('CERTDATA'); | ||
| expect(screen.getByLabelText('SAML_Custom_IDP_SLO_Redirect_URL')).toHaveValue('https://idp.test/slo'); | ||
|
|
||
| await userEvent.clear(entryPointInput); | ||
| await userEvent.type(entryPointInput, 'https://idp.test/sso-edited'); | ||
| await userEvent.click(screen.getByText('Apply')); | ||
|
|
||
| expect(props.onApply).toHaveBeenCalledWith({ | ||
| cert: 'CERTDATA', | ||
| entryPoint: 'https://idp.test/sso-edited', | ||
| idpSLORedirectURL: 'https://idp.test/slo', | ||
| }); | ||
| }); | ||
|
|
||
| it('shows a warning callout when the fetch result carries warnings', async () => { | ||
| setup({ | ||
| onFetch: jest.fn().mockResolvedValue({ cert: 'CERTDATA', warnings: ['SAML_Metadata_warning_multiple_certs'] }), | ||
| }); | ||
|
|
||
| await userEvent.type(screen.getByLabelText('SAML_Metadata_url'), 'https://idp.test/metadata.xml'); | ||
| await userEvent.click(screen.getByText('SAML_Metadata_fetch')); | ||
|
|
||
| expect(await screen.findByText('SAML_Metadata_warning_multiple_certs')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('returns to Fetch mode when the URL is edited after a successful fetch', async () => { | ||
| setup(); | ||
|
|
||
| await userEvent.type(screen.getByLabelText('SAML_Metadata_url'), 'https://idp.test/metadata.xml'); | ||
| await userEvent.click(screen.getByText('SAML_Metadata_fetch')); | ||
|
|
||
| expect(await screen.findByText('Apply')).toBeInTheDocument(); | ||
|
|
||
| await userEvent.type(screen.getByLabelText('SAML_Metadata_url'), '2'); | ||
|
|
||
| expect(await screen.findByText('SAML_Metadata_fetch')).toBeInTheDocument(); | ||
| expect(screen.queryByText('Apply')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows the Identifier Format row only when showIdentifierFormat is true', async () => { | ||
| setup({ | ||
| showIdentifierFormat: true, | ||
| onFetch: jest.fn().mockResolvedValue({ | ||
| cert: 'CERTDATA', | ||
| entryPoint: 'https://idp.test/sso', | ||
| idpSLORedirectURL: 'https://idp.test/slo', | ||
| identifierFormat: 'urn:oasis:names:tc:SAML:2.0:nameid-format:transient', | ||
| warnings: [], | ||
| }), | ||
| }); | ||
|
|
||
| await userEvent.type(screen.getByLabelText('SAML_Metadata_url'), 'https://idp.test/metadata.xml'); | ||
| await userEvent.click(screen.getByText('SAML_Metadata_fetch')); | ||
|
|
||
| expect(await screen.findByLabelText('SAML_Identifier_Format')).toHaveValue('urn:oasis:names:tc:SAML:2.0:nameid-format:transient'); | ||
| }); | ||
|
|
||
| it('does not show the Identifier Format row when showIdentifierFormat is false', async () => { | ||
| setup(); | ||
|
|
||
| await userEvent.type(screen.getByLabelText('SAML_Metadata_url'), 'https://idp.test/metadata.xml'); | ||
| await userEvent.click(screen.getByText('SAML_Metadata_fetch')); | ||
|
|
||
| expect(await screen.findByLabelText('SAML_Custom_Cert')).toBeInTheDocument(); | ||
| expect(screen.queryByLabelText('SAML_Identifier_Format')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('calls onClose when Cancel is clicked', async () => { | ||
| const props = setup(); | ||
| await userEvent.click(screen.getByText('Cancel')); | ||
| expect(props.onClose).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('shows a danger callout and allows retry when fetch fails', async () => { | ||
| setup({ | ||
| onFetch: jest.fn().mockRejectedValueOnce({ success: false, error: 'SAML_Metadata_fetch_failed' }), | ||
| }); | ||
|
|
||
| await userEvent.type(screen.getByLabelText('SAML_Metadata_url'), 'https://idp.test/metadata.xml'); | ||
| await userEvent.click(screen.getByText('SAML_Metadata_fetch')); | ||
|
|
||
| expect(await screen.findByText('SAML_Metadata_fetch_failed')).toBeInTheDocument(); | ||
|
|
||
| const confirmButton = screen.getByText('SAML_Metadata_fetch'); | ||
| expect(confirmButton).toBeInTheDocument(); | ||
| expect(confirmButton).not.toBeDisabled(); | ||
|
|
||
| const urlInput = screen.getByLabelText('SAML_Metadata_url') as HTMLInputElement; | ||
| expect(urlInput.value).toBe('https://idp.test/metadata.xml'); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.