security-setting.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:security-setting');
  3. const express = require('express');
  4. const router = express.Router();
  5. const { body } = require('express-validator/check');
  6. const ErrorV3 = require('../../models/vo/error-apiv3');
  7. const removeNullPropertyFromObject = require('../../../lib/util/removeNullPropertyFromObject');
  8. const validator = {
  9. generalSetting: [
  10. body('restrictGuestMode').if(value => value != null).isString().isIn([
  11. 'Deny', 'Readonly',
  12. ]),
  13. body('pageCompleteDeletionAuthority').if(value => value != null).isString().isIn([
  14. 'anyOne', 'adminOnly', 'adminAndAuthor',
  15. ]),
  16. body('hideRestrictedByOwner').if(value => value != null).isBoolean(),
  17. body('hideRestrictedByGroup').if(value => value != null).isBoolean(),
  18. ],
  19. authenticationSetting: [
  20. body('isEnabled').if(value => value != null).isBoolean(),
  21. body('authId').isString().isIn([
  22. 'local', 'ldap', 'saml', 'oidc', 'basic', 'google', 'github', 'twitter',
  23. ]),
  24. ],
  25. localSetting: [
  26. body('registrationMode').isString().isIn([
  27. 'Open', 'Restricted', 'Closed',
  28. ]),
  29. body('registrationWhiteList').if(value => value != null).isArray().customSanitizer((value, { req }) => {
  30. return value.filter(email => email !== '');
  31. }),
  32. ],
  33. ldapAuth: [
  34. body('serverUrl').if(value => value != null).isString(),
  35. body('isUserBind').if(value => value != null).isBoolean(),
  36. body('ldapBindDN').if(value => value != null).isString(),
  37. body('ldapBindDNPassword').if(value => value != null).isString(),
  38. body('ldapSearchFilter').if(value => value != null).isString(),
  39. body('ldapAttrMapUsername').if(value => value != null).isString(),
  40. body('isSameUsernameTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  41. body('ldapAttrMapMail').if(value => value != null).isString(),
  42. body('ldapAttrMapName').if(value => value != null).isString(),
  43. body('ldapGroupSearchBase').if(value => value != null).isString(),
  44. body('ldapGroupSearchFilter').if(value => value != null).isString(),
  45. body('ldapGroupDnProperty').if(value => value != null).isString(),
  46. ],
  47. samlAuth: [
  48. body('entryPoint').if(value => value != null).isString(),
  49. body('issuer').if(value => value != null).isString(),
  50. body('cert').if(value => value != null).isString(),
  51. body('attrMapId').if(value => value != null).isString(),
  52. body('attrMapUsername').if(value => value != null).isString(),
  53. body('attrMapMail').if(value => value != null).isString(),
  54. body('attrMapFirstName').if(value => value != null).isString(),
  55. body('attrMapLastName').if(value => value != null).isString(),
  56. body('isSameUsernameTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  57. body('isSameEmailTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  58. body('ABLCRule').if(value => value != null).isString(),
  59. ],
  60. oidcAuth: [
  61. body('oidcProviderName').if(value => value != null).isString(),
  62. body('oidcIssuerHost').if(value => value != null).isString(),
  63. body('oidcClientId').if(value => value != null).isString(),
  64. body('oidcClientSecret').if(value => value != null).isString(),
  65. body('oidcAttrMapId').if(value => value != null).isString(),
  66. body('oidcAttrMapUserName').if(value => value != null).isString(),
  67. body('oidcAttrMapEmail').if(value => value != null).isString(),
  68. body('isSameUsernameTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  69. body('isSameEmailTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  70. ],
  71. basicAuth: [
  72. body('isSameUsernameTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  73. ],
  74. googleOAuth: [
  75. body('googleClientId').if(value => value != null).isString(),
  76. body('googleClientSecret').if(value => value != null).isString(),
  77. body('isSameUsernameTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  78. ],
  79. githubOAuth: [
  80. body('githubClientId').if(value => value != null).isString(),
  81. body('githubClientSecret').if(value => value != null).isString(),
  82. body('isSameUsernameTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  83. ],
  84. twitterOAuth: [
  85. body('twitterConsumerKey').if(value => value != null).isString(),
  86. body('twitterConsumerSecret').if(value => value != null).isString(),
  87. body('isSameUsernameTreatedAsIdenticalUser').if(value => value != null).isBoolean(),
  88. ],
  89. };
  90. /**
  91. * @swagger
  92. * tags:
  93. * name: SecuritySetting
  94. */
  95. /**
  96. * @swagger
  97. *
  98. * components:
  99. * schemas:
  100. * GeneralSetting:
  101. * type: object
  102. * properties:
  103. * restrictGuestMode:
  104. * type: string
  105. * description: type of restrictGuestMode
  106. * pageCompleteDeletionAuthority:
  107. * type: string
  108. * description: type of pageDeletionAuthority
  109. * hideRestrictedByOwner:
  110. * type: boolean
  111. * description: enable hide by owner
  112. * hideRestrictedByGroup:
  113. * type: boolean
  114. * description: enable hide by group
  115. * LocalSetting:
  116. * type: object
  117. * properties:
  118. * isLocalEnabled:
  119. * type: boolean
  120. * description: local setting mode
  121. * registrationMode:
  122. * type: string
  123. * description: type of registrationMode
  124. * registrationWhiteList:
  125. * type: array
  126. * description: array of regsitrationList
  127. * items:
  128. * type: string
  129. * description: registration whiteList
  130. * LdapAuthSetting:
  131. * type: object
  132. * properties:
  133. * serverUrl:
  134. * type: string
  135. * description: server url for ldap
  136. * isUserBind:
  137. * type: boolean
  138. * description: enable user bind
  139. * ldapBindDN:
  140. * type: string
  141. * description: the query used to bind with the directory service
  142. * ldapBindDNPassword:
  143. * type: string
  144. * description: the password that is entered in the login page will be used to bind
  145. * ldapSearchFilter:
  146. * type: string
  147. * description: the query used to locate the authenticated user
  148. * ldapAttrMapUsername:
  149. * type: string
  150. * description: specification of mappings for username when creating new users
  151. * isSameUsernameTreatedAsIdenticalUser:
  152. * type: boolean
  153. * description: local account automatically linked the user name matched
  154. * ldapAttrMapMail:
  155. * type: string
  156. * description: specification of mappings for mail address when creating new users
  157. * ldapAttrMapName:
  158. * type: string
  159. * description: Specification of mappings for full name address when creating new users
  160. * ldapGroupSearchBase:
  161. * type: string
  162. * description: the base DN from which to search for groups.
  163. * ldapGroupSearchFilter:
  164. * type: string
  165. * description: the query used to filter for groups
  166. * ldapGroupDnProperty:
  167. * type: string
  168. * description: The property of user object to use in dn interpolation of Group Search Filter
  169. * SamlAuthSetting:
  170. * type: object
  171. * properties:
  172. * samlEntryPoint:
  173. * type: string
  174. * description: entry point for saml
  175. * samlIssuer:
  176. * type: string
  177. * description: issuer for saml
  178. * samlCert:
  179. * type: string
  180. * description: certificate for saml
  181. * samlAttrMapId:
  182. * type: string
  183. * description: attribute mapping id for saml
  184. * samlAttrMapUserName:
  185. * type: string
  186. * description: attribute mapping user name for saml
  187. * samlAttrMapMail:
  188. * type: string
  189. * description: attribute mapping mail for saml
  190. * samlAttrMapFirstName:
  191. * type: string
  192. * description: attribute mapping first name for saml
  193. * samlAttrMapLastName:
  194. * type: string
  195. * description: attribute mapping last name for saml
  196. * isSameUsernameTreatedAsIdenticalUser:
  197. * type: boolean
  198. * description: local account automatically linked the user name matched
  199. * isSameEmailTreatedAsIdenticalUser:
  200. * type: boolean
  201. * description: local account automatically linked the email matched
  202. * samlABLCRule:
  203. * type: string
  204. * description: ABLCRule for saml
  205. * OidcAuthSetting:
  206. * type: object
  207. * properties:
  208. * oidcProviderName:
  209. * type: string
  210. * description: provider name for oidc
  211. * oidcIssuerHost:
  212. * type: string
  213. * description: issuer host for oidc
  214. * oidcClientId:
  215. * type: string
  216. * description: client id for oidc
  217. * oidcClientSecret:
  218. * type: string
  219. * description: client secret for oidc
  220. * oidcAttrMapId:
  221. * type: string
  222. * description: attr map id for oidc
  223. * oidcAttrMapUserName:
  224. * type: string
  225. * description: attr map username for oidc
  226. * oidcAttrMapName:
  227. * type: string
  228. * description: attr map name for oidc
  229. * oidcAttrMapMail:
  230. * type: string
  231. * description: attr map mail for oidc
  232. * isSameUsernameTreatedAsIdenticalUser:
  233. * type: boolean
  234. * description: local account automatically linked the user name matched
  235. * isSameEmailTreatedAsIdenticalUser:
  236. * type: boolean
  237. * description: local account automatically linked the email matched
  238. * BasicAuthSetting:
  239. * type: object
  240. * properties:
  241. * isSameUsernameTreatedAsIdenticalUser:
  242. * type: boolean
  243. * description: local account automatically linked the email matched
  244. * GitHubOAuthSetting:
  245. * type: object
  246. * properties:
  247. * githubClientId:
  248. * type: string
  249. * description: key of comsumer
  250. * githubClientSecret:
  251. * type: string
  252. * description: password of comsumer
  253. * isSameUsernameTreatedAsIdenticalUser:
  254. * type: boolean
  255. * description: local account automatically linked the email matched
  256. * GoogleOAuthSetting:
  257. * type: object
  258. * properties:
  259. * googleClientId:
  260. * type: string
  261. * description: key of comsumer
  262. * googleClientSecret:
  263. * type: string
  264. * description: password of comsumer
  265. * isSameUsernameTreatedAsIdenticalUser:
  266. * type: boolean
  267. * description: local account automatically linked the email matched
  268. * TwitterOAuthSetting:
  269. * type: object
  270. * properties:
  271. * twitterConsumerKey:
  272. * type: string
  273. * description: key of comsumer
  274. * twitterConsumerSecret:
  275. * type: string
  276. * description: password of comsumer
  277. * isSameUsernameTreatedAsIdenticalUser:
  278. * type: boolean
  279. * description: local account automatically linked the email matched
  280. */
  281. module.exports = (crowi) => {
  282. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  283. const adminRequired = require('../../middleware/admin-required')(crowi);
  284. const csrf = require('../../middleware/csrf')(crowi);
  285. const { ApiV3FormValidator } = crowi.middlewares;
  286. /**
  287. * @swagger
  288. *
  289. * /_api/v3/security-setting/:
  290. * get:
  291. * tags: [SecuritySetting, apiv3]
  292. * description: Get security paramators
  293. * responses:
  294. * 200:
  295. * description: params of security
  296. * content:
  297. * application/json:
  298. * schema:
  299. * properties:
  300. * securityParams:
  301. * type: object
  302. * description: security params
  303. */
  304. router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
  305. const securityParams = {
  306. generalSetting: {
  307. restrictGuestMode: await crowi.configManager.getConfig('crowi', 'security:restrictGuestMode'),
  308. pageCompleteDeletionAuthority: await crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority'),
  309. hideRestrictedByOwner: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByOwner'),
  310. hideRestrictedByGroup: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByGroup'),
  311. wikiMode: await crowi.configManager.getConfig('crowi', 'security:wikiMode'),
  312. },
  313. localSetting: {
  314. useOnlyEnvVarsForSomeOptions: await crowi.configManager.getConfig('crowi', 'security:passport-local:useOnlyEnvVarsForSomeOptions'),
  315. registrationMode: await crowi.configManager.getConfig('crowi', 'security:registrationMode'),
  316. registrationWhiteList: await crowi.configManager.getConfig('crowi', 'security:registrationWhiteList'),
  317. },
  318. generalAuth: {
  319. isLocalEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-local:isEnabled'),
  320. isLdapEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:isEnabled'),
  321. isSamlEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-saml:isEnabled'),
  322. isOidcEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:isEnabled'),
  323. isBasicEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-basic:isEnabled'),
  324. isGoogleEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-google:isEnabled'),
  325. isGitHubEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-github:isEnabled'),
  326. isTwitterEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isEnabled'),
  327. },
  328. ldapAuth: {
  329. serverUrl: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:serverUrl'),
  330. isUserBind: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:isUserBind'),
  331. ldapBindDN: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:bindDN'),
  332. ldapBindDNPassword: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:bindDNPassword'),
  333. ldapSearchFilter: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:searchFilter'),
  334. ldapAttrMapUsername: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapUsername'),
  335. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser'),
  336. ldapAttrMapMail: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapMail'),
  337. ldapAttrMapName: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapName'),
  338. ldapGroupSearchBase: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupSearchBase'),
  339. ldapGroupSearchFilter: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupSearchFilter'),
  340. ldapGroupDnProperty: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupDnProperty'),
  341. },
  342. samlAuth: {
  343. missingMandatoryConfigKeys: await crowi.passportService.getSamlMissingMandatoryConfigKeys(),
  344. useOnlyEnvVarsForSomeOptions: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:useOnlyEnvVarsForSomeOptions'),
  345. samlEntryPoint: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:entryPoint'),
  346. samlEnvVarEntryPoint: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:entryPoint'),
  347. samlIssuer: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:issuer'),
  348. samlEnvVarIssuer: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:issuer'),
  349. samlCert: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:cert'),
  350. samlEnvVarCert: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:cert'),
  351. samlAttrMapId: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapId'),
  352. samlEnvVarAttrMapId: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:attrMapId'),
  353. samlAttrMapUsername: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapUsername'),
  354. samlEnvVarAttrMapUsername: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:attrMapUsername'),
  355. samlAttrMapMail: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapMail'),
  356. samlEnvVarAttrMapMail: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:attrMapMail'),
  357. samlAttrMapFirstName: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapFirstName'),
  358. samlEnvVarAttrMapFirstName: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:attrMapFirstName'),
  359. samlAttrMapLastName: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapLastName'),
  360. samlEnvVarAttrMapLastName: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:attrMapLastName'),
  361. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-saml:isSameUsernameTreatedAsIdenticalUser'),
  362. isSameEmailTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-saml:isSameEmailTreatedAsIdenticalUser'),
  363. samlABLCRule: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:ABLCRule'),
  364. samlEnvVarABLCRule: await crowi.configManager.getConfigFromEnvVars('crowi', 'security:passport-saml:ABLCRule'),
  365. },
  366. oidcAuth: {
  367. oidcProviderName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:providerName'),
  368. oidcIssuerHost: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:issuerHost'),
  369. oidcClientId: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:clientId'),
  370. oidcClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:clientSecret'),
  371. oidcAttrMapId: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapId'),
  372. oidcAttrMapUserName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapUserName'),
  373. oidcAttrMapName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapName'),
  374. oidcAttrMapEmail: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapMail'),
  375. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:isSameUsernameTreatedAsIdenticalUser'),
  376. isSameEmailTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:isSameEmailTreatedAsIdenticalUser'),
  377. },
  378. basicAuth: {
  379. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-basic:isSameUsernameTreatedAsIdenticalUser'),
  380. },
  381. googleOAuth: {
  382. googleClientId: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientId'),
  383. googleClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientSecret'),
  384. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-google:isSameUsernameTreatedAsIdenticalUser'),
  385. },
  386. githubOAuth: {
  387. githubClientId: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientId'),
  388. githubClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientSecret'),
  389. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-github:isSameUsernameTreatedAsIdenticalUser'),
  390. },
  391. twitterOAuth: {
  392. twitterConsumerKey: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
  393. twitterConsumerSecret: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
  394. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser'),
  395. },
  396. };
  397. return res.apiv3({ securityParams });
  398. });
  399. /**
  400. * @swagger
  401. *
  402. * /_api/v3/security-setting/authentication/enabled:
  403. * put:
  404. * tags: [SecuritySetting, apiv3]
  405. * description: Update authentication isEnabled
  406. * requestBody:
  407. * required: true
  408. * content:
  409. * application/json:
  410. * schema:
  411. * type: object
  412. * properties:
  413. * isEnabled:
  414. * type: boolean
  415. * target:
  416. * type: string
  417. * responses:
  418. * 200:
  419. * description: Succeeded to enable authentication
  420. * content:
  421. * application/json:
  422. * schema:
  423. * type: object
  424. * description: updated param
  425. */
  426. router.put('/authentication/enabled', loginRequiredStrictly, adminRequired, csrf, validator.authenticationSetting, ApiV3FormValidator, async(req, res) => {
  427. const { isEnabled, authId } = req.body;
  428. let setupStrategies = await crowi.passportService.getSetupStrategies();
  429. // Reflect request param
  430. setupStrategies = setupStrategies.filter(strategy => strategy !== authId);
  431. if (setupStrategies.length === 0) {
  432. return res.apiv3Err(new ErrorV3('Can not turn everything off'), 405);
  433. }
  434. const enableParams = { [`security:passport-${authId}:isEnabled`]: isEnabled };
  435. try {
  436. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', enableParams);
  437. await crowi.passportService.setupStrategyById(authId);
  438. const responseParams = {
  439. [`security:passport-${authId}:isEnabled`]: await crowi.configManager.getConfig('crowi', `security:passport-${authId}:isEnabled`),
  440. };
  441. return res.apiv3({ responseParams });
  442. }
  443. catch (err) {
  444. const msg = 'Error occurred in updating enable setting';
  445. logger.error('Error', err);
  446. return res.apiv3Err(new ErrorV3(msg, 'update-enable-setting failed'));
  447. }
  448. });
  449. /**
  450. * @swagger
  451. *
  452. * /_api/v3/security-setting/authentication:
  453. * get:
  454. * tags: [SecuritySetting, apiv3]
  455. * description: Get setup strategies for passport
  456. * responses:
  457. * 200:
  458. * description: params of setup strategies
  459. * content:
  460. * application/json:
  461. * schema:
  462. * properties:
  463. * setupStrategies:
  464. * type: array
  465. * description: setup strategies list
  466. * items:
  467. * type: string
  468. * description: setup strategie
  469. * example: ["local"]
  470. */
  471. router.get('/authentication/', loginRequiredStrictly, adminRequired, async(req, res) => {
  472. const setupStrategies = await crowi.passportService.getSetupStrategies();
  473. return res.apiv3({ setupStrategies });
  474. });
  475. /**
  476. * @swagger
  477. *
  478. * /_api/v3/security-setting/general-setting:
  479. * put:
  480. * tags: [SecuritySetting, apiv3]
  481. * description: Update GeneralSetting
  482. * requestBody:
  483. * required: true
  484. * content:
  485. * application/json:
  486. * schema:
  487. * $ref: '#/components/schemas/GeneralSetting'
  488. * responses:
  489. * 200:
  490. * description: Succeeded to update general Setting
  491. * content:
  492. * application/json:
  493. * schema:
  494. * $ref: '#/components/schemas/GeneralSetting'
  495. */
  496. router.put('/general-setting', loginRequiredStrictly, adminRequired, csrf, validator.generalSetting, ApiV3FormValidator, async(req, res) => {
  497. const requestParams = {
  498. 'security:restrictGuestMode': req.body.restrictGuestMode,
  499. 'security:pageCompleteDeletionAuthority': req.body.pageCompleteDeletionAuthority,
  500. 'security:list-policy:hideRestrictedByOwner': req.body.hideRestrictedByOwner,
  501. 'security:list-policy:hideRestrictedByGroup': req.body.hideRestrictedByGroup,
  502. };
  503. const wikiMode = await crowi.configManager.getConfig('crowi', 'security:wikiMode');
  504. if (wikiMode === 'private' || wikiMode === 'public') {
  505. logger.debug('security:restrictGuestMode will not be changed because wiki mode is forced to set');
  506. delete requestParams['security:restrictGuestMode'];
  507. }
  508. try {
  509. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  510. const securitySettingParams = {
  511. restrictGuestMode: await crowi.configManager.getConfig('crowi', 'security:restrictGuestMode'),
  512. pageCompleteDeletionAuthority: await crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority'),
  513. hideRestrictedByOwner: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByOwner'),
  514. hideRestrictedByGroup: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByGroup'),
  515. };
  516. return res.apiv3({ securitySettingParams });
  517. }
  518. catch (err) {
  519. const msg = 'Error occurred in updating security setting';
  520. logger.error('Error', err);
  521. return res.apiv3Err(new ErrorV3(msg, 'update-secuirty-setting failed'));
  522. }
  523. });
  524. /**
  525. * @swagger
  526. *
  527. * /_api/v3/security-setting/all-share-links:
  528. * get:
  529. * tags: [ShareLinkSettings, apiv3]
  530. * description: Get All ShareLinks at Share Link Setting
  531. * responses:
  532. * 200:
  533. * description: all share links
  534. * content:
  535. * application/json:
  536. * schema:
  537. * properties:
  538. * securityParams:
  539. * type: object
  540. * description: suceed to get all share links
  541. */
  542. router.get('/all-share-links/', /* loginRequiredStrictly, adminRequired, csrf, ApiV3FormValidator, */ async(req, res) => {
  543. const ShareLink = crowi.model('ShareLink');
  544. const page = parseInt(req.query.page) || 1;
  545. const limit = 10;
  546. const linkQuery = {};
  547. try {
  548. const shareLinksResult = await ShareLink.paginate(
  549. linkQuery,
  550. {
  551. page,
  552. limit,
  553. },
  554. );
  555. return res.apiv3({ shareLinksResult });
  556. }
  557. catch (err) {
  558. const msg = 'Error occured in get share link';
  559. logger.error('Error', err);
  560. return res.apiv3Err(new ErrorV3(msg, 'get-all-share-links-failed'));
  561. }
  562. });
  563. /**
  564. * @swagger
  565. *
  566. * /_api/v3/security-setting/all-share-links:
  567. * delete:
  568. * tags: [ShareLinkSettings, apiv3]
  569. * description: Delete All ShareLinks at Share Link Setting
  570. * responses:
  571. * 200:
  572. * description: succeed to delete all share links
  573. */
  574. router.delete('/all-share-links/', loginRequiredStrictly, adminRequired, csrf, async(req, res) => {
  575. const ShareLink = crowi.model('ShareLink');
  576. try {
  577. const removedAct = await ShareLink.remove({});
  578. const removeTotal = await removedAct.n;
  579. return res.apiv3({ removeTotal });
  580. }
  581. catch (err) {
  582. const msg = 'Error occured in delete all share links';
  583. logger.error('Error', err);
  584. return res.apiv3Err(new ErrorV3(msg, 'failed-to-delete-all-share-links'));
  585. }
  586. });
  587. /**
  588. * @swagger
  589. *
  590. * /_api/v3/security-setting/local-setting:
  591. * put:
  592. * tags: [LocalSetting, apiv3]
  593. * description: Update LocalSetting
  594. * requestBody:
  595. * required: true
  596. * content:
  597. * application/json:
  598. * schema:
  599. * $ref: '#/components/schemas/LocalSetting'
  600. * responses:
  601. * 200:
  602. * description: Succeeded to update local Setting
  603. * content:
  604. * application/json:
  605. * schema:
  606. * $ref: '#/components/schemas/LocalSetting'
  607. */
  608. router.put('/local-setting', loginRequiredStrictly, adminRequired, csrf, validator.localSetting, ApiV3FormValidator, async(req, res) => {
  609. const requestParams = {
  610. 'security:registrationMode': req.body.registrationMode,
  611. 'security:registrationWhiteList': req.body.registrationWhiteList,
  612. };
  613. try {
  614. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  615. await crowi.passportService.setupStrategyById('local');
  616. const localSettingParams = {
  617. registrationMode: await crowi.configManager.getConfig('crowi', 'security:registrationMode'),
  618. registrationWhiteList: await crowi.configManager.getConfig('crowi', 'security:registrationWhiteList'),
  619. };
  620. return res.apiv3({ localSettingParams });
  621. }
  622. catch (err) {
  623. const msg = 'Error occurred in updating local setting';
  624. logger.error('Error', err);
  625. return res.apiv3Err(new ErrorV3(msg, 'update-local-setting failed'));
  626. }
  627. });
  628. /**
  629. * @swagger
  630. *
  631. * /_api/v3/security-setting/ldap:
  632. * put:
  633. * tags: [SecuritySetting, apiv3]
  634. * description: Update LDAP setting
  635. * requestBody:
  636. * required: true
  637. * content:
  638. * application/json:
  639. * schema:
  640. * $ref: '#/components/schemas/LdapAuthSetting'
  641. * responses:
  642. * 200:
  643. * description: Succeeded to update LDAP setting
  644. * content:
  645. * application/json:
  646. * schema:
  647. * $ref: '#/components/schemas/LdapAuthSetting'
  648. */
  649. router.put('/ldap', loginRequiredStrictly, adminRequired, csrf, validator.ldapAuth, ApiV3FormValidator, async(req, res) => {
  650. const requestParams = {
  651. 'security:passport-ldap:serverUrl': req.body.serverUrl,
  652. 'security:passport-ldap:isUserBind': req.body.isUserBind,
  653. 'security:passport-ldap:bindDN': req.body.ldapBindDN,
  654. 'security:passport-ldap:bindDNPassword': req.body.ldapBindDNPassword,
  655. 'security:passport-ldap:searchFilter': req.body.ldapSearchFilter,
  656. 'security:passport-ldap:attrMapUsername': req.body.ldapAttrMapUsername,
  657. 'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  658. 'security:passport-ldap:attrMapMail': req.body.ldapAttrMapMail,
  659. 'security:passport-ldap:attrMapName': req.body.ldapAttrMapName,
  660. 'security:passport-ldap:groupSearchBase': req.body.ldapGroupSearchBase,
  661. 'security:passport-ldap:groupSearchFilter': req.body.ldapGroupSearchFilter,
  662. 'security:passport-ldap:groupDnProperty': req.body.ldapGroupDnProperty,
  663. };
  664. try {
  665. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  666. await crowi.passportService.setupStrategyById('ldap');
  667. const securitySettingParams = {
  668. serverUrl: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:serverUrl'),
  669. isUserBind: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:isUserBind'),
  670. ldapBindDN: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:bindDN'),
  671. ldapBindDNPassword: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:bindDNPassword'),
  672. ldapSearchFilter: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:searchFilter'),
  673. ldapAttrMapUsername: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapUsername'),
  674. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser'),
  675. ldapAttrMapMail: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapMail'),
  676. ldapAttrMapName: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapName'),
  677. ldapGroupSearchBase: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupSearchBase'),
  678. ldapGroupSearchFilter: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupSearchFilter'),
  679. ldapGroupDnProperty: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupDnProperty'),
  680. };
  681. return res.apiv3({ securitySettingParams });
  682. }
  683. catch (err) {
  684. const msg = 'Error occurred in updating SAML setting';
  685. logger.error('Error', err);
  686. return res.apiv3Err(new ErrorV3(msg, 'update-SAML-failed'));
  687. }
  688. });
  689. /**
  690. * @swagger
  691. *
  692. * /_api/v3/security-setting/saml:
  693. * put:
  694. * tags: [SecuritySetting, apiv3]
  695. * description: Update SAML setting
  696. * requestBody:
  697. * required: true
  698. * content:
  699. * application/json:
  700. * schema:
  701. * $ref: '#/components/schemas/SamlAuthSetting'
  702. * responses:
  703. * 200:
  704. * description: Succeeded to update SAML setting
  705. * content:
  706. * application/json:
  707. * schema:
  708. * $ref: '#/components/schemas/SamlAuthSetting'
  709. */
  710. router.put('/saml', loginRequiredStrictly, adminRequired, csrf, validator.samlAuth, ApiV3FormValidator, async(req, res) => {
  711. // For the value of each mandatory items,
  712. // check whether it from the environment variables is empty and form value to update it is empty
  713. // validate the syntax of a attribute - based login control rule
  714. const invalidValues = [];
  715. for (const configKey of crowi.passportService.mandatoryConfigKeysForSaml) {
  716. const key = configKey.replace('security:passport-saml:', '');
  717. const formValue = req.body[key];
  718. if (crowi.configManager.getConfigFromEnvVars('crowi', configKey) === null && formValue == null) {
  719. const formItemName = req.t(`security_setting.form_item_name.${key}`);
  720. invalidValues.push(req.t('form_validation.required', formItemName));
  721. }
  722. }
  723. if (invalidValues.length !== 0) {
  724. return res.apiv3Err(req.t('form_validation.error_message'), 400, invalidValues);
  725. }
  726. const rule = req.body.ABLCRule;
  727. // Empty string disables attribute-based login control.
  728. // So, when rule is empty string, validation is passed.
  729. if (rule != null) {
  730. try {
  731. crowi.passportService.parseABLCRule(rule);
  732. }
  733. catch (err) {
  734. return res.apiv3Err(req.t('form_validation.invalid_syntax', req.t('security_setting.form_item_name.ABLCRule')), 400);
  735. }
  736. }
  737. const requestParams = {
  738. 'security:passport-saml:entryPoint': req.body.entryPoint,
  739. 'security:passport-saml:issuer': req.body.issuer,
  740. 'security:passport-saml:cert': req.body.cert,
  741. 'security:passport-saml:attrMapId': req.body.attrMapId,
  742. 'security:passport-saml:attrMapUsername': req.body.attrMapUsername,
  743. 'security:passport-saml:attrMapMail': req.body.attrMapMail,
  744. 'security:passport-saml:attrMapFirstName': req.body.attrMapFirstName,
  745. 'security:passport-saml:attrMapLastName': req.body.attrMapLastName,
  746. 'security:passport-saml:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  747. 'security:passport-saml:isSameEmailTreatedAsIdenticalUser': req.body.isSameEmailTreatedAsIdenticalUser,
  748. 'security:passport-saml:ABLCRule': req.body.ABLCRule,
  749. };
  750. try {
  751. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  752. await crowi.passportService.setupStrategyById('saml');
  753. const securitySettingParams = {
  754. missingMandatoryConfigKeys: await crowi.passportService.getSamlMissingMandatoryConfigKeys(),
  755. samlEntryPoint: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:entryPoint'),
  756. samlIssuer: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:issuer'),
  757. samlCert: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:cert'),
  758. samlAttrMapId: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapId'),
  759. samlAttrMapUsername: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapUsername'),
  760. samlAttrMapMail: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapMail'),
  761. samlAttrMapFirstName: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapFirstName'),
  762. samlAttrMapLastName: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapLastName'),
  763. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-saml:isSameUsernameTreatedAsIdenticalUser'),
  764. isSameEmailTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-saml:isSameEmailTreatedAsIdenticalUser'),
  765. samlABLCRule: await crowi.configManager.getConfig('crowi', 'security:passport-saml:ABLCRule'),
  766. };
  767. return res.apiv3({ securitySettingParams });
  768. }
  769. catch (err) {
  770. const msg = 'Error occurred in updating SAML setting';
  771. logger.error('Error', err);
  772. return res.apiv3Err(new ErrorV3(msg, 'update-SAML-failed'));
  773. }
  774. });
  775. /**
  776. * @swagger
  777. *
  778. * /_api/v3/security-setting/oidc:
  779. * put:
  780. * tags: [SecuritySetting, apiv3]
  781. * description: Update OpenID Connect setting
  782. * requestBody:
  783. * required: true
  784. * content:
  785. * application/json:
  786. * schema:
  787. * $ref: '#/components/schemas/OidcAuthSetting'
  788. * responses:
  789. * 200:
  790. * description: Succeeded to update OpenID Connect setting
  791. * content:
  792. * application/json:
  793. * schema:
  794. * $ref: '#/components/schemas/OidcAuthSetting'
  795. */
  796. router.put('/oidc', loginRequiredStrictly, adminRequired, csrf, validator.oidcAuth, ApiV3FormValidator, async(req, res) => {
  797. const requestParams = {
  798. 'security:passport-oidc:providerName': req.body.oidcProviderName,
  799. 'security:passport-oidc:issuerHost': req.body.oidcIssuerHost,
  800. 'security:passport-oidc:clientId': req.body.oidcClientId,
  801. 'security:passport-oidc:clientSecret': req.body.oidcClientSecret,
  802. 'security:passport-oidc:attrMapId': req.body.oidcAttrMapId,
  803. 'security:passport-oidc:attrMapUserName': req.body.oidcAttrMapUserName,
  804. 'security:passport-oidc:attrMapName': req.body.oidcAttrMapName,
  805. 'security:passport-oidc:attrMapMail': req.body.oidcAttrMapEmail,
  806. 'security:passport-oidc:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  807. 'security:passport-oidc:isSameEmailTreatedAsIdenticalUser': req.body.isSameEmailTreatedAsIdenticalUser,
  808. };
  809. try {
  810. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  811. await crowi.passportService.setupStrategyById('oidc');
  812. const securitySettingParams = {
  813. oidcProviderName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:providerName'),
  814. oidcIssuerHost: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:issuerHost'),
  815. oidcClientId: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:clientId'),
  816. oidcClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:clientSecret'),
  817. oidcAttrMapId: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapId'),
  818. oidcAttrMapUserName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapUserName'),
  819. oidcAttrMapName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapName'),
  820. oidcAttrMapEmail: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapMail'),
  821. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:isSameUsernameTreatedAsIdenticalUser'),
  822. isSameEmailTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:isSameEmailTreatedAsIdenticalUser'),
  823. };
  824. return res.apiv3({ securitySettingParams });
  825. }
  826. catch (err) {
  827. const msg = 'Error occurred in updating OpenIDConnect';
  828. logger.error('Error', err);
  829. return res.apiv3Err(new ErrorV3(msg, 'update-OpenIDConnect-failed'));
  830. }
  831. });
  832. /**
  833. * @swagger
  834. *
  835. * /_api/v3/security-setting/basic:
  836. * put:
  837. * tags: [SecuritySetting, apiv3]
  838. * description: Update basic
  839. * requestBody:
  840. * required: true
  841. * content:
  842. * application/json:
  843. * schema:
  844. * $ref: '#/components/schemas/BasicAuthSetting'
  845. * responses:
  846. * 200:
  847. * description: Succeeded to update basic
  848. * content:
  849. * application/json:
  850. * schema:
  851. * $ref: '#/components/schemas/BasicAuthSetting'
  852. */
  853. router.put('/basic', loginRequiredStrictly, adminRequired, csrf, validator.basicAuth, ApiV3FormValidator, async(req, res) => {
  854. const requestParams = {
  855. 'security:passport-basic:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  856. };
  857. try {
  858. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  859. await crowi.passportService.setupStrategyById('basic');
  860. const securitySettingParams = {
  861. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-basic:isSameUsernameTreatedAsIdenticalUser'),
  862. };
  863. return res.apiv3({ securitySettingParams });
  864. }
  865. catch (err) {
  866. const msg = 'Error occurred in updating basicAuth';
  867. logger.error('Error', err);
  868. return res.apiv3Err(new ErrorV3(msg, 'update-basicOAuth-failed'));
  869. }
  870. });
  871. /**
  872. * @swagger
  873. *
  874. * /_api/v3/security-setting/google-oauth:
  875. * put:
  876. * tags: [SecuritySetting, apiv3]
  877. * description: Update google OAuth
  878. * requestBody:
  879. * required: true
  880. * content:
  881. * application/json:
  882. * schema:
  883. * $ref: '#/components/schemas/GoogleOAuthSetting'
  884. * responses:
  885. * 200:
  886. * description: Succeeded to google OAuth
  887. * content:
  888. * application/json:
  889. * schema:
  890. * $ref: '#/components/schemas/GoogleOAuthSetting'
  891. */
  892. router.put('/google-oauth', loginRequiredStrictly, adminRequired, csrf, validator.googleOAuth, ApiV3FormValidator, async(req, res) => {
  893. const requestParams = {
  894. 'security:passport-google:clientId': req.body.googleClientId,
  895. 'security:passport-google:clientSecret': req.body.googleClientSecret,
  896. 'security:passport-google:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  897. };
  898. try {
  899. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  900. await crowi.passportService.setupStrategyById('google');
  901. const securitySettingParams = {
  902. googleClientId: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientId'),
  903. googleClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientSecret'),
  904. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-google:isSameUsernameTreatedAsIdenticalUser'),
  905. };
  906. return res.apiv3({ securitySettingParams });
  907. }
  908. catch (err) {
  909. const msg = 'Error occurred in updating googleOAuth';
  910. logger.error('Error', err);
  911. return res.apiv3Err(new ErrorV3(msg, 'update-googleOAuth-failed'));
  912. }
  913. });
  914. /**
  915. * @swagger
  916. *
  917. * /_api/v3/security-setting/github-oauth:
  918. * put:
  919. * tags: [SecuritySetting, apiv3]
  920. * description: Update github OAuth
  921. * requestBody:
  922. * required: true
  923. * content:
  924. * application/json:
  925. * schema:
  926. * $ref: '#/components/schemas/GitHubOAuthSetting'
  927. * responses:
  928. * 200:
  929. * description: Succeeded to github OAuth
  930. * content:
  931. * application/json:
  932. * schema:
  933. * $ref: '#/components/schemas/GitHubOAuthSetting'
  934. */
  935. router.put('/github-oauth', loginRequiredStrictly, adminRequired, csrf, validator.githubOAuth, ApiV3FormValidator, async(req, res) => {
  936. const requestParams = {
  937. 'security:passport-github:clientId': req.body.githubClientId,
  938. 'security:passport-github:clientSecret': req.body.githubClientSecret,
  939. 'security:passport-github:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  940. };
  941. try {
  942. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  943. await crowi.passportService.setupStrategyById('github');
  944. const securitySettingParams = {
  945. githubClientId: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientId'),
  946. githubClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientSecret'),
  947. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-github:isSameUsernameTreatedAsIdenticalUser'),
  948. };
  949. return res.apiv3({ securitySettingParams });
  950. }
  951. catch (err) {
  952. // reset strategy
  953. await crowi.passportService.resetGitHubStrategy();
  954. const msg = 'Error occurred in updating githubOAuth';
  955. logger.error('Error', err);
  956. return res.apiv3Err(new ErrorV3(msg, 'update-githubOAuth-failed'));
  957. }
  958. });
  959. /**
  960. * @swagger
  961. *
  962. * /_api/v3/security-setting/twitter-oauth:
  963. * put:
  964. * tags: [SecuritySetting, apiv3]
  965. * description: Update twitter OAuth
  966. * requestBody:
  967. * required: true
  968. * content:
  969. * application/json:
  970. * schema:
  971. * $ref: '#/components/schemas/TwitterOAuthSetting'
  972. * responses:
  973. * 200:
  974. * description: Succeeded to update twitter OAuth
  975. * content:
  976. * application/json:
  977. * schema:
  978. * $ref: '#/components/schemas/TwitterOAuthSetting'
  979. */
  980. router.put('/twitter-oauth', loginRequiredStrictly, adminRequired, csrf, validator.twitterOAuth, ApiV3FormValidator, async(req, res) => {
  981. let requestParams = {
  982. 'security:passport-twitter:consumerKey': req.body.twitterConsumerKey,
  983. 'security:passport-twitter:consumerSecret': req.body.twitterConsumerSecret,
  984. 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  985. };
  986. requestParams = removeNullPropertyFromObject(requestParams);
  987. try {
  988. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  989. await crowi.passportService.setupStrategyById('twitter');
  990. const securitySettingParams = {
  991. twitterConsumerId: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
  992. twitterConsumerSecret: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
  993. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser'),
  994. };
  995. return res.apiv3({ securitySettingParams });
  996. }
  997. catch (err) {
  998. const msg = 'Error occurred in updating twitterOAuth';
  999. logger.error('Error', err);
  1000. return res.apiv3Err(new ErrorV3(msg, 'update-twitterOAuth-failed'));
  1001. }
  1002. });
  1003. return router;
  1004. };