authentication.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import Fiber from 'fibers';
  2. Meteor.startup(() => {
  3. // Node Fibers 100% CPU usage issue
  4. // https://github.com/wekan/wekan-mongodb/issues/2#issuecomment-381453161
  5. // https://github.com/meteor/meteor/issues/9796#issuecomment-381676326
  6. // https://github.com/sandstorm-io/sandstorm/blob/0f1fec013fe7208ed0fd97eb88b31b77e3c61f42/shell/server/00-startup.js#L99-L129
  7. Fiber.poolSize = 1e9;
  8. Accounts.validateLoginAttempt(function (options) {
  9. const user = options.user || {};
  10. return !user.loginDisabled;
  11. });
  12. Authentication = {};
  13. Authentication.checkUserId = function (userId) {
  14. if (userId === undefined) {
  15. const error = new Meteor.Error('Unauthorized', 'Unauthorized');
  16. error.statusCode = 401;
  17. throw error;
  18. }
  19. const admin = Users.findOne({ _id: userId, isAdmin: true });
  20. if (admin === undefined) {
  21. const error = new Meteor.Error('Forbidden', 'Forbidden');
  22. error.statusCode = 403;
  23. throw error;
  24. }
  25. };
  26. // This will only check if the user is logged in.
  27. // The authorization checks for the user will have to be done inside each API endpoint
  28. Authentication.checkLoggedIn = function(userId) {
  29. if(userId === undefined) {
  30. const error = new Meteor.Error('Unauthorized', 'Unauthorized');
  31. error.statusCode = 401;
  32. throw error;
  33. }
  34. };
  35. // An admin should be authorized to access everything, so we use a separate check for admins
  36. // This throws an error if otherReq is false and the user is not an admin
  37. Authentication.checkAdminOrCondition = function(userId, otherReq) {
  38. if(otherReq) return;
  39. const admin = Users.findOne({ _id: userId, isAdmin: true });
  40. if (admin === undefined) {
  41. const error = new Meteor.Error('Forbidden', 'Forbidden');
  42. error.statusCode = 403;
  43. throw error;
  44. }
  45. };
  46. // Helper function. Will throw an error if the user does not have read only access to the given board
  47. Authentication.checkBoardAccess = function(userId, boardId) {
  48. Authentication.checkLoggedIn(userId);
  49. const board = Boards.findOne({ _id: boardId });
  50. const normalAccess = board.permission === 'public' || board.members.some((e) => e.userId === userId);
  51. Authentication.checkAdminOrCondition(userId, normalAccess);
  52. };
  53. if (Meteor.isServer) {
  54. if(process.env.OAUTH2_CLIENT_ID !== '') {
  55. ServiceConfiguration.configurations.upsert( // eslint-disable-line no-undef
  56. { service: 'oidc' },
  57. {
  58. $set: {
  59. loginStyle: 'redirect',
  60. clientId: process.env.OAUTH2_CLIENT_ID,
  61. secret: process.env.OAUTH2_SECRET,
  62. serverUrl: process.env.OAUTH2_SERVER_URL,
  63. authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
  64. userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
  65. tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
  66. idTokenWhitelistFields: process.env.OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  67. requestPermissions: process.env.OAUTH2_REQUEST_PERMISSIONS || ['openid', 'profile', 'email'],
  68. },
  69. }
  70. );
  71. }
  72. }
  73. });