security-setting.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* eslint-disable max-len */
  2. /* eslint-disable no-unused-vars */
  3. const loggerFactory = require('@alias/logger');
  4. const logger = loggerFactory('growi:routes:apiv3:security-setting');
  5. const express = require('express');
  6. const router = express.Router();
  7. const { body } = require('express-validator/check');
  8. const ErrorV3 = require('../../models/vo/error-apiv3');
  9. const validator = {
  10. // TODO correct validator
  11. generalSetting: [
  12. body('restrictGuestMode').isString(),
  13. body('pageCompleteDeletionAuthority').isString(),
  14. body('hideRestrictedByOwner').isBoolean(),
  15. body('hideRestrictedByGroup').isBoolean(),
  16. ],
  17. googleOAuth: [
  18. body('googleClientId').isString(),
  19. body('googleClientSecret').isString(),
  20. body('isSameUsernameTreatedAsIdenticalUser').isBoolean(),
  21. ],
  22. githubOAuth: [
  23. body('githubClientId').isString(),
  24. body('githubClientSecret').isString(),
  25. body('isSameUsernameTreatedAsIdenticalUser').isBoolean(),
  26. ],
  27. twitterOAuth: [
  28. body('twitterConsumerKey').isString(),
  29. body('twitterConsumerSecret').isString(),
  30. body('isSameUsernameTreatedAsIdenticalUser').isBoolean(),
  31. ],
  32. };
  33. /**
  34. * @swagger
  35. * tags:
  36. * name: SecuritySetting
  37. */
  38. /**
  39. * @swagger
  40. *
  41. * components:
  42. * schemas:
  43. * SecurityParams:
  44. * type: object
  45. * GeneralSetting:
  46. * type:object
  47. * GuestModeParams:
  48. * type: object
  49. * properties:
  50. * restrictGuestMode:
  51. * type: string
  52. * description: type of restrictGuestMode
  53. * PageDeletionParams:
  54. * type: object
  55. * properties:
  56. * pageCompleteDeletionAuthority:
  57. * type: string
  58. * description: type of pageDeletionAuthority
  59. * Function:
  60. * type: object
  61. * properties:
  62. * hideRestrictedByOwner:
  63. * type: boolean
  64. * description: enable hide by owner
  65. * hideRestrictedByGroup:
  66. * type: boolean
  67. * description: enable hide by group
  68. * GitHubOAuthSetting:
  69. * type:object
  70. * githubClientId:
  71. * type: string
  72. * description: key of comsumer
  73. * githubClientSecret:
  74. * type: string
  75. * description: password of comsumer
  76. * isSameUsernameTreatedAsIdenticalUser
  77. * type: boolean
  78. * description: local account automatically linked the email matched
  79. * GoogleOAuthSetting:
  80. * type:object
  81. * googleClientId:
  82. * type: string
  83. * description: key of comsumer
  84. * googleClientSecret:
  85. * type: string
  86. * description: password of comsumer
  87. * isSameUsernameTreatedAsIdenticalUser
  88. * type: boolean
  89. * description: local account automatically linked the email matched
  90. * TwitterOAuthSetting:
  91. * type:object
  92. * twitterConsumerKey:
  93. * type: string
  94. * description: key of comsumer
  95. * twitterConsumerSecret:
  96. * type: string
  97. * description: password of comsumer
  98. * isSameUsernameTreatedAsIdenticalUser
  99. * type: boolean
  100. * description: local account automatically linked the email matched
  101. */
  102. module.exports = (crowi) => {
  103. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  104. const adminRequired = require('../../middleware/admin-required')(crowi);
  105. const csrf = require('../../middleware/csrf')(crowi);
  106. const { ApiV3FormValidator } = crowi.middlewares;
  107. /**
  108. * @swagger
  109. *
  110. * /security-setting/:
  111. * get:
  112. * tags: [SecuritySetting]
  113. * description: Get security paramators
  114. * responses:
  115. * 200:
  116. * description: params of security
  117. * content:
  118. * application/json:
  119. * schema:
  120. * properties:
  121. * securityParams:
  122. * $ref: '#/components/schemas/SecurityParams'
  123. */
  124. router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
  125. const securityParams = {
  126. generalAuth: {
  127. isGoogleOAuthEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-google:isEnabled'),
  128. isGithubOAuthEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-github:isEnabled'),
  129. isTwitterOAuthEnabled: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isEnabled'),
  130. },
  131. googleOAuth: {
  132. googleClientId: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientId'),
  133. googleClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientSecret'),
  134. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-google:isSameUsernameTreatedAsIdenticalUser'),
  135. },
  136. githubOAuth: {
  137. githubClientId: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientId'),
  138. githubClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientSecret'),
  139. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-github:isSameUsernameTreatedAsIdenticalUser'),
  140. },
  141. twitterOAuth: {
  142. twitterConsumerKey: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
  143. twitterConsumerSecret: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
  144. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser'),
  145. },
  146. };
  147. return res.apiv3({ securityParams });
  148. });
  149. /**
  150. * @swagger
  151. *
  152. * /security-setting/general-setting:
  153. * put:
  154. * tags: [SecuritySetting]
  155. * description: Update GeneralSetting
  156. * requestBody:
  157. * required: true
  158. * content:
  159. * application/json:
  160. * schema:
  161. * type: object
  162. * properties:
  163. * restrictGuestMode:
  164. * description: type of restrictGuestMode
  165. * type: string
  166. * pageCompleteDeletionAuthority:
  167. * type: string
  168. * description: type of pageDeletionAuthority
  169. * hideRestrictedByOwner:
  170. * type: boolean
  171. * description: enable hide by owner
  172. * hideRestrictedByGroup:
  173. * type: boolean
  174. * description: enable hide by group
  175. * responses:
  176. * 200:
  177. * description: Succeeded to update general Setting
  178. * content:
  179. * application/json:
  180. * schema:
  181. * properties:
  182. * status:
  183. * $ref: '#/components/schemas/SecurityParams/GeneralSetting'
  184. */
  185. router.put('/general-setting', loginRequiredStrictly, adminRequired, csrf, validator.generalSetting, ApiV3FormValidator, async(req, res) => {
  186. const requestParams = {
  187. 'security:restrictGuestMode': req.body.restrictGuestMode,
  188. 'security:pageCompleteDeletionAuthority': req.body.pageCompleteDeletionAuthority,
  189. 'security:list-policy:hideRestrictedByOwner': req.body.hideRestrictedByOwner,
  190. 'security:list-policy:hideRestrictedByGroup': req.body.hideRestrictedByGroup,
  191. };
  192. try {
  193. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  194. const securitySettingParams = {
  195. restrictGuestMode: await crowi.configManager.getConfig('crowi', 'security:restrictGuestMode'),
  196. pageCompleteDeletionAuthority: await crowi.configManager.getConfig('crowi', 'security:pageCompleteDeletionAuthority'),
  197. hideRestrictedByOwner: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByOwner'),
  198. hideRestrictedByGroup: await crowi.configManager.getConfig('crowi', 'security:list-policy:hideRestrictedByGroup'),
  199. };
  200. return res.apiv3({ securitySettingParams });
  201. }
  202. catch (err) {
  203. const msg = 'Error occurred in updating security setting';
  204. logger.error('Error', err);
  205. return res.apiv3Err(new ErrorV3(msg, 'update-secuirty-setting failed'));
  206. }
  207. });
  208. /**
  209. * @swagger
  210. *
  211. * /security-setting/google-oauth:
  212. * put:
  213. * tags: [SecuritySetting]
  214. * description: Update google OAuth
  215. * requestBody:
  216. * required: true
  217. * content:
  218. * application/json:
  219. * schema:
  220. * $ref: '#/components/schemas/SecurityParams/GoogleOAuthSetting'
  221. * responses:
  222. * 200:
  223. * description: Succeeded to google OAuth
  224. * content:
  225. * application/json:
  226. * schema:
  227. * $ref: '#/components/schemas/SecurityParams/GoogleOAuthSetting'
  228. */
  229. router.put('/google-oauth', loginRequiredStrictly, adminRequired, csrf, validator.googleOAuth, ApiV3FormValidator, async(req, res) => {
  230. const requestParams = {
  231. 'security:passport-google:clientId': req.body.googleClientId,
  232. 'security:passport-google:clientSecret': req.body.googleClientSecret,
  233. 'security:passport-google:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  234. };
  235. try {
  236. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  237. const securitySettingParams = {
  238. googleClientId: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientId'),
  239. googleClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-google:clientSecret'),
  240. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-google:isSameUsernameTreatedAsIdenticalUser'),
  241. };
  242. return res.apiv3({ securitySettingParams });
  243. }
  244. catch (err) {
  245. const msg = 'Error occurred in updating googleOAuth';
  246. logger.error('Error', err);
  247. return res.apiv3Err(new ErrorV3(msg, 'update-googleOAuth-failed'));
  248. }
  249. });
  250. /**
  251. * @swagger
  252. *
  253. * /security-setting/github-oauth:
  254. * put:
  255. * tags: [SecuritySetting]
  256. * description: Update github OAuth
  257. * requestBody:
  258. * required: true
  259. * content:
  260. * application/json:
  261. * schema:
  262. * $ref: '#/components/schemas/SecurityParams/GitHubOAuthSetting'
  263. * responses:
  264. * 200:
  265. * description: Succeeded to github OAuth
  266. * content:
  267. * application/json:
  268. * schema:
  269. * $ref: '#/components/schemas/SecurityParams/GitHubOAuthSetting'
  270. */
  271. router.put('/github-oauth', loginRequiredStrictly, adminRequired, csrf, validator.githubOAuth, ApiV3FormValidator, async(req, res) => {
  272. const requestParams = {
  273. 'security:passport-github:clientId': req.body.githubClientId,
  274. 'security:passport-github:clientSecret': req.body.githubClientSecret,
  275. 'security:passport-github:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  276. };
  277. try {
  278. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  279. const securitySettingParams = {
  280. githubClientId: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientId'),
  281. githubClientSecret: await crowi.configManager.getConfig('crowi', 'security:passport-github:clientSecret'),
  282. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-github:isSameUsernameTreatedAsIdenticalUser'),
  283. };
  284. return res.apiv3({ securitySettingParams });
  285. }
  286. catch (err) {
  287. const msg = 'Error occurred in updating githubOAuth';
  288. logger.error('Error', err);
  289. return res.apiv3Err(new ErrorV3(msg, 'update-githubOAuth-failed'));
  290. }
  291. });
  292. /**
  293. * @swagger
  294. *
  295. * /security-setting/twitter-oauth:
  296. * put:
  297. * tags: [SecuritySetting]
  298. * description: Update twitter OAuth
  299. * requestBody:
  300. * required: true
  301. * content:
  302. * application/json:
  303. * schema:
  304. * $ref: '#/components/schemas/SecurityParams/TwitterOAuthSetting'
  305. * responses:
  306. * 200:
  307. * description: Succeeded to update twitter OAuth
  308. * content:
  309. * application/json:
  310. * schema:
  311. * $ref: '#/components/schemas/SecurityParams/TwitterOAuthSetting'
  312. */
  313. router.put('/twitter-oauth', loginRequiredStrictly, adminRequired, csrf, validator.twitterOAuth, ApiV3FormValidator, async(req, res) => {
  314. const requestParams = {
  315. 'security:passport-twitter:consumerKey': req.body.twitterConsumerKey,
  316. 'security:passport-twitter:consumerSecret': req.body.twitterConsumerSecret,
  317. 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser': req.body.isSameUsernameTreatedAsIdenticalUser,
  318. };
  319. try {
  320. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  321. const securitySettingParams = {
  322. twitterConsumerId: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerKey'),
  323. twitterConsumerSecret: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:consumerSecret'),
  324. isSameUsernameTreatedAsIdenticalUser: await crowi.configManager.getConfig('crowi', 'security:passport-twitter:isSameUsernameTreatedAsIdenticalUser'),
  325. };
  326. return res.apiv3({ securitySettingParams });
  327. }
  328. catch (err) {
  329. const msg = 'Error occurred in updating twitterOAuth';
  330. logger.error('Error', err);
  331. return res.apiv3Err(new ErrorV3(msg, 'update-twitterOAuth-failed'));
  332. }
  333. });
  334. return router;
  335. };