AdminSamlSecurityContainer.js 6.7 KB

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