authentication.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. ServiceConfiguration.configurations.upsert(
  55. { service: 'oidc' },
  56. {
  57. $set: {
  58. loginStyle: 'redirect',
  59. clientId: 'CLIENT_ID',
  60. secret: 'SECRET',
  61. serverUrl: 'https://my-server',
  62. authorizationEndpoint: '/oauth/authorize',
  63. userinfoEndpoint: '/oauth/userinfo',
  64. tokenEndpoint: '/oauth/token',
  65. idTokenWhitelistFields: [],
  66. requestPermissions: ['openid']
  67. }
  68. }
  69. );
  70. }
  71. });