index.tsx 8.5 KB

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