123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import {RouteComponentProps} from 'react-router';
- import styled from '@emotion/styled';
- import {addErrorMessage} from 'sentry/actionCreators/indicator';
- import {openEmailVerification} from 'sentry/actionCreators/modal';
- import {Button} from 'sentry/components/button';
- import CircleIndicator from 'sentry/components/circleIndicator';
- import EmptyMessage from 'sentry/components/emptyMessage';
- import FieldGroup from 'sentry/components/forms/fieldGroup';
- import ListLink from 'sentry/components/links/listLink';
- import NavTabs from 'sentry/components/navTabs';
- import Panel from 'sentry/components/panels/panel';
- import PanelBody from 'sentry/components/panels/panelBody';
- import PanelHeader from 'sentry/components/panels/panelHeader';
- import PanelItem from 'sentry/components/panels/panelItem';
- import {Tooltip} from 'sentry/components/tooltip';
- import {IconDelete} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {Authenticator, OrganizationSummary} from 'sentry/types';
- import oxfordizeArray from 'sentry/utils/oxfordizeArray';
- import recreateRoute from 'sentry/utils/recreateRoute';
- import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
- import RemoveConfirm from 'sentry/views/settings/account/accountSecurity/components/removeConfirm';
- import TwoFactorRequired from 'sentry/views/settings/account/accountSecurity/components/twoFactorRequired';
- import PasswordForm from 'sentry/views/settings/account/passwordForm';
- import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
- import TextBlock from 'sentry/views/settings/components/text/textBlock';
- type Props = {
- authenticators: Authenticator[] | null;
- countEnrolled: number;
- deleteDisabled: boolean;
- handleRefresh: () => void;
- hasVerifiedEmail: boolean;
- onDisable: (auth: Authenticator) => void;
- orgsRequire2fa: OrganizationSummary[];
- } & DeprecatedAsyncView['props'] &
- RouteComponentProps<{}, {}>;
- /**
- * Lists 2fa devices + password change form
- */
- class AccountSecurity extends DeprecatedAsyncView<Props> {
- getTitle() {
- return t('Security');
- }
- getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
- return [];
- }
- handleSessionClose = async () => {
- try {
- await this.api.requestPromise('/auth/', {
- method: 'DELETE',
- data: {all: true},
- });
- window.location.assign('/auth/login/');
- } catch (err) {
- addErrorMessage(t('There was a problem closing all sessions'));
- throw err;
- }
- };
- formatOrgSlugs = () => {
- const {orgsRequire2fa} = this.props;
- const slugs = orgsRequire2fa.map(({slug}) => slug);
- return oxfordizeArray(slugs);
- };
- handleAdd2FAClicked = () => {
- const {handleRefresh} = this.props;
- openEmailVerification({
- onClose: () => {
- handleRefresh();
- },
- actionMessage: 'enrolling a 2FA device',
- });
- };
- renderBody() {
- const {authenticators, countEnrolled, deleteDisabled, onDisable, hasVerifiedEmail} =
- this.props;
- const isEmpty = !authenticators?.length;
- return (
- <div>
- <SettingsPageHeader
- title={t('Security')}
- tabs={
- <NavTabs underlined>
- <ListLink to={recreateRoute('', this.props)} index>
- {t('Settings')}
- </ListLink>
- <ListLink to={recreateRoute('session-history/', this.props)}>
- {t('Session History')}
- </ListLink>
- </NavTabs>
- }
- />
- {!isEmpty && countEnrolled === 0 && <TwoFactorRequired />}
- <PasswordForm />
- <Panel>
- <PanelHeader>{t('Sessions')}</PanelHeader>
- <PanelBody>
- <FieldGroup
- alignRight
- flexibleControlStateSize
- label={t('Sign out of all devices')}
- help={t(
- 'Signing out of all devices will sign you out of this device as well.'
- )}
- >
- <Button onClick={this.handleSessionClose}>
- {t('Sign out of all devices')}
- </Button>
- </FieldGroup>
- </PanelBody>
- </Panel>
- <Panel>
- <PanelHeader>{t('Two-Factor Authentication')}</PanelHeader>
- {isEmpty && (
- <EmptyMessage>{t('No available authenticators to add')}</EmptyMessage>
- )}
- <PanelBody>
- {!isEmpty &&
- authenticators?.map(auth => {
- const {
- id,
- authId,
- description,
- isBackupInterface,
- isEnrolled,
- disallowNewEnrollment,
- configureButton,
- name,
- } = auth;
- if (disallowNewEnrollment && !isEnrolled) {
- return null;
- }
- return (
- <AuthenticatorPanelItem key={id}>
- <AuthenticatorHeader>
- <AuthenticatorTitle>
- <AuthenticatorStatus
- role="status"
- aria-label={
- isEnrolled
- ? t('Authentication Method Active')
- : t('Authentication Method Inactive')
- }
- enabled={isEnrolled}
- />
- <AuthenticatorName>{name}</AuthenticatorName>
- </AuthenticatorTitle>
- <Actions>
- {!isBackupInterface && !isEnrolled && hasVerifiedEmail && (
- <Button
- to={`/settings/account/security/mfa/${id}/enroll/`}
- size="sm"
- priority="primary"
- >
- {t('Add')}
- </Button>
- )}
- {!isBackupInterface && !isEnrolled && !hasVerifiedEmail && (
- <Button
- onClick={this.handleAdd2FAClicked}
- size="sm"
- priority="primary"
- >
- {t('Add')}
- </Button>
- )}
- {isEnrolled && authId && (
- <Button
- to={`/settings/account/security/mfa/${authId}/`}
- size="sm"
- >
- {configureButton}
- </Button>
- )}
- {!isBackupInterface && isEnrolled && (
- <Tooltip
- title={t(
- `Two-factor authentication is required for organization(s): %s.`,
- this.formatOrgSlugs()
- )}
- disabled={!deleteDisabled}
- >
- <RemoveConfirm
- onConfirm={() => onDisable(auth)}
- disabled={deleteDisabled}
- >
- <Button
- size="sm"
- aria-label={t('Delete')}
- icon={<IconDelete />}
- />
- </RemoveConfirm>
- </Tooltip>
- )}
- </Actions>
- {isBackupInterface && !isEnrolled ? t('requires 2FA') : null}
- </AuthenticatorHeader>
- <Description>{description}</Description>
- </AuthenticatorPanelItem>
- );
- })}
- </PanelBody>
- </Panel>
- </div>
- );
- }
- }
- const AuthenticatorName = styled('span')`
- font-size: 1.2em;
- `;
- const AuthenticatorPanelItem = styled(PanelItem)`
- flex-direction: column;
- `;
- const AuthenticatorHeader = styled('div')`
- display: flex;
- flex: 1;
- align-items: center;
- `;
- const AuthenticatorTitle = styled('div')`
- flex: 1;
- `;
- const Actions = styled('div')`
- display: grid;
- grid-auto-flow: column;
- gap: ${space(1)};
- `;
- const AuthenticatorStatus = styled(CircleIndicator)`
- margin-right: ${space(1)};
- `;
- const Description = styled(TextBlock)`
- margin-top: ${space(2)};
- margin-bottom: 0;
- `;
- export default AccountSecurity;
|