AdminGitHubSecurityContainer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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:AdminGitHubSecurityContainer');
  7. /**
  8. * Service container for admin security page (GitHubSecurityManagement.jsx)
  9. * @extends {Container} unstated Container
  10. */
  11. export default class AdminGitHubSecurityContainer extends Container {
  12. constructor(appContainer) {
  13. super();
  14. this.appContainer = appContainer;
  15. this.state = {
  16. appSiteUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/github/callback'),
  17. githubClientId: '',
  18. githubClientSecret: '',
  19. isSameUsernameTreatedAsIdenticalUser: true,
  20. };
  21. }
  22. /**
  23. * retrieve security data
  24. */
  25. async retrieveSecurityData() {
  26. const response = await this.appContainer.apiv3.get('/security-setting/');
  27. const { githubOAuth } = response.data.securityParams;
  28. this.setState({
  29. githubClientId: githubOAuth.githubClientId,
  30. githubClientSecret: githubOAuth.githubClientSecret,
  31. isSameUsernameTreatedAsIdenticalUser: githubOAuth.isSameUsernameTreatedAsIdenticalUser,
  32. });
  33. }
  34. /**
  35. * Workaround for the mangling in production build to break constructor.name
  36. */
  37. static getClassName() {
  38. return 'AdminGitHubSecurityContainer';
  39. }
  40. /**
  41. * Change githubClientId
  42. */
  43. changeGitHubClientId(value) {
  44. this.setState({ githubClientId: value });
  45. }
  46. /**
  47. * Change githubClientSecret
  48. */
  49. changeGitHubClientSecret(value) {
  50. this.setState({ githubClientSecret: value });
  51. }
  52. /**
  53. * Switch isSameUsernameTreatedAsIdenticalUser
  54. */
  55. switchIsSameUsernameTreatedAsIdenticalUser() {
  56. this.setState({ isSameUsernameTreatedAsIdenticalUser: !this.state.isSameUsernameTreatedAsIdenticalUser });
  57. }
  58. /**
  59. * Update githubSetting
  60. */
  61. async updateGitHubSetting() {
  62. const response = await this.appContainer.apiv3.put('/security-setting/github-oauth', {
  63. githubClientId: this.state.githubClientId,
  64. githubClientSecret: this.state.githubClientSecret,
  65. isSameUsernameTreatedAsIdenticalUser: this.state.isSameUsernameTreatedAsIdenticalUser,
  66. });
  67. const { securitySettingParams } = response.data;
  68. this.setState({
  69. githubClientId: securitySettingParams.githubClientId,
  70. githubClientSecret: securitySettingParams.githubClientSecret,
  71. isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
  72. });
  73. return response;
  74. }
  75. }