security-setting.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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('../../middlewares/login-required')(crowi);
  283. const adminRequired = require('../../middlewares/admin-required')(crowi);
  284. const csrf = require('../../middlewares/csrf')(crowi);
  285. const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
  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, 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 paginateResult = await ShareLink.paginate(
  549. linkQuery,
  550. {
  551. page,
  552. limit,
  553. populate: {
  554. path: 'relatedPage',
  555. select: 'path',
  556. },
  557. },
  558. );
  559. return res.apiv3({ paginateResult });
  560. }
  561. catch (err) {
  562. const msg = 'Error occured in get share link';
  563. logger.error('Error', err);
  564. return res.apiv3Err(new ErrorV3(msg, 'get-all-share-links-failed'));
  565. }
  566. });
  567. /**
  568. * @swagger
  569. *
  570. * /_api/v3/security-setting/all-share-links:
  571. * delete:
  572. * tags: [ShareLinkSettings, apiv3]
  573. * description: Delete All ShareLinks at Share Link Setting
  574. * responses:
  575. * 200:
  576. * description: succeed to delete all share links
  577. */
  578. router.delete('/all-share-links/', loginRequiredStrictly, adminRequired, async(req, res) => {
  579. const ShareLink = crowi.model('ShareLink');
  580. try {
  581. const removedAct = await ShareLink.remove({});
  582. const removeTotal = await removedAct.n;
  583. return res.apiv3({ removeTotal });
  584. }
  585. catch (err) {
  586. const msg = 'Error occured in delete all share links';
  587. logger.error('Error', err);
  588. return res.apiv3Err(new ErrorV3(msg, 'failed-to-delete-all-share-links'));
  589. }
  590. });
  591. /**
  592. * @swagger
  593. *
  594. * /_api/v3/security-setting/local-setting:
  595. * put:
  596. * tags: [LocalSetting, apiv3]
  597. * description: Update LocalSetting
  598. * requestBody:
  599. * required: true
  600. * content:
  601. * application/json:
  602. * schema:
  603. * $ref: '#/components/schemas/LocalSetting'
  604. * responses:
  605. * 200:
  606. * description: Succeeded to update local Setting
  607. * content:
  608. * application/json:
  609. * schema:
  610. * $ref: '#/components/schemas/LocalSetting'
  611. */
  612. router.put('/local-setting', loginRequiredStrictly, adminRequired, csrf, validator.localSetting, apiV3FormValidator, async(req, res) => {
  613. const requestParams = {
  614. 'security:registrationMode': req.body.registrationMode,
  615. 'security:registrationWhiteList': req.body.registrationWhiteList,
  616. };
  617. try {
  618. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  619. await crowi.passportService.setupStrategyById('local');
  620. const localSettingParams = {
  621. registrationMode: await crowi.configManager.getConfig('crowi', 'security:registrationMode'),
  622. registrationWhiteList: await crowi.configManager.getConfig('crowi', 'security:registrationWhiteList'),
  623. };
  624. return res.apiv3({ localSettingParams });
  625. }
  626. catch (err) {
  627. const msg = 'Error occurred in updating local setting';
  628. logger.error('Error', err);
  629. return res.apiv3Err(new ErrorV3(msg, 'update-local-setting failed'));
  630. }
  631. });
  632. /**
  633. * @swagger
  634. *
  635. * /_api/v3/security-setting/ldap:
  636. * put:
  637. * tags: [SecuritySetting, apiv3]
  638. * description: Update LDAP setting
  639. * requestBody:
  640. * required: true
  641. * content:
  642. * application/json:
  643. * schema:
  644. * $ref: '#/components/schemas/LdapAuthSetting'
  645. * responses:
  646. * 200:
  647. * description: Succeeded to update LDAP setting
  648. * content:
  649. * application/json:
  650. * schema:
  651. * $ref: '#/components/schemas/LdapAuthSetting'
  652. */
  653. router.put('/ldap', loginRequiredStrictly, adminRequired, csrf, validator.ldapAuth, apiV3FormValidator, async(req, res) => {
  654. const requestParams = {
  655. 'security:passport-ldap:serverUrl': req.body.serverUrl,
  656. 'security:passport-ldap:isUserBind': req.body.isUserBind,
  657. 'security:passport-ldap:bindDN': req.body.ldapBindDN,
  658. 'security:passport-ldap:bindDNPassword': req.body.ldapBindDNPassword,
  659. 'security:passport-ldap:searchFilter': req.body.ldapSearchFilter,
  660. 'security:passport-ldap:attrMapUsername': req.body.ldapAttrMapUsername,
  661. 'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  662. 'security:passport-ldap:attrMapMail': req.body.ldapAttrMapMail,
  663. 'security:passport-ldap:attrMapName': req.body.ldapAttrMapName,
  664. 'security:passport-ldap:groupSearchBase': req.body.ldapGroupSearchBase,
  665. 'security:passport-ldap:groupSearchFilter': req.body.ldapGroupSearchFilter,
  666. 'security:passport-ldap:groupDnProperty': req.body.ldapGroupDnProperty,
  667. };
  668. try {
  669. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  670. await crowi.passportService.setupStrategyById('ldap');
  671. const securitySettingParams = {
  672. serverUrl: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:serverUrl'),
  673. isUserBind: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:isUserBind'),
  674. ldapBindDN: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:bindDN'),
  675. ldapBindDNPassword: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:bindDNPassword'),
  676. ldapSearchFilter: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:searchFilter'),
  677. ldapAttrMapUsername: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapUsername'),
  678. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:isSameUsernameTreatedAsIdenticalUser'),
  679. ldapAttrMapMail: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapMail'),
  680. ldapAttrMapName: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:attrMapName'),
  681. ldapGroupSearchBase: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupSearchBase'),
  682. ldapGroupSearchFilter: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupSearchFilter'),
  683. ldapGroupDnProperty: await crowi.configManager.getConfig('crowi', 'security:passport-ldap:groupDnProperty'),
  684. };
  685. return res.apiv3({ securitySettingParams });
  686. }
  687. catch (err) {
  688. const msg = 'Error occurred in updating SAML setting';
  689. logger.error('Error', err);
  690. return res.apiv3Err(new ErrorV3(msg, 'update-SAML-failed'));
  691. }
  692. });
  693. /**
  694. * @swagger
  695. *
  696. * /_api/v3/security-setting/saml:
  697. * put:
  698. * tags: [SecuritySetting, apiv3]
  699. * description: Update SAML setting
  700. * requestBody:
  701. * required: true
  702. * content:
  703. * application/json:
  704. * schema:
  705. * $ref: '#/components/schemas/SamlAuthSetting'
  706. * responses:
  707. * 200:
  708. * description: Succeeded to update SAML setting
  709. * content:
  710. * application/json:
  711. * schema:
  712. * $ref: '#/components/schemas/SamlAuthSetting'
  713. */
  714. router.put('/saml', loginRequiredStrictly, adminRequired, csrf, validator.samlAuth, apiV3FormValidator, async(req, res) => {
  715. // For the value of each mandatory items,
  716. // check whether it from the environment variables is empty and form value to update it is empty
  717. // validate the syntax of a attribute - based login control rule
  718. const invalidValues = [];
  719. for (const configKey of crowi.passportService.mandatoryConfigKeysForSaml) {
  720. const key = configKey.replace('security:passport-saml:', '');
  721. const formValue = req.body[key];
  722. if (crowi.configManager.getConfigFromEnvVars('crowi', configKey) === null && formValue == null) {
  723. const formItemName = req.t(`security_setting.form_item_name.${key}`);
  724. invalidValues.push(req.t('form_validation.required', formItemName));
  725. }
  726. }
  727. if (invalidValues.length !== 0) {
  728. return res.apiv3Err(req.t('form_validation.error_message'), 400, invalidValues);
  729. }
  730. const rule = req.body.ABLCRule;
  731. // Empty string disables attribute-based login control.
  732. // So, when rule is empty string, validation is passed.
  733. if (rule != null) {
  734. try {
  735. crowi.passportService.parseABLCRule(rule);
  736. }
  737. catch (err) {
  738. return res.apiv3Err(req.t('form_validation.invalid_syntax', req.t('security_setting.form_item_name.ABLCRule')), 400);
  739. }
  740. }
  741. const requestParams = {
  742. 'security:passport-saml:entryPoint': req.body.entryPoint,
  743. 'security:passport-saml:issuer': req.body.issuer,
  744. 'security:passport-saml:cert': req.body.cert,
  745. 'security:passport-saml:attrMapId': req.body.attrMapId,
  746. 'security:passport-saml:attrMapUsername': req.body.attrMapUsername,
  747. 'security:passport-saml:attrMapMail': req.body.attrMapMail,
  748. 'security:passport-saml:attrMapFirstName': req.body.attrMapFirstName,
  749. 'security:passport-saml:attrMapLastName': req.body.attrMapLastName,
  750. 'security:passport-saml:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  751. 'security:passport-saml:isSameEmailTreatedAsIdenticalUser': req.body.isSameEmailTreatedAsIdenticalUser,
  752. 'security:passport-saml:ABLCRule': req.body.ABLCRule,
  753. };
  754. try {
  755. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  756. await crowi.passportService.setupStrategyById('saml');
  757. const securitySettingParams = {
  758. missingMandatoryConfigKeys: await crowi.passportService.getSamlMissingMandatoryConfigKeys(),
  759. samlEntryPoint: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:entryPoint'),
  760. samlIssuer: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:issuer'),
  761. samlCert: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:cert'),
  762. samlAttrMapId: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapId'),
  763. samlAttrMapUsername: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapUsername'),
  764. samlAttrMapMail: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapMail'),
  765. samlAttrMapFirstName: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapFirstName'),
  766. samlAttrMapLastName: await crowi.configManager.getConfigFromDB('crowi', 'security:passport-saml:attrMapLastName'),
  767. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-saml:isSameUsernameTreatedAsIdenticalUser'),
  768. isSameEmailTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-saml:isSameEmailTreatedAsIdenticalUser'),
  769. samlABLCRule: await crowi.configManager.getConfig('crowi', 'security:passport-saml:ABLCRule'),
  770. };
  771. return res.apiv3({ securitySettingParams });
  772. }
  773. catch (err) {
  774. const msg = 'Error occurred in updating SAML setting';
  775. logger.error('Error', err);
  776. return res.apiv3Err(new ErrorV3(msg, 'update-SAML-failed'));
  777. }
  778. });
  779. /**
  780. * @swagger
  781. *
  782. * /_api/v3/security-setting/oidc:
  783. * put:
  784. * tags: [SecuritySetting, apiv3]
  785. * description: Update OpenID Connect setting
  786. * requestBody:
  787. * required: true
  788. * content:
  789. * application/json:
  790. * schema:
  791. * $ref: '#/components/schemas/OidcAuthSetting'
  792. * responses:
  793. * 200:
  794. * description: Succeeded to update OpenID Connect setting
  795. * content:
  796. * application/json:
  797. * schema:
  798. * $ref: '#/components/schemas/OidcAuthSetting'
  799. */
  800. router.put('/oidc', loginRequiredStrictly, adminRequired, csrf, validator.oidcAuth, apiV3FormValidator, async(req, res) => {
  801. const requestParams = {
  802. 'security:passport-oidc:providerName': req.body.oidcProviderName,
  803. 'security:passport-oidc:issuerHost': req.body.oidcIssuerHost,
  804. 'security:passport-oidc:clientId': req.body.oidcClientId,
  805. 'security:passport-oidc:clientSecret': req.body.oidcClientSecret,
  806. 'security:passport-oidc:attrMapId': req.body.oidcAttrMapId,
  807. 'security:passport-oidc:attrMapUserName': req.body.oidcAttrMapUserName,
  808. 'security:passport-oidc:attrMapName': req.body.oidcAttrMapName,
  809. 'security:passport-oidc:attrMapMail': req.body.oidcAttrMapEmail,
  810. 'security:passport-oidc:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  811. 'security:passport-oidc:isSameEmailTreatedAsIdenticalUser': req.body.isSameEmailTreatedAsIdenticalUser,
  812. };
  813. try {
  814. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  815. await crowi.passportService.setupStrategyById('oidc');
  816. const securitySettingParams = {
  817. oidcProviderName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:providerName'),
  818. oidcIssuerHost: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:issuerHost'),
  819. oidcClientId: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:clientId'),
  820. oidcClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:clientSecret'),
  821. oidcAttrMapId: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapId'),
  822. oidcAttrMapUserName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapUserName'),
  823. oidcAttrMapName: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapName'),
  824. oidcAttrMapEmail: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:attrMapMail'),
  825. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:isSameUsernameTreatedAsIdenticalUser'),
  826. isSameEmailTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-oidc:isSameEmailTreatedAsIdenticalUser'),
  827. };
  828. return res.apiv3({ securitySettingParams });
  829. }
  830. catch (err) {
  831. const msg = 'Error occurred in updating OpenIDConnect';
  832. logger.error('Error', err);
  833. return res.apiv3Err(new ErrorV3(msg, 'update-OpenIDConnect-failed'));
  834. }
  835. });
  836. /**
  837. * @swagger
  838. *
  839. * /_api/v3/security-setting/basic:
  840. * put:
  841. * tags: [SecuritySetting, apiv3]
  842. * description: Update basic
  843. * requestBody:
  844. * required: true
  845. * content:
  846. * application/json:
  847. * schema:
  848. * $ref: '#/components/schemas/BasicAuthSetting'
  849. * responses:
  850. * 200:
  851. * description: Succeeded to update basic
  852. * content:
  853. * application/json:
  854. * schema:
  855. * $ref: '#/components/schemas/BasicAuthSetting'
  856. */
  857. router.put('/basic', loginRequiredStrictly, adminRequired, csrf, validator.basicAuth, apiV3FormValidator, async(req, res) => {
  858. const requestParams = {
  859. 'security:passport-basic:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  860. };
  861. try {
  862. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  863. await crowi.passportService.setupStrategyById('basic');
  864. const securitySettingParams = {
  865. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-basic:isSameUsernameTreatedAsIdenticalUser'),
  866. };
  867. return res.apiv3({ securitySettingParams });
  868. }
  869. catch (err) {
  870. const msg = 'Error occurred in updating basicAuth';
  871. logger.error('Error', err);
  872. return res.apiv3Err(new ErrorV3(msg, 'update-basicOAuth-failed'));
  873. }
  874. });
  875. /**
  876. * @swagger
  877. *
  878. * /_api/v3/security-setting/google-oauth:
  879. * put:
  880. * tags: [SecuritySetting, apiv3]
  881. * description: Update google OAuth
  882. * requestBody:
  883. * required: true
  884. * content:
  885. * application/json:
  886. * schema:
  887. * $ref: '#/components/schemas/GoogleOAuthSetting'
  888. * responses:
  889. * 200:
  890. * description: Succeeded to google OAuth
  891. * content:
  892. * application/json:
  893. * schema:
  894. * $ref: '#/components/schemas/GoogleOAuthSetting'
  895. */
  896. router.put('/google-oauth', loginRequiredStrictly, adminRequired, csrf, validator.googleOAuth, apiV3FormValidator, async(req, res) => {
  897. const requestParams = {
  898. 'security:passport-google:clientId': req.body.googleClientId,
  899. 'security:passport-google:clientSecret': req.body.googleClientSecret,
  900. 'security:passport-google:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  901. };
  902. try {
  903. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  904. await crowi.passportService.setupStrategyById('google');
  905. const securitySettingParams = {
  906. googleClientId: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientId'),
  907. googleClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientSecret'),
  908. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-google:isSameUsernameTreatedAsIdenticalUser'),
  909. };
  910. return res.apiv3({ securitySettingParams });
  911. }
  912. catch (err) {
  913. const msg = 'Error occurred in updating googleOAuth';
  914. logger.error('Error', err);
  915. return res.apiv3Err(new ErrorV3(msg, 'update-googleOAuth-failed'));
  916. }
  917. });
  918. /**
  919. * @swagger
  920. *
  921. * /_api/v3/security-setting/github-oauth:
  922. * put:
  923. * tags: [SecuritySetting, apiv3]
  924. * description: Update github OAuth
  925. * requestBody:
  926. * required: true
  927. * content:
  928. * application/json:
  929. * schema:
  930. * $ref: '#/components/schemas/GitHubOAuthSetting'
  931. * responses:
  932. * 200:
  933. * description: Succeeded to github OAuth
  934. * content:
  935. * application/json:
  936. * schema:
  937. * $ref: '#/components/schemas/GitHubOAuthSetting'
  938. */
  939. router.put('/github-oauth', loginRequiredStrictly, adminRequired, csrf, validator.githubOAuth, apiV3FormValidator, async(req, res) => {
  940. const requestParams = {
  941. 'security:passport-github:clientId': req.body.githubClientId,
  942. 'security:passport-github:clientSecret': req.body.githubClientSecret,
  943. 'security:passport-github:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  944. };
  945. try {
  946. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  947. await crowi.passportService.setupStrategyById('github');
  948. const securitySettingParams = {
  949. githubClientId: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientId'),
  950. githubClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientSecret'),
  951. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-github:isSameUsernameTreatedAsIdenticalUser'),
  952. };
  953. return res.apiv3({ securitySettingParams });
  954. }
  955. catch (err) {
  956. // reset strategy
  957. await crowi.passportService.resetGitHubStrategy();
  958. const msg = 'Error occurred in updating githubOAuth';
  959. logger.error('Error', err);
  960. return res.apiv3Err(new ErrorV3(msg, 'update-githubOAuth-failed'));
  961. }
  962. });
  963. /**
  964. * @swagger
  965. *
  966. * /_api/v3/security-setting/twitter-oauth:
  967. * put:
  968. * tags: [SecuritySetting, apiv3]
  969. * description: Update twitter OAuth
  970. * requestBody:
  971. * required: true
  972. * content:
  973. * application/json:
  974. * schema:
  975. * $ref: '#/components/schemas/TwitterOAuthSetting'
  976. * responses:
  977. * 200:
  978. * description: Succeeded to update twitter OAuth
  979. * content:
  980. * application/json:
  981. * schema:
  982. * $ref: '#/components/schemas/TwitterOAuthSetting'
  983. */
  984. router.put('/twitter-oauth', loginRequiredStrictly, adminRequired, csrf, validator.twitterOAuth, apiV3FormValidator, async(req, res) => {
  985. let requestParams = {
  986. 'security:passport-twitter:consumerKey': req.body.twitterConsumerKey,
  987. 'security:passport-twitter:consumerSecret': req.body.twitterConsumerSecret,
  988. 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  989. };
  990. requestParams = removeNullPropertyFromObject(requestParams);
  991. try {
  992. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  993. await crowi.passportService.setupStrategyById('twitter');
  994. const securitySettingParams = {
  995. twitterConsumerId: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
  996. twitterConsumerSecret: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
  997. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser'),
  998. };
  999. return res.apiv3({ securitySettingParams });
  1000. }
  1001. catch (err) {
  1002. const msg = 'Error occurred in updating twitterOAuth';
  1003. logger.error('Error', err);
  1004. return res.apiv3Err(new ErrorV3(msg, 'update-twitterOAuth-failed'));
  1005. }
  1006. });
  1007. return router;
  1008. };