AdminGoogleSecurityContainer.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. import { pathUtils } from 'growi-commons';
  4. import urljoin from 'url-join';
  5. // eslint-disable-next-line no-unused-vars
  6. const logger = loggerFactory('growi:security:AdminGoogleSecurityContainer');
  7. /**
  8. * Service container for admin security page (GoogleSecurityManagement.jsx)
  9. * @extends {Container} unstated Container
  10. */
  11. export default class AdminGoogleSecurityContainer extends Container {
  12. constructor(appContainer) {
  13. super();
  14. this.appContainer = appContainer;
  15. this.state = {
  16. callbackUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/google/callback'),
  17. isGoogleStrategySetup: false,
  18. googleClientId: '',
  19. googleClientSecret: '',
  20. isSameUsernameTreatedAsIdenticalUser: false,
  21. };
  22. }
  23. /**
  24. * retrieve security data
  25. */
  26. async retrieveSecurityData() {
  27. const response = await this.appContainer.apiv3.get('/security-setting/');
  28. const { googleOAuth } = response.data.securityParams;
  29. this.setState({
  30. isGoogleStrategySetup: googleOAuth.isGoogleStrategySetup,
  31. googleClientId: googleOAuth.googleClientId || '',
  32. googleClientSecret: googleOAuth.googleClientSecret || '',
  33. isSameUsernameTreatedAsIdenticalUser: googleOAuth.isSameUsernameTreatedAsIdenticalUser || false,
  34. });
  35. }
  36. /**
  37. * Workaround for the mangling in production build to break constructor.name
  38. */
  39. static getClassName() {
  40. return 'AdminGoogleSecurityContainer';
  41. }
  42. /**
  43. * Change googleClientId
  44. */
  45. changeGoogleClientId(value) {
  46. this.setState({ googleClientId: value });
  47. }
  48. /**
  49. * Change googleClientSecret
  50. */
  51. changeGoogleClientSecret(value) {
  52. this.setState({ googleClientSecret: value });
  53. }
  54. /**
  55. * Switch isSameUsernameTreatedAsIdenticalUser
  56. */
  57. switchIsSameUsernameTreatedAsIdenticalUser() {
  58. this.setState({ isSameUsernameTreatedAsIdenticalUser: !this.state.isSameUsernameTreatedAsIdenticalUser });
  59. }
  60. /**
  61. * Update googleSetting
  62. */
  63. async updateGoogleSetting() {
  64. const response = await this.appContainer.apiv3.put('/security-setting/google-oauth', {
  65. googleClientId: this.state.googleClientId,
  66. googleClientSecret: this.state.googleClientSecret,
  67. isSameUsernameTreatedAsIdenticalUser: this.state.isSameUsernameTreatedAsIdenticalUser,
  68. });
  69. const { securitySettingParams } = response.data;
  70. this.setState({
  71. isGoogleStrategySetup: securitySettingParams.isGoogleStrategySetup,
  72. googleClientId: securitySettingParams.googleClientId,
  73. googleClientSecret: securitySettingParams.googleClientSecret,
  74. isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
  75. });
  76. return response;
  77. }
  78. }