GoogleSecuritySettingContents.tsx 8.6 KB

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