index.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import {RouteComponentProps} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import {openEmailVerification} from 'sentry/actionCreators/modal';
  5. import {Button} from 'sentry/components/button';
  6. import CircleIndicator from 'sentry/components/circleIndicator';
  7. import EmptyMessage from 'sentry/components/emptyMessage';
  8. import FieldGroup from 'sentry/components/forms/fieldGroup';
  9. import ListLink from 'sentry/components/links/listLink';
  10. import NavTabs from 'sentry/components/navTabs';
  11. import Panel from 'sentry/components/panels/panel';
  12. import PanelBody from 'sentry/components/panels/panelBody';
  13. import PanelHeader from 'sentry/components/panels/panelHeader';
  14. import PanelItem from 'sentry/components/panels/panelItem';
  15. import {Tooltip} from 'sentry/components/tooltip';
  16. import {IconDelete} from 'sentry/icons';
  17. import {t} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import {Authenticator, OrganizationSummary} from 'sentry/types';
  20. import recreateRoute from 'sentry/utils/recreateRoute';
  21. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  22. import RemoveConfirm from 'sentry/views/settings/account/accountSecurity/components/removeConfirm';
  23. import TwoFactorRequired from 'sentry/views/settings/account/accountSecurity/components/twoFactorRequired';
  24. import PasswordForm from 'sentry/views/settings/account/passwordForm';
  25. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  26. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  27. type Props = {
  28. authenticators: Authenticator[] | null;
  29. countEnrolled: number;
  30. deleteDisabled: boolean;
  31. handleRefresh: () => void;
  32. hasVerifiedEmail: boolean;
  33. onDisable: (auth: Authenticator) => void;
  34. orgsRequire2fa: OrganizationSummary[];
  35. } & DeprecatedAsyncView['props'] &
  36. RouteComponentProps<{}, {}>;
  37. /**
  38. * Lists 2fa devices + password change form
  39. */
  40. class AccountSecurity extends DeprecatedAsyncView<Props> {
  41. getTitle() {
  42. return t('Security');
  43. }
  44. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  45. return [];
  46. }
  47. handleSessionClose = async () => {
  48. try {
  49. await this.api.requestPromise('/auth/', {
  50. method: 'DELETE',
  51. data: {all: true},
  52. });
  53. window.location.assign('/auth/login/');
  54. } catch (err) {
  55. addErrorMessage(t('There was a problem closing all sessions'));
  56. throw err;
  57. }
  58. };
  59. formatOrgSlugs = () => {
  60. const {orgsRequire2fa} = this.props;
  61. const slugs = orgsRequire2fa.map(({slug}) => slug);
  62. return [slugs.slice(0, -1).join(', '), slugs.slice(-1)[0]].join(
  63. slugs.length > 1 ? ' and ' : ''
  64. );
  65. };
  66. handleAdd2FAClicked = () => {
  67. const {handleRefresh} = this.props;
  68. openEmailVerification({
  69. onClose: () => {
  70. handleRefresh();
  71. },
  72. actionMessage: 'enrolling a 2FA device',
  73. });
  74. };
  75. renderBody() {
  76. const {authenticators, countEnrolled, deleteDisabled, onDisable, hasVerifiedEmail} =
  77. this.props;
  78. const isEmpty = !authenticators?.length;
  79. return (
  80. <div>
  81. <SettingsPageHeader
  82. title={t('Security')}
  83. tabs={
  84. <NavTabs underlined>
  85. <ListLink to={recreateRoute('', this.props)} index>
  86. {t('Settings')}
  87. </ListLink>
  88. <ListLink to={recreateRoute('session-history/', this.props)}>
  89. {t('Session History')}
  90. </ListLink>
  91. </NavTabs>
  92. }
  93. />
  94. {!isEmpty && countEnrolled === 0 && <TwoFactorRequired />}
  95. <PasswordForm />
  96. <Panel>
  97. <PanelHeader>{t('Sessions')}</PanelHeader>
  98. <PanelBody>
  99. <FieldGroup
  100. alignRight
  101. flexibleControlStateSize
  102. label={t('Sign out of all devices')}
  103. help={t(
  104. 'Signing out of all devices will sign you out of this device as well.'
  105. )}
  106. >
  107. <Button onClick={this.handleSessionClose}>
  108. {t('Sign out of all devices')}
  109. </Button>
  110. </FieldGroup>
  111. </PanelBody>
  112. </Panel>
  113. <Panel>
  114. <PanelHeader>{t('Two-Factor Authentication')}</PanelHeader>
  115. {isEmpty && (
  116. <EmptyMessage>{t('No available authenticators to add')}</EmptyMessage>
  117. )}
  118. <PanelBody>
  119. {!isEmpty &&
  120. authenticators?.map(auth => {
  121. const {
  122. id,
  123. authId,
  124. description,
  125. isBackupInterface,
  126. isEnrolled,
  127. disallowNewEnrollment,
  128. configureButton,
  129. name,
  130. } = auth;
  131. if (disallowNewEnrollment && !isEnrolled) {
  132. return null;
  133. }
  134. return (
  135. <AuthenticatorPanelItem key={id}>
  136. <AuthenticatorHeader>
  137. <AuthenticatorTitle>
  138. <AuthenticatorStatus
  139. role="status"
  140. aria-label={
  141. isEnrolled
  142. ? t('Authentication Method Active')
  143. : t('Authentication Method Inactive')
  144. }
  145. enabled={isEnrolled}
  146. />
  147. <AuthenticatorName>{name}</AuthenticatorName>
  148. </AuthenticatorTitle>
  149. <Actions>
  150. {!isBackupInterface && !isEnrolled && hasVerifiedEmail && (
  151. <Button
  152. to={`/settings/account/security/mfa/${id}/enroll/`}
  153. size="sm"
  154. priority="primary"
  155. >
  156. {t('Add')}
  157. </Button>
  158. )}
  159. {!isBackupInterface && !isEnrolled && !hasVerifiedEmail && (
  160. <Button
  161. onClick={this.handleAdd2FAClicked}
  162. size="sm"
  163. priority="primary"
  164. >
  165. {t('Add')}
  166. </Button>
  167. )}
  168. {isEnrolled && authId && (
  169. <Button
  170. to={`/settings/account/security/mfa/${authId}/`}
  171. size="sm"
  172. >
  173. {configureButton}
  174. </Button>
  175. )}
  176. {!isBackupInterface && isEnrolled && (
  177. <Tooltip
  178. title={t(
  179. `Two-factor authentication is required for organization(s): %s.`,
  180. this.formatOrgSlugs()
  181. )}
  182. disabled={!deleteDisabled}
  183. >
  184. <RemoveConfirm
  185. onConfirm={() => onDisable(auth)}
  186. disabled={deleteDisabled}
  187. >
  188. <Button
  189. size="sm"
  190. aria-label={t('Delete')}
  191. icon={<IconDelete />}
  192. />
  193. </RemoveConfirm>
  194. </Tooltip>
  195. )}
  196. </Actions>
  197. {isBackupInterface && !isEnrolled ? t('requires 2FA') : null}
  198. </AuthenticatorHeader>
  199. <Description>{description}</Description>
  200. </AuthenticatorPanelItem>
  201. );
  202. })}
  203. </PanelBody>
  204. </Panel>
  205. </div>
  206. );
  207. }
  208. }
  209. const AuthenticatorName = styled('span')`
  210. font-size: 1.2em;
  211. `;
  212. const AuthenticatorPanelItem = styled(PanelItem)`
  213. flex-direction: column;
  214. `;
  215. const AuthenticatorHeader = styled('div')`
  216. display: flex;
  217. flex: 1;
  218. align-items: center;
  219. `;
  220. const AuthenticatorTitle = styled('div')`
  221. flex: 1;
  222. `;
  223. const Actions = styled('div')`
  224. display: grid;
  225. grid-auto-flow: column;
  226. gap: ${space(1)};
  227. `;
  228. const AuthenticatorStatus = styled(CircleIndicator)`
  229. margin-right: ${space(1)};
  230. `;
  231. const Description = styled(TextBlock)`
  232. margin-top: ${space(2)};
  233. margin-bottom: 0;
  234. `;
  235. export default AccountSecurity;