AdminSamlSecurityContainer.js 6.5 KB

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