index.tsx 8.1 KB

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