AdminSamlSecurityContainer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. import { pathUtils } from 'growi-commons';
  4. import urljoin from 'url-join';
  5. import removeNullPropertyFromObject from '../../../lib/util/removeNullPropertyFromObject';
  6. const logger = loggerFactory('growi:security:AdminSamlSecurityContainer');
  7. /**
  8. * Service container for admin security page (SecuritySamlSetting.jsx)
  9. * @extends {Container} unstated Container
  10. */
  11. export default class AdminSamlSecurityContainer extends Container {
  12. constructor(appContainer) {
  13. super();
  14. this.appContainer = appContainer;
  15. this.state = {
  16. retrieveError: null,
  17. useOnlyEnvVars: false,
  18. callbackUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/saml/callback'),
  19. missingMandatoryConfigKeys: [],
  20. samlEntryPoint: '',
  21. samlIssuer: '',
  22. samlCert: '',
  23. samlAttrMapId: '',
  24. samlAttrMapUsername: '',
  25. samlAttrMapMail: '',
  26. samlAttrMapFirstName: '',
  27. samlAttrMapLastName: '',
  28. isSameUsernameTreatedAsIdenticalUser: false,
  29. isSameEmailTreatedAsIdenticalUser: false,
  30. samlABLCRule: '',
  31. };
  32. }
  33. /**
  34. * retrieve security data
  35. */
  36. async retrieveSecurityData() {
  37. try {
  38. const response = await this.appContainer.apiv3.get('/security-setting/');
  39. const { samlAuth } = response.data.securityParams;
  40. this.setState({
  41. missingMandatoryConfigKeys: samlAuth.missingMandatoryConfigKeys,
  42. samlEntryPoint: samlAuth.samlEntryPoint,
  43. samlIssuer: samlAuth.samlIssuer,
  44. samlCert: samlAuth.samlCert,
  45. samlAttrMapId: samlAuth.samlAttrMapId,
  46. samlAttrMapUsername: samlAuth.samlAttrMapUsername,
  47. samlAttrMapMail: samlAuth.samlAttrMapMail,
  48. samlAttrMapFirstName: samlAuth.samlAttrMapFirstName,
  49. samlAttrMapLastName: samlAuth.samlAttrMapLastName,
  50. isSameUsernameTreatedAsIdenticalUser: samlAuth.isSameUsernameTreatedAsIdenticalUser,
  51. isSameEmailTreatedAsIdenticalUser: samlAuth.isSameEmailTreatedAsIdenticalUser,
  52. samlABLCRule: samlAuth.samlABLCRule,
  53. });
  54. return samlAuth;
  55. }
  56. catch (err) {
  57. this.setState({ retrieveError: err });
  58. logger.error(err);
  59. throw new Error('Failed to fetch data');
  60. }
  61. }
  62. /**
  63. * Workaround for the mangling in production build to break constructor.name
  64. */
  65. static getClassName() {
  66. return 'AdminSamlSecurityContainer';
  67. }
  68. /**
  69. * Change samlEntryPoint
  70. */
  71. changeSamlEntryPoint(inputValue) {
  72. this.setState({ samlEntryPoint: inputValue });
  73. }
  74. /**
  75. * Change samlIssuer
  76. */
  77. changeSamlIssuer(inputValue) {
  78. this.setState({ samlIssuer: inputValue });
  79. }
  80. /**
  81. * Change samlCert
  82. */
  83. changeSamlCert(inputValue) {
  84. this.setState({ samlCert: inputValue });
  85. }
  86. /**
  87. * Change samlAttrMapId
  88. */
  89. changeSamlAttrMapId(inputValue) {
  90. this.setState({ samlAttrMapId: inputValue });
  91. }
  92. /**
  93. * Change samlAttrMapUsername
  94. */
  95. changeSamlAttrMapUserName(inputValue) {
  96. this.setState({ samlAttrMapUsername: inputValue });
  97. }
  98. /**
  99. * Change samlAttrMapMail
  100. */
  101. changeSamlAttrMapMail(inputValue) {
  102. this.setState({ samlAttrMapMail: inputValue });
  103. }
  104. /**
  105. * Change samlAttrMapFirstName
  106. */
  107. changeSamlAttrMapFirstName(inputValue) {
  108. this.setState({ samlAttrMapFirstName: inputValue });
  109. }
  110. /**
  111. * Change samlAttrMapLastName
  112. */
  113. changeSamlAttrMapLastName(inputValue) {
  114. this.setState({ samlAttrMapLastName: inputValue });
  115. }
  116. /**
  117. * Switch isSameUsernameTreatedAsIdenticalUser
  118. */
  119. switchIsSameUsernameTreatedAsIdenticalUser() {
  120. this.setState({ isSameUsernameTreatedAsIdenticalUser: !this.state.isSameUsernameTreatedAsIdenticalUser });
  121. }
  122. /**
  123. * Switch isSameEmailTreatedAsIdenticalUser
  124. */
  125. switchIsSameEmailTreatedAsIdenticalUser() {
  126. this.setState({ isSameEmailTreatedAsIdenticalUser: !this.state.isSameEmailTreatedAsIdenticalUser });
  127. }
  128. /**
  129. * Change samlABLCRule
  130. */
  131. changeSamlABLCRule(inputValue) {
  132. this.setState({ samlABLCRule: inputValue });
  133. }
  134. /**
  135. * Update saml option
  136. */
  137. async updateSamlSetting() {
  138. let requestParams = {
  139. entryPoint: this.state.samlEntryPoint,
  140. issuer: this.state.samlIssuer,
  141. cert: this.state.samlCert,
  142. attrMapId: this.state.samlAttrMapId,
  143. attrMapUsername: this.state.samlAttrMapUsername,
  144. attrMapMail: this.state.samlAttrMapMail,
  145. attrMapFirstName: this.state.samlAttrMapFirstName,
  146. attrMapLastName: this.state.samlAttrMapLastName,
  147. isSameUsernameTreatedAsIdenticalUser: this.state.isSameUsernameTreatedAsIdenticalUser,
  148. isSameEmailTreatedAsIdenticalUser: this.state.isSameEmailTreatedAsIdenticalUser,
  149. ABLCRule: this.state.samlABLCRule,
  150. };
  151. requestParams = await removeNullPropertyFromObject(requestParams);
  152. const response = await this.appContainer.apiv3.put('/security-setting/saml', requestParams);
  153. const { securitySettingParams } = response.data;
  154. this.setState({
  155. missingMandatoryConfigKeys: securitySettingParams.missingMandatoryConfigKeys,
  156. samlEntryPoint: securitySettingParams.samlEntryPoint,
  157. samlIssuer: securitySettingParams.samlIssuer,
  158. samlCert: securitySettingParams.samlCert,
  159. samlAttrMapId: securitySettingParams.samlAttrMapId,
  160. samlAttrMapUsername: securitySettingParams.samlAttrMapUsername,
  161. samlAttrMapMail: securitySettingParams.samlAttrMapMail,
  162. samlAttrMapFirstName: securitySettingParams.samlAttrMapFirstName,
  163. samlAttrMapLastName: securitySettingParams.samlAttrMapLastName,
  164. isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
  165. isSameEmailTreatedAsIdenticalUser: securitySettingParams.isSameEmailTreatedAsIdenticalUser,
  166. samlABLCRule: securitySettingParams.samlABLCRule,
  167. });
  168. return response;
  169. }
  170. }