index.tsx 8.1 KB

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