GoogleSecuritySettingContents.tsx 8.7 KB

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