authentication.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // Monkey patch to work around the problem described in
  16. // https://github.com/sandstorm-io/meteor-accounts-sandstorm/pull/31
  17. const _httpMethods = HTTP.methods;
  18. HTTP.methods = (newMethods) => {
  19. Object.keys(newMethods).forEach((key) => {
  20. if (newMethods[key].auth) {
  21. newMethods[key].auth = function() {
  22. const sandstormID = this.req.headers['x-sandstorm-user-id'];
  23. const user = Meteor.users.findOne({'services.sandstorm.id': sandstormID});
  24. if (user) {
  25. userId = user._id;
  26. }
  27. //return user && user._id;
  28. };
  29. }
  30. });
  31. _httpMethods(newMethods);
  32. };
  33. }
  34. if (userId === undefined) {
  35. const error = new Meteor.Error('Unauthorized', 'Unauthorized');
  36. error.statusCode = 401;
  37. throw error;
  38. }
  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. // This will only check if the user is logged in.
  47. // The authorization checks for the user will have to be done inside each API endpoint
  48. Authentication.checkLoggedIn = function(userId) {
  49. if(userId === undefined) {
  50. const error = new Meteor.Error('Unauthorized', 'Unauthorized');
  51. error.statusCode = 401;
  52. throw error;
  53. }
  54. };
  55. // An admin should be authorized to access everything, so we use a separate check for admins
  56. // This throws an error if otherReq is false and the user is not an admin
  57. Authentication.checkAdminOrCondition = function(userId, otherReq) {
  58. if(otherReq) return;
  59. const admin = Users.findOne({ _id: userId, isAdmin: true });
  60. if (admin === undefined) {
  61. const error = new Meteor.Error('Forbidden', 'Forbidden');
  62. error.statusCode = 403;
  63. throw error;
  64. }
  65. };
  66. // Helper function. Will throw an error if the user does not have read only access to the given board
  67. Authentication.checkBoardAccess = function(userId, boardId) {
  68. Authentication.checkLoggedIn(userId);
  69. const board = Boards.findOne({ _id: boardId });
  70. const normalAccess = board.permission === 'public' || board.members.some((e) => e.userId === userId);
  71. Authentication.checkAdminOrCondition(userId, normalAccess);
  72. };
  73. if (Meteor.isServer) {
  74. if(process.env.OAUTH2_CLIENT_ID !== '') {
  75. ServiceConfiguration.configurations.upsert( // eslint-disable-line no-undef
  76. { service: 'oidc' },
  77. {
  78. $set: {
  79. loginStyle: 'redirect',
  80. clientId: process.env.OAUTH2_CLIENT_ID,
  81. secret: process.env.OAUTH2_SECRET,
  82. serverUrl: process.env.OAUTH2_SERVER_URL,
  83. authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
  84. userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
  85. tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
  86. idTokenWhitelistFields: [],
  87. requestPermissions: ['openid'],
  88. },
  89. }
  90. );
  91. }
  92. }
  93. });