sudoModal.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import * as React from 'react';
  2. import {withRouter, WithRouterProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {ModalRenderProps} from 'sentry/actionCreators/modal';
  5. import {Client} from 'sentry/api';
  6. import Alert from 'sentry/components/alert';
  7. import Button from 'sentry/components/button';
  8. import Form from 'sentry/components/forms/form';
  9. import InputField from 'sentry/components/forms/inputField';
  10. import U2fContainer from 'sentry/components/u2f/u2fContainer';
  11. import {IconFlag} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import ConfigStore from 'sentry/stores/configStore';
  14. import space from 'sentry/styles/space';
  15. import withApi from 'sentry/utils/withApi';
  16. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  17. type OnTapProps = NonNullable<React.ComponentProps<typeof U2fContainer>['onTap']>;
  18. type Props = WithRouterProps &
  19. Pick<ModalRenderProps, 'Body' | 'Header'> & {
  20. api: Client;
  21. closeModal: () => void;
  22. /**
  23. * expects a function that returns a Promise
  24. */
  25. retryRequest?: () => Promise<any>;
  26. /**
  27. * User is a superuser without an active su session
  28. */
  29. superuser?: boolean;
  30. };
  31. type State = {
  32. busy: boolean;
  33. error: boolean;
  34. };
  35. class SudoModal extends React.Component<Props, State> {
  36. state: State = {
  37. error: false,
  38. busy: false,
  39. };
  40. handleSuccess = () => {
  41. const {closeModal, superuser, location, router, retryRequest} = this.props;
  42. if (!retryRequest) {
  43. closeModal();
  44. return;
  45. }
  46. if (superuser) {
  47. router.replace({pathname: location.pathname, state: {forceUpdate: new Date()}});
  48. return;
  49. }
  50. this.setState({busy: true}, () => {
  51. retryRequest().then(() => {
  52. this.setState({busy: false}, closeModal);
  53. });
  54. });
  55. };
  56. handleError = () => {
  57. this.setState({busy: false, error: true});
  58. };
  59. handleU2fTap = async (data: Parameters<OnTapProps>[0]) => {
  60. this.setState({busy: true});
  61. const {api} = this.props;
  62. try {
  63. await api.requestPromise('/auth/', {method: 'PUT', data});
  64. this.handleSuccess();
  65. } catch (err) {
  66. this.setState({busy: false});
  67. // u2fInterface relies on this
  68. throw err;
  69. }
  70. };
  71. renderBodyContent() {
  72. const {superuser} = this.props;
  73. const {error} = this.state;
  74. const user = ConfigStore.get('user');
  75. if (!user.hasPasswordAuth) {
  76. return (
  77. <React.Fragment>
  78. <TextBlock>{t('You will need to reauthenticate to continue.')}</TextBlock>
  79. <Button
  80. priority="primary"
  81. href={`/auth/login/?next=${encodeURIComponent(location.pathname)}`}
  82. >
  83. {t('Continue')}
  84. </Button>
  85. </React.Fragment>
  86. );
  87. }
  88. return (
  89. <React.Fragment>
  90. <StyledTextBlock>
  91. {superuser
  92. ? t(
  93. 'You are attempting to access a resource that requires superuser access, please re-authenticate as a superuser.'
  94. )
  95. : t('Help us keep your account safe by confirming your identity.')}
  96. </StyledTextBlock>
  97. {error && (
  98. <StyledAlert type="error" icon={<IconFlag size="md" />}>
  99. {t('Incorrect password')}
  100. </StyledAlert>
  101. )}
  102. <Form
  103. apiMethod="PUT"
  104. apiEndpoint="/auth/"
  105. submitLabel={t('Confirm Password')}
  106. onSubmitSuccess={this.handleSuccess}
  107. onSubmitError={this.handleError}
  108. hideFooter={!user.hasPasswordAuth}
  109. resetOnError
  110. >
  111. <StyledInputField
  112. type="password"
  113. inline={false}
  114. label={t('Password')}
  115. name="password"
  116. autoFocus
  117. flexibleControlStateSize
  118. />
  119. <U2fContainer displayMode="sudo" onTap={this.handleU2fTap} />
  120. </Form>
  121. </React.Fragment>
  122. );
  123. }
  124. render() {
  125. const {Header, Body} = this.props;
  126. return (
  127. <React.Fragment>
  128. <Header closeButton>{t('Confirm Password to Continue')}</Header>
  129. <Body>{this.renderBodyContent()}</Body>
  130. </React.Fragment>
  131. );
  132. }
  133. }
  134. export default withRouter(withApi(SudoModal));
  135. export {SudoModal};
  136. const StyledTextBlock = styled(TextBlock)`
  137. margin-bottom: ${space(1)};
  138. `;
  139. const StyledInputField = styled(InputField)`
  140. padding-left: 0;
  141. `;
  142. const StyledAlert = styled(Alert)`
  143. margin-bottom: 0;
  144. `;