-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Mock IDP login page and role switcher (#172257)
- Loading branch information
1 parent
1865d4d
commit 7bee86d
Showing
38 changed files
with
684 additions
and
159 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { plugin } from './plugin'; |
This file contains 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,152 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { | ||
EuiButton, | ||
EuiPageTemplate, | ||
EuiEmptyPrompt, | ||
EuiComboBox, | ||
EuiInlineEditTitle, | ||
EuiFormRow, | ||
EuiSpacer, | ||
EuiComboBoxOptionOption, | ||
EuiButtonEmpty, | ||
} from '@elastic/eui'; | ||
import React, { ChangeEvent, FunctionComponent } from 'react'; | ||
import { FormikProvider, useFormik, Field, Form } from 'formik'; | ||
|
||
import { | ||
MOCK_IDP_SECURITY_ROLE_NAMES, | ||
MOCK_IDP_OBSERVABILITY_ROLE_NAMES, | ||
MOCK_IDP_SEARCH_ROLE_NAMES, | ||
} from '@kbn/mock-idp-utils/src/constants'; | ||
import { useAuthenticator } from './role_switcher'; | ||
|
||
export interface LoginPageProps { | ||
projectType?: string; | ||
} | ||
|
||
export const LoginPage: FunctionComponent<LoginPageProps> = ({ projectType }) => { | ||
const roles = | ||
projectType === 'security' | ||
? MOCK_IDP_SECURITY_ROLE_NAMES | ||
: projectType === 'observability' | ||
? MOCK_IDP_OBSERVABILITY_ROLE_NAMES | ||
: MOCK_IDP_SEARCH_ROLE_NAMES; | ||
|
||
const [, switchCurrentUser] = useAuthenticator(true); | ||
const formik = useFormik({ | ||
initialValues: { | ||
full_name: 'Test User', | ||
role: roles[0], | ||
}, | ||
async onSubmit(values) { | ||
await switchCurrentUser({ | ||
username: sanitizeUsername(values.full_name), | ||
full_name: values.full_name, | ||
email: sanitizeEmail(values.full_name), | ||
roles: [values.role], | ||
}); | ||
}, | ||
}); | ||
|
||
return ( | ||
<FormikProvider value={formik}> | ||
<EuiPageTemplate panelled={false}> | ||
<EuiPageTemplate.Section alignment="center"> | ||
<Form> | ||
<EuiEmptyPrompt | ||
iconType="user" | ||
layout="vertical" | ||
color="plain" | ||
body={ | ||
<> | ||
<Field | ||
as={EuiInlineEditTitle} | ||
name="full_name" | ||
heading="h2" | ||
inputAriaLabel="Edit name inline" | ||
value={formik.values.full_name} | ||
onChange={(event: ChangeEvent<HTMLInputElement>) => { | ||
formik.setFieldValue('full_name', event.target.value); | ||
}} | ||
onCancel={(previousValue: string) => { | ||
formik.setFieldValue('full_name', previousValue); | ||
}} | ||
isReadOnly={formik.isSubmitting} | ||
editModeProps={{ | ||
formRowProps: { | ||
error: formik.errors.full_name, | ||
}, | ||
}} | ||
validate={(value: string) => { | ||
if (value.trim().length === 0) { | ||
return 'Name cannot be empty'; | ||
} | ||
}} | ||
isInvalid={!!formik.errors.full_name} | ||
placeholder="Enter your name" | ||
css={{ width: 350 }} | ||
/> | ||
<EuiSpacer size="m" /> | ||
|
||
<EuiFormRow error={formik.errors.role} isInvalid={!!formik.errors.role}> | ||
<Field | ||
as={EuiComboBox} | ||
name="role" | ||
placeholder="Select your role" | ||
singleSelection={{ asPlainText: true }} | ||
options={roles.map((role) => ({ label: role }))} | ||
selectedOptions={ | ||
formik.values.role ? [{ label: formik.values.role }] : undefined | ||
} | ||
onCreateOption={(value: string) => { | ||
formik.setFieldValue('role', value); | ||
}} | ||
onChange={(selectedOptions: EuiComboBoxOptionOption[]) => { | ||
formik.setFieldValue( | ||
'role', | ||
selectedOptions.length === 0 ? '' : selectedOptions[0].label | ||
); | ||
}} | ||
validate={(value: string) => { | ||
if (value.trim().length === 0) { | ||
return 'Role cannot be empty'; | ||
} | ||
}} | ||
isInvalid={!!formik.errors.role} | ||
isClearable={false} | ||
fullWidth | ||
/> | ||
</EuiFormRow> | ||
</> | ||
} | ||
actions={[ | ||
<EuiButton | ||
type="submit" | ||
disabled={!formik.isValid} | ||
isLoading={formik.isSubmitting} | ||
fill | ||
> | ||
Log in | ||
</EuiButton>, | ||
<EuiButtonEmpty size="xs" href="/"> | ||
More login options | ||
</EuiButtonEmpty>, | ||
]} | ||
/> | ||
</Form> | ||
</EuiPageTemplate.Section> | ||
</EuiPageTemplate> | ||
</FormikProvider> | ||
); | ||
}; | ||
|
||
const sanitizeUsername = (username: string) => | ||
username.replace(/[^a-zA-Z0-9_]/g, '_').toLowerCase(); | ||
const sanitizeEmail = (email: string) => `${sanitizeUsername(email)}@elastic.co`; |
This file contains 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,84 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { PluginInitializer } from '@kbn/core-plugins-browser'; | ||
import { AppNavLinkStatus } from '@kbn/core-application-browser'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { KibanaThemeProvider } from '@kbn/react-kibana-context-theme'; | ||
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public'; | ||
import { I18nProvider } from '@kbn/i18n-react'; | ||
import { MOCK_IDP_LOGIN_PATH } from '@kbn/mock-idp-utils/src/constants'; | ||
import type { CloudStart, CloudSetup } from '@kbn/cloud-plugin/public'; | ||
import { RoleSwitcher } from './role_switcher'; | ||
|
||
export interface PluginSetupDependencies { | ||
cloud?: CloudSetup; | ||
} | ||
|
||
export interface PluginStartDependencies { | ||
cloud?: CloudStart; | ||
} | ||
|
||
export const plugin: PluginInitializer< | ||
void, | ||
void, | ||
PluginSetupDependencies, | ||
PluginStartDependencies | ||
> = () => ({ | ||
setup(coreSetup, plugins) { | ||
// Register Mock IDP login page | ||
coreSetup.http.anonymousPaths.register(MOCK_IDP_LOGIN_PATH); | ||
coreSetup.application.register({ | ||
id: 'mock_idp', | ||
title: 'Mock IDP', | ||
chromeless: true, | ||
appRoute: MOCK_IDP_LOGIN_PATH, | ||
navLinkStatus: AppNavLinkStatus.hidden, | ||
mount: async (params) => { | ||
const [[coreStart], { LoginPage }] = await Promise.all([ | ||
coreSetup.getStartServices(), | ||
import('./login_page'), | ||
]); | ||
|
||
ReactDOM.render( | ||
<KibanaThemeProvider theme={coreStart.theme}> | ||
<KibanaContextProvider services={coreStart}> | ||
<I18nProvider> | ||
<LoginPage projectType={plugins.cloud?.serverless.projectType} /> | ||
</I18nProvider> | ||
</KibanaContextProvider> | ||
</KibanaThemeProvider>, | ||
params.element | ||
); | ||
|
||
return () => ReactDOM.unmountComponentAtNode(params.element); | ||
}, | ||
}); | ||
}, | ||
start(coreStart, plugins) { | ||
// Register role switcher dropdown menu in the top right navigation of the Kibana UI | ||
coreStart.chrome.navControls.registerRight({ | ||
order: 4000 + 1, // Make sure it comes after the user menu | ||
mount: (element: HTMLElement) => { | ||
ReactDOM.render( | ||
<KibanaThemeProvider theme={coreStart.theme}> | ||
<KibanaContextProvider services={coreStart}> | ||
<I18nProvider> | ||
<RoleSwitcher projectType={plugins.cloud?.serverless.projectType} /> | ||
</I18nProvider> | ||
</KibanaContextProvider> | ||
</KibanaThemeProvider>, | ||
element | ||
); | ||
return () => ReactDOM.unmountComponentAtNode(element); | ||
}, | ||
}); | ||
}, | ||
stop() {}, | ||
}); |
Oops, something went wrong.