GoogleSecuritySettingContents.jsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import React from 'react';
  2. import { pathUtils } from '@growi/core/dist/utils';
  3. import { useTranslation } from 'next-i18next';
  4. import PropTypes from 'prop-types';
  5. import urljoin from 'url-join';
  6. import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer';
  7. import AdminGoogleSecurityContainer from '~/client/services/AdminGoogleSecurityContainer';
  8. import { toastSuccess, toastError } from '~/client/util/toastr';
  9. import { useSiteUrl } from '~/stores-universal/context';
  10. import { withUnstatedContainers } from '../../UnstatedUtils';
  11. class GoogleSecurityManagementContents extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.onClickSubmit = this.onClickSubmit.bind(this);
  15. }
  16. async onClickSubmit() {
  17. const { t, adminGoogleSecurityContainer, adminGeneralSecurityContainer } = this.props;
  18. try {
  19. await adminGoogleSecurityContainer.updateGoogleSetting();
  20. await adminGeneralSecurityContainer.retrieveSetupStratedies();
  21. toastSuccess(t('security_settings.OAuth.Google.updated_google'));
  22. }
  23. catch (err) {
  24. toastError(err);
  25. }
  26. }
  27. render() {
  28. const {
  29. t, adminGeneralSecurityContainer, adminGoogleSecurityContainer, siteUrl,
  30. } = this.props;
  31. const { isGoogleEnabled } = adminGeneralSecurityContainer.state;
  32. const googleCallbackUrl = urljoin(pathUtils.removeTrailingSlash(siteUrl), '/passport/google/callback');
  33. return (
  34. <React.Fragment>
  35. <h2 className="alert-anchor border-bottom">
  36. {t('security_settings.OAuth.Google.name')}
  37. </h2>
  38. {adminGoogleSecurityContainer.state.retrieveError != null && (
  39. <div className="alert alert-danger">
  40. <p>{t('Error occurred')} : {adminGoogleSecurityContainer.state.retrieveError}</p>
  41. </div>
  42. )}
  43. <div className="row my-4">
  44. <div className="col-6 offset-3">
  45. <div className="form-check form-switch form-check-success">
  46. <input
  47. id="isGoogleEnabled"
  48. className="form-check-input"
  49. type="checkbox"
  50. checked={adminGeneralSecurityContainer.state.isGoogleEnabled || false}
  51. onChange={() => { adminGeneralSecurityContainer.switchIsGoogleOAuthEnabled() }}
  52. />
  53. <label className="form-label form-check-label" htmlFor="isGoogleEnabled">
  54. {t('security_settings.OAuth.Google.enable_google')}
  55. </label>
  56. </div>
  57. {(!adminGeneralSecurityContainer.state.setupStrategies.includes('google') && isGoogleEnabled)
  58. && <div className="badge text-bg-warning">{t('security_settings.setup_is_not_yet_complete')}</div>}
  59. </div>
  60. </div>
  61. <div className="row mb-5">
  62. <label className="form-label col-12 col-md-3 text-start text-md-end py-2">{t('security_settings.callback_URL')}</label>
  63. <div className="col-12 col-md-6">
  64. <input
  65. className="form-control"
  66. type="text"
  67. value={googleCallbackUrl}
  68. readOnly
  69. />
  70. <p className="form-text text-muted small">{t('security_settings.desc_of_callback_URL', { AuthName: 'OAuth' })}</p>
  71. {(siteUrl == null || siteUrl === '') && (
  72. <div className="alert alert-danger">
  73. <span className="material-symbols-outlined">error</span>
  74. <span
  75. // eslint-disable-next-line max-len
  76. dangerouslySetInnerHTML={{ __html: t('alert.siteUrl_is_not_set', { link: `<a href="/admin/app">${t('headers.app_settings', { ns: 'commons' })}<span class="material-symbols-outlined">login</span></a>`, ns: 'commons' }) }}
  77. />
  78. </div>
  79. )}
  80. </div>
  81. </div>
  82. {isGoogleEnabled && (
  83. <React.Fragment>
  84. <h3 className="border-bottom mb-4">{t('security_settings.configuration')}</h3>
  85. <div className="row mb-4">
  86. <label htmlFor="googleClientId" className="col-3 text-end py-2 form-label">{t('security_settings.clientID')}</label>
  87. <div className="col-6">
  88. <input
  89. className="form-control"
  90. type="text"
  91. name="googleClientId"
  92. defaultValue={adminGoogleSecurityContainer.state.googleClientId || ''}
  93. onChange={e => adminGoogleSecurityContainer.changeGoogleClientId(e.target.value)}
  94. />
  95. <p className="form-text text-muted">
  96. <small dangerouslySetInnerHTML={{ __html: t('security_settings.Use env var if empty', { env: 'OAUTH_GOOGLE_CLIENT_ID' }) }} />
  97. </p>
  98. </div>
  99. </div>
  100. <div className="row mb-4">
  101. <label htmlFor="googleClientSecret" className="col-3 text-end py-2 form-label">{t('security_settings.client_secret')}</label>
  102. <div className="col-6">
  103. <input
  104. className="form-control"
  105. type="password"
  106. name="googleClientSecret"
  107. defaultValue={adminGoogleSecurityContainer.state.googleClientSecret || ''}
  108. onChange={e => adminGoogleSecurityContainer.changeGoogleClientSecret(e.target.value)}
  109. />
  110. <p className="form-text text-muted">
  111. <small dangerouslySetInnerHTML={{ __html: t('security_settings.Use env var if empty', { env: 'OAUTH_GOOGLE_CLIENT_SECRET' }) }} />
  112. </p>
  113. </div>
  114. </div>
  115. <div className="row mb-3">
  116. <div className="offset-3 col-6">
  117. <div className="form-check form-check-success">
  118. <input
  119. id="bindByUserNameGoogle"
  120. className="form-check-input"
  121. type="checkbox"
  122. checked={adminGoogleSecurityContainer.state.isSameEmailTreatedAsIdenticalUser || false}
  123. onChange={() => { adminGoogleSecurityContainer.switchIsSameEmailTreatedAsIdenticalUser() }}
  124. />
  125. <label
  126. className="form-check-label"
  127. htmlFor="bindByUserNameGoogle"
  128. dangerouslySetInnerHTML={{ __html: t('security_settings.Treat email matching as identical') }}
  129. />
  130. </div>
  131. <p className="form-text text-muted">
  132. <small dangerouslySetInnerHTML={{ __html: t('security_settings.Treat email matching as identical_warn') }} />
  133. </p>
  134. </div>
  135. </div>
  136. <div className="row mb-4">
  137. <div className="offset-3 col-5">
  138. <button
  139. type="button"
  140. className="btn btn-primary"
  141. disabled={adminGoogleSecurityContainer.state.retrieveError != null}
  142. onClick={this.onClickSubmit}
  143. >
  144. {t('Update')}
  145. </button>
  146. </div>
  147. </div>
  148. </React.Fragment>
  149. )}
  150. <hr />
  151. <div style={{ minHeight: '300px' }}>
  152. <h4>
  153. <span className="material-symbols-outlined" aria-hidden="true">help</span>
  154. <a href="#collapseHelpForGoogleOauth" data-bs-toggle="collapse"> {t('security_settings.OAuth.how_to.google')}</a>
  155. </h4>
  156. <div className="card custom-card bg-body-tertiary">
  157. <ol id="collapseHelpForGoogleOauth" className="collapse mb-0">
  158. {/* eslint-disable-next-line max-len */}
  159. <li dangerouslySetInnerHTML={{ __html: t('security_settings.OAuth.Google.register_1', { link: '<a href="https://console.cloud.google.com/apis/credentials" target=_blank>Google Cloud Platform API Manager</a>' }) }} />
  160. <li dangerouslySetInnerHTML={{ __html: t('security_settings.OAuth.Google.register_2') }} />
  161. <li dangerouslySetInnerHTML={{ __html: t('security_settings.OAuth.Google.register_3') }} />
  162. <li dangerouslySetInnerHTML={{ __html: t('security_settings.OAuth.Google.register_4', { url: googleCallbackUrl }) }} />
  163. <li dangerouslySetInnerHTML={{ __html: t('security_settings.OAuth.Google.register_5') }} />
  164. </ol>
  165. </div>
  166. </div>
  167. </React.Fragment>
  168. );
  169. }
  170. }
  171. const GoogleSecurityManagementContentsFc = (props) => {
  172. const { t } = useTranslation('admin');
  173. const { data: siteUrl } = useSiteUrl();
  174. return <GoogleSecurityManagementContents t={t} siteUrl={siteUrl} {...props} />;
  175. };
  176. GoogleSecurityManagementContents.propTypes = {
  177. t: PropTypes.func.isRequired, // i18next
  178. adminGeneralSecurityContainer: PropTypes.instanceOf(AdminGeneralSecurityContainer).isRequired,
  179. adminGoogleSecurityContainer: PropTypes.instanceOf(AdminGoogleSecurityContainer).isRequired,
  180. siteUrl: PropTypes.string,
  181. };
  182. const GoogleSecurityManagementContentsWrapper = withUnstatedContainers(GoogleSecurityManagementContentsFc, [
  183. AdminGeneralSecurityContainer,
  184. AdminGoogleSecurityContainer,
  185. ]);
  186. export default GoogleSecurityManagementContentsWrapper;