sudoModal.tsx 4.4 KB

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