AdminSamlSecurityContainer.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.dummySamlEntryPoint = 0;
  16. this.dummySamlEntryPointForError = 1;
  17. this.state = {
  18. retrieveError: null,
  19. // TODO GW-1324 ABLCRure DB value takes precedence
  20. useOnlyEnvVars: false,
  21. callbackUrl: urljoin(pathUtils.removeTrailingSlash(appContainer.config.crowi.url), '/passport/saml/callback'),
  22. missingMandatoryConfigKeys: [],
  23. // set dummy value tile for using suspense
  24. samlEntryPoint: this.dummySamlEntryPoint,
  25. samlIssuer: '',
  26. samlCert: '',
  27. samlAttrMapId: '',
  28. samlAttrMapUsername: '',
  29. samlAttrMapMail: '',
  30. samlAttrMapFirstName: '',
  31. samlAttrMapLastName: '',
  32. isSameUsernameTreatedAsIdenticalUser: false,
  33. isSameEmailTreatedAsIdenticalUser: false,
  34. samlABLCRule: '',
  35. envEntryPoint: '',
  36. envIssuer: '',
  37. envCert: '',
  38. envAttrMapId: '',
  39. envAttrMapUsername: '',
  40. envAttrMapMail: '',
  41. envAttrMapFirstName: '',
  42. envAttrMapLastName: '',
  43. envABLCRule: '',
  44. };
  45. }
  46. /**
  47. * retrieve security data
  48. */
  49. async retrieveSecurityData() {
  50. try {
  51. const response = await this.appContainer.apiv3.get('/security-setting/');
  52. const { samlAuth } = response.data.securityParams;
  53. this.setState({
  54. missingMandatoryConfigKeys: samlAuth.missingMandatoryConfigKeys,
  55. useOnlyEnvVars: samlAuth.useOnlyEnvVarsForSomeOptions,
  56. samlEntryPoint: samlAuth.samlEntryPoint,
  57. samlIssuer: samlAuth.samlIssuer,
  58. samlCert: samlAuth.samlCert,
  59. samlAttrMapId: samlAuth.samlAttrMapId,
  60. samlAttrMapUsername: samlAuth.samlAttrMapUsername,
  61. samlAttrMapMail: samlAuth.samlAttrMapMail,
  62. samlAttrMapFirstName: samlAuth.samlAttrMapFirstName,
  63. samlAttrMapLastName: samlAuth.samlAttrMapLastName,
  64. isSameUsernameTreatedAsIdenticalUser: samlAuth.isSameUsernameTreatedAsIdenticalUser,
  65. isSameEmailTreatedAsIdenticalUser: samlAuth.isSameEmailTreatedAsIdenticalUser,
  66. samlABLCRule: samlAuth.samlABLCRule,
  67. envEntryPoint: samlAuth.samlEnvVarEntryPoint,
  68. envIssuer: samlAuth.samlEnvVarIssuer,
  69. envCert: samlAuth.samlEnvVarCert,
  70. envAttrMapId: samlAuth.samlEnvVarAttrMapId,
  71. envAttrMapUsername: samlAuth.samlEnvVarAttrMapUsername,
  72. envAttrMapMail: samlAuth.samlEnvVarAttrMapMail,
  73. envAttrMapFirstName: samlAuth.samlEnvVarAttrMapFirstName,
  74. envAttrMapLastName: samlAuth.samlEnvVarAttrMapLastName,
  75. envABLCRule: samlAuth.samlEnvVarABLCRule,
  76. });
  77. }
  78. catch (err) {
  79. this.setState({ retrieveError: err });
  80. logger.error(err);
  81. throw new Error('Failed to fetch data');
  82. }
  83. }
  84. /**
  85. * Workaround for the mangling in production build to break constructor.name
  86. */
  87. static getClassName() {
  88. return 'AdminSamlSecurityContainer';
  89. }
  90. /**
  91. * Change samlEntryPoint
  92. */
  93. changeSamlEntryPoint(inputValue) {
  94. this.setState({ samlEntryPoint: inputValue });
  95. }
  96. /**
  97. * Change samlIssuer
  98. */
  99. changeSamlIssuer(inputValue) {
  100. this.setState({ samlIssuer: inputValue });
  101. }
  102. /**
  103. * Change samlCert
  104. */
  105. changeSamlCert(inputValue) {
  106. this.setState({ samlCert: inputValue });
  107. }
  108. /**
  109. * Change samlAttrMapId
  110. */
  111. changeSamlAttrMapId(inputValue) {
  112. this.setState({ samlAttrMapId: inputValue });
  113. }
  114. /**
  115. * Change samlAttrMapUsername
  116. */
  117. changeSamlAttrMapUserName(inputValue) {
  118. this.setState({ samlAttrMapUsername: inputValue });
  119. }
  120. /**
  121. * Change samlAttrMapMail
  122. */
  123. changeSamlAttrMapMail(inputValue) {
  124. this.setState({ samlAttrMapMail: inputValue });
  125. }
  126. /**
  127. * Change samlAttrMapFirstName
  128. */
  129. changeSamlAttrMapFirstName(inputValue) {
  130. this.setState({ samlAttrMapFirstName: inputValue });
  131. }
  132. /**
  133. * Change samlAttrMapLastName
  134. */
  135. changeSamlAttrMapLastName(inputValue) {
  136. this.setState({ samlAttrMapLastName: inputValue });
  137. }
  138. /**
  139. * Switch isSameUsernameTreatedAsIdenticalUser
  140. */
  141. switchIsSameUsernameTreatedAsIdenticalUser() {
  142. this.setState({ isSameUsernameTreatedAsIdenticalUser: !this.state.isSameUsernameTreatedAsIdenticalUser });
  143. }
  144. /**
  145. * Switch isSameEmailTreatedAsIdenticalUser
  146. */
  147. switchIsSameEmailTreatedAsIdenticalUser() {
  148. this.setState({ isSameEmailTreatedAsIdenticalUser: !this.state.isSameEmailTreatedAsIdenticalUser });
  149. }
  150. /**
  151. * Change samlABLCRule
  152. */
  153. changeSamlABLCRule(inputValue) {
  154. this.setState({ samlABLCRule: inputValue });
  155. }
  156. /**
  157. * Update saml option
  158. */
  159. async updateSamlSetting() {
  160. let requestParams = {
  161. entryPoint: this.state.samlEntryPoint,
  162. issuer: this.state.samlIssuer,
  163. cert: this.state.samlCert,
  164. attrMapId: this.state.samlAttrMapId,
  165. attrMapUsername: this.state.samlAttrMapUsername,
  166. attrMapMail: this.state.samlAttrMapMail,
  167. attrMapFirstName: this.state.samlAttrMapFirstName,
  168. attrMapLastName: this.state.samlAttrMapLastName,
  169. isSameUsernameTreatedAsIdenticalUser: this.state.isSameUsernameTreatedAsIdenticalUser,
  170. isSameEmailTreatedAsIdenticalUser: this.state.isSameEmailTreatedAsIdenticalUser,
  171. ABLCRule: this.state.samlABLCRule,
  172. };
  173. requestParams = await removeNullPropertyFromObject(requestParams);
  174. const response = await this.appContainer.apiv3.put('/security-setting/saml', requestParams);
  175. const { securitySettingParams } = response.data;
  176. this.setState({
  177. missingMandatoryConfigKeys: securitySettingParams.missingMandatoryConfigKeys,
  178. samlEntryPoint: securitySettingParams.samlEntryPoint,
  179. samlIssuer: securitySettingParams.samlIssuer,
  180. samlCert: securitySettingParams.samlCert,
  181. samlAttrMapId: securitySettingParams.samlAttrMapId,
  182. samlAttrMapUsername: securitySettingParams.samlAttrMapUsername,
  183. samlAttrMapMail: securitySettingParams.samlAttrMapMail,
  184. samlAttrMapFirstName: securitySettingParams.samlAttrMapFirstName,
  185. samlAttrMapLastName: securitySettingParams.samlAttrMapLastName,
  186. isSameUsernameTreatedAsIdenticalUser: securitySettingParams.isSameUsernameTreatedAsIdenticalUser,
  187. isSameEmailTreatedAsIdenticalUser: securitySettingParams.isSameEmailTreatedAsIdenticalUser,
  188. samlABLCRule: securitySettingParams.samlABLCRule,
  189. });
  190. return response;
  191. }
  192. }