GoogleSecuritySettingContents.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import React, { useCallback, useEffect } from 'react';
  2. import { pathUtils } from '@growi/core/dist/utils';
  3. import { useTranslation } from 'next-i18next';
  4. import { useForm } from 'react-hook-form';
  5. import urljoin from 'url-join';
  6. import AdminGeneralSecurityContainer from '~/client/services/AdminGeneralSecurityContainer';
  7. import AdminGoogleSecurityContainer from '~/client/services/AdminGoogleSecurityContainer';
  8. import { toastError, toastSuccess } from '~/client/util/toastr';
  9. import { useSiteUrlWithEmptyValueWarn } from '~/states/global';
  10. import { withUnstatedContainers } from '../../UnstatedUtils';
  11. type Props = {
  12. adminGeneralSecurityContainer: AdminGeneralSecurityContainer;
  13. adminGoogleSecurityContainer: AdminGoogleSecurityContainer;
  14. };
  15. const GoogleSecurityManagementContents = (props: Props) => {
  16. const { adminGeneralSecurityContainer, adminGoogleSecurityContainer } = props;
  17. const { t } = useTranslation('admin');
  18. const siteUrl = useSiteUrlWithEmptyValueWarn();
  19. const { isGoogleEnabled } = adminGeneralSecurityContainer.state;
  20. const { googleClientId, googleClientSecret, retrieveError } =
  21. adminGoogleSecurityContainer.state;
  22. const googleCallbackUrl = urljoin(
  23. pathUtils.removeTrailingSlash(siteUrl),
  24. '/passport/google/callback',
  25. );
  26. const { register, handleSubmit, reset } = useForm();
  27. // Sync form with container state
  28. useEffect(() => {
  29. reset({
  30. googleClientId,
  31. googleClientSecret,
  32. });
  33. }, [reset, googleClientId, googleClientSecret]);
  34. const onClickSubmit = useCallback(
  35. async (data) => {
  36. try {
  37. await adminGoogleSecurityContainer.updateGoogleSetting({
  38. googleClientId: data.googleClientId ?? '',
  39. googleClientSecret: data.googleClientSecret ?? '',
  40. isSameEmailTreatedAsIdenticalUser:
  41. adminGoogleSecurityContainer.state
  42. .isSameEmailTreatedAsIdenticalUser,
  43. });
  44. await adminGeneralSecurityContainer.retrieveSetupStratedies();
  45. toastSuccess(t('security_settings.OAuth.Google.updated_google'));
  46. } catch (err) {
  47. toastError(err);
  48. }
  49. },
  50. [adminGoogleSecurityContainer, adminGeneralSecurityContainer, t],
  51. );
  52. return (
  53. <form onSubmit={handleSubmit(onClickSubmit)}>
  54. <h2 className="alert-anchor border-bottom">
  55. {t('security_settings.OAuth.Google.name')}
  56. </h2>
  57. {retrieveError != null && (
  58. <div className="alert alert-danger">
  59. <p>
  60. {t('Error occurred')} : {retrieveError}
  61. </p>
  62. </div>
  63. )}
  64. <div className="row my-4">
  65. <div className="col-6 offset-3">
  66. <div className="form-check form-switch form-check-success">
  67. <input
  68. id="isGoogleEnabled"
  69. className="form-check-input"
  70. type="checkbox"
  71. checked={
  72. adminGeneralSecurityContainer.state.isGoogleEnabled || false
  73. }
  74. onChange={() => {
  75. adminGeneralSecurityContainer.switchIsGoogleOAuthEnabled();
  76. }}
  77. />
  78. <label
  79. className="form-label form-check-label"
  80. htmlFor="isGoogleEnabled"
  81. >
  82. {t('security_settings.OAuth.Google.enable_google')}
  83. </label>
  84. </div>
  85. {!adminGeneralSecurityContainer.state.setupStrategies.includes(
  86. 'google',
  87. ) &&
  88. isGoogleEnabled && (
  89. <div className="badge text-bg-warning">
  90. {t('security_settings.setup_is_not_yet_complete')}
  91. </div>
  92. )}
  93. </div>
  94. </div>
  95. <div className="row mb-5">
  96. <label
  97. className="form-label col-12 col-md-3 text-start text-md-end py-2"
  98. htmlFor="googleCallbackUrl"
  99. >
  100. {t('security_settings.callback_URL')}
  101. </label>
  102. <div className="col-12 col-md-6">
  103. <input
  104. id="googleCallbackUrl"
  105. className="form-control"
  106. type="text"
  107. value={googleCallbackUrl}
  108. readOnly
  109. />
  110. <p className="form-text text-muted small">
  111. {t('security_settings.desc_of_callback_URL', {
  112. AuthName: 'OAuth',
  113. })}
  114. </p>
  115. {(siteUrl == null || siteUrl === '') && (
  116. <div className="alert alert-danger">
  117. <span className="material-symbols-outlined">error</span>
  118. <span
  119. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  120. dangerouslySetInnerHTML={{
  121. __html: t('alert.siteUrl_is_not_set', {
  122. link: `<a href="/admin/app">${t('headers.app_settings', { ns: 'commons' })}<span class="material-symbols-outlined">login</span></a>`,
  123. ns: 'commons',
  124. }),
  125. }}
  126. />
  127. </div>
  128. )}
  129. </div>
  130. </div>
  131. {isGoogleEnabled && (
  132. <React.Fragment>
  133. <h3 className="border-bottom mb-4">
  134. {t('security_settings.configuration')}
  135. </h3>
  136. <div className="row mb-4">
  137. <label
  138. htmlFor="googleClientId"
  139. className="col-3 text-end py-2 form-label"
  140. >
  141. {t('security_settings.clientID')}
  142. </label>
  143. <div className="col-6">
  144. <input
  145. className="form-control"
  146. type="text"
  147. {...register('googleClientId')}
  148. />
  149. <p className="form-text text-muted">
  150. <small
  151. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  152. dangerouslySetInnerHTML={{
  153. __html: t('security_settings.Use env var if empty', {
  154. env: 'OAUTH_GOOGLE_CLIENT_ID',
  155. }),
  156. }}
  157. />
  158. </p>
  159. </div>
  160. </div>
  161. <div className="row mb-4">
  162. <label
  163. htmlFor="googleClientSecret"
  164. className="col-3 text-end py-2 form-label"
  165. >
  166. {t('security_settings.client_secret')}
  167. </label>
  168. <div className="col-6">
  169. <input
  170. className="form-control"
  171. type="password"
  172. {...register('googleClientSecret')}
  173. />
  174. <p className="form-text text-muted">
  175. <small
  176. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  177. dangerouslySetInnerHTML={{
  178. __html: t('security_settings.Use env var if empty', {
  179. env: 'OAUTH_GOOGLE_CLIENT_SECRET',
  180. }),
  181. }}
  182. />
  183. </p>
  184. </div>
  185. </div>
  186. <div className="row mb-3">
  187. <div className="offset-3 col-6">
  188. <div className="form-check form-check-success">
  189. <input
  190. id="bindByUserNameGoogle"
  191. className="form-check-input"
  192. type="checkbox"
  193. checked={
  194. adminGoogleSecurityContainer.state
  195. .isSameEmailTreatedAsIdenticalUser || false
  196. }
  197. onChange={() => {
  198. adminGoogleSecurityContainer.switchIsSameEmailTreatedAsIdenticalUser();
  199. }}
  200. />
  201. <label
  202. className="form-check-label"
  203. htmlFor="bindByUserNameGoogle"
  204. >
  205. <span
  206. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  207. dangerouslySetInnerHTML={{
  208. __html: t(
  209. 'security_settings.Treat email matching as identical',
  210. ),
  211. }}
  212. />
  213. </label>
  214. </div>
  215. <p className="form-text text-muted">
  216. <small
  217. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  218. dangerouslySetInnerHTML={{
  219. __html: t(
  220. 'security_settings.Treat email matching as identical_warn',
  221. ),
  222. }}
  223. />
  224. </p>
  225. </div>
  226. </div>
  227. <div className="row mb-4">
  228. <div className="offset-3 col-5">
  229. <button
  230. type="submit"
  231. className="btn btn-primary"
  232. disabled={retrieveError != null}
  233. >
  234. {t('Update')}
  235. </button>
  236. </div>
  237. </div>
  238. </React.Fragment>
  239. )}
  240. <hr />
  241. <div style={{ minHeight: '300px' }}>
  242. <h4>
  243. <span className="material-symbols-outlined" aria-hidden="true">
  244. help
  245. </span>
  246. <a href="#collapseHelpForGoogleOauth" data-bs-toggle="collapse">
  247. {' '}
  248. {t('security_settings.OAuth.how_to.google')}
  249. </a>
  250. </h4>
  251. <div className="card custom-card bg-body-tertiary">
  252. <ol id="collapseHelpForGoogleOauth" className="collapse mb-0">
  253. <li
  254. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  255. dangerouslySetInnerHTML={{
  256. __html: t('security_settings.OAuth.Google.register_1', {
  257. link: '<a href="https://console.cloud.google.com/apis/credentials" target=_blank>Google Cloud Platform API Manager</a>',
  258. }),
  259. }}
  260. />
  261. <li
  262. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  263. dangerouslySetInnerHTML={{
  264. __html: t('security_settings.OAuth.Google.register_2'),
  265. }}
  266. />
  267. <li
  268. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  269. dangerouslySetInnerHTML={{
  270. __html: t('security_settings.OAuth.Google.register_3'),
  271. }}
  272. />
  273. <li
  274. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  275. dangerouslySetInnerHTML={{
  276. __html: t('security_settings.OAuth.Google.register_4', {
  277. url: googleCallbackUrl,
  278. }),
  279. }}
  280. />
  281. <li
  282. // biome-ignore lint/security/noDangerouslySetInnerHtml: trusted translation markup
  283. dangerouslySetInnerHTML={{
  284. __html: t('security_settings.OAuth.Google.register_5'),
  285. }}
  286. />
  287. </ol>
  288. </div>
  289. </div>
  290. </form>
  291. );
  292. };
  293. const GoogleSecurityManagementContentsWrapper = withUnstatedContainers(
  294. GoogleSecurityManagementContents,
  295. [AdminGeneralSecurityContainer, AdminGoogleSecurityContainer],
  296. );
  297. export default GoogleSecurityManagementContentsWrapper;