accountSecurityDetails.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * AccountSecurityDetails is only displayed when user is enrolled in the 2fa method.
  3. * It displays created + last used time of the 2fa method.
  4. *
  5. * Also displays 2fa method specific details.
  6. */
  7. import {Fragment} from 'react';
  8. import type {RouteComponentProps} from 'react-router';
  9. import styled from '@emotion/styled';
  10. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  11. import {Button} from 'sentry/components/button';
  12. import CircleIndicator from 'sentry/components/circleIndicator';
  13. import {DateTime} from 'sentry/components/dateTime';
  14. import {Tooltip} from 'sentry/components/tooltip';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {Authenticator, AuthenticatorDevice} from 'sentry/types';
  18. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  19. import RecoveryCodes from 'sentry/views/settings/account/accountSecurity/components/recoveryCodes';
  20. import RemoveConfirm from 'sentry/views/settings/account/accountSecurity/components/removeConfirm';
  21. import U2fEnrolledDetails from 'sentry/views/settings/account/accountSecurity/components/u2fEnrolledDetails';
  22. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  23. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  24. const ENDPOINT = '/users/me/authenticators/';
  25. type AuthenticatorDateProps = {
  26. /**
  27. * Can be null or a Date object.
  28. * Component will have value "never" if it is null
  29. */
  30. date: string | null;
  31. label: string;
  32. };
  33. function AuthenticatorDate({label, date}: AuthenticatorDateProps) {
  34. return (
  35. <Fragment>
  36. <DateLabel>{label}</DateLabel>
  37. <div>{date ? <DateTime date={date} /> : t('never')}</div>
  38. </Fragment>
  39. );
  40. }
  41. type Props = {
  42. deleteDisabled: boolean;
  43. onRegenerateBackupCodes: () => void;
  44. } & RouteComponentProps<{authId: string}, {}>;
  45. type State = {
  46. authenticator: Authenticator | null;
  47. } & DeprecatedAsyncView['state'];
  48. class AccountSecurityDetails extends DeprecatedAsyncView<Props, State> {
  49. getTitle() {
  50. return t('Security');
  51. }
  52. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  53. const {params} = this.props;
  54. const {authId} = params;
  55. return [['authenticator', `${ENDPOINT}${authId}/`]];
  56. }
  57. handleRemove = async (device?: AuthenticatorDevice) => {
  58. const {authenticator} = this.state;
  59. if (!authenticator || !authenticator.authId) {
  60. return;
  61. }
  62. // if the device is defined, it means that U2f is being removed
  63. // reason for adding a trailing slash is a result of the endpoint on line 109 needing it but it can't be set there as if deviceId is None, the route will end with '//'
  64. const deviceId = device ? `${device.key_handle}/` : '';
  65. const deviceName = device ? device.name : t('Authenticator');
  66. this.setState({loading: true});
  67. try {
  68. await this.api.requestPromise(`${ENDPOINT}${authenticator.authId}/${deviceId}`, {
  69. method: 'DELETE',
  70. });
  71. this.props.router.push('/settings/account/security');
  72. addSuccessMessage(t('%s has been removed', deviceName));
  73. } catch {
  74. // Error deleting authenticator
  75. this.setState({loading: false});
  76. addErrorMessage(t('Error removing %s', deviceName));
  77. }
  78. };
  79. handleRename = async (device: AuthenticatorDevice, deviceName: string) => {
  80. const {authenticator} = this.state;
  81. if (!authenticator?.authId) {
  82. return;
  83. }
  84. // if the device is defined, it means that U2f is being renamed
  85. // reason for adding a trailing slash is a result of the endpoint on line 109 needing it but it can't be set there as if deviceId is None, the route will end with '//'
  86. const deviceId = device ? `${device.key_handle}/` : '';
  87. this.setState({loading: true});
  88. const data = {
  89. name: deviceName,
  90. };
  91. try {
  92. await this.api.requestPromise(`${ENDPOINT}${authenticator.authId}/${deviceId}`, {
  93. method: 'PUT',
  94. data,
  95. });
  96. this.props.router.push(`/settings/account/security/mfa/${authenticator.authId}`);
  97. addSuccessMessage(t('Device was renamed'));
  98. } catch {
  99. this.setState({loading: false});
  100. addErrorMessage(t('Error renaming the device'));
  101. }
  102. };
  103. renderBody() {
  104. const {authenticator} = this.state;
  105. if (!authenticator) {
  106. return null;
  107. }
  108. const {deleteDisabled, onRegenerateBackupCodes} = this.props;
  109. return (
  110. <Fragment>
  111. <SettingsPageHeader
  112. title={
  113. <Fragment>
  114. <span>{authenticator.name}</span>
  115. <AuthenticatorStatus
  116. data-test-id={`auth-status-${
  117. authenticator.isEnrolled ? 'enabled' : 'disabled'
  118. }`}
  119. enabled={authenticator.isEnrolled}
  120. />
  121. </Fragment>
  122. }
  123. action={
  124. <AuthenticatorActions>
  125. {authenticator.isEnrolled && authenticator.allowRotationInPlace && (
  126. <Button to={`/settings/account/security/mfa/${authenticator.id}/enroll/`}>
  127. {t('Rotate Secret Key')}
  128. </Button>
  129. )}
  130. {authenticator.isEnrolled && authenticator.removeButton && (
  131. <Tooltip
  132. title={t(
  133. "Two-factor authentication is required for at least one organization you're a member of."
  134. )}
  135. disabled={!deleteDisabled}
  136. >
  137. <RemoveConfirm onConfirm={this.handleRemove} disabled={deleteDisabled}>
  138. <Button priority="danger">{authenticator.removeButton}</Button>
  139. </RemoveConfirm>
  140. </Tooltip>
  141. )}
  142. </AuthenticatorActions>
  143. }
  144. />
  145. <TextBlock>{authenticator.description}</TextBlock>
  146. <AuthenticatorDates>
  147. <AuthenticatorDate label={t('Created at')} date={authenticator.createdAt} />
  148. <AuthenticatorDate label={t('Last used')} date={authenticator.lastUsedAt} />
  149. </AuthenticatorDates>
  150. <U2fEnrolledDetails
  151. isEnrolled={authenticator.isEnrolled}
  152. id={authenticator.id}
  153. devices={authenticator.devices}
  154. onRemoveU2fDevice={this.handleRemove}
  155. onRenameU2fDevice={this.handleRename}
  156. />
  157. {authenticator.isEnrolled && authenticator.phone && (
  158. <PhoneWrapper>
  159. {t('Confirmation codes are sent to the following phone number')}:
  160. <Phone>{authenticator.phone}</Phone>
  161. </PhoneWrapper>
  162. )}
  163. <RecoveryCodes
  164. onRegenerateBackupCodes={onRegenerateBackupCodes}
  165. isEnrolled={authenticator.isEnrolled}
  166. codes={authenticator.codes}
  167. />
  168. </Fragment>
  169. );
  170. }
  171. }
  172. export default AccountSecurityDetails;
  173. const AuthenticatorStatus = styled(CircleIndicator)`
  174. margin-left: ${space(1)};
  175. `;
  176. const AuthenticatorActions = styled('div')`
  177. display: flex;
  178. justify-content: center;
  179. align-items: center;
  180. > * {
  181. margin-left: ${space(1)};
  182. }
  183. `;
  184. const AuthenticatorDates = styled('div')`
  185. display: grid;
  186. gap: ${space(2)};
  187. grid-template-columns: max-content auto;
  188. `;
  189. const DateLabel = styled('span')`
  190. font-weight: bold;
  191. `;
  192. const PhoneWrapper = styled('div')`
  193. margin-top: ${space(4)};
  194. `;
  195. const Phone = styled('span')`
  196. font-weight: bold;
  197. margin-left: ${space(1)};
  198. `;