authentication.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 =
  51. board.permission === 'public' ||
  52. board.members.some(e => e.userId === userId);
  53. Authentication.checkAdminOrCondition(userId, normalAccess);
  54. };
  55. if (Meteor.isServer) {
  56. if (process.env.OAUTH2_CLIENT_ID !== '') {
  57. ServiceConfiguration.configurations.upsert(
  58. // eslint-disable-line no-undef
  59. { service: 'oidc' },
  60. {
  61. $set: {
  62. loginStyle: process.env.OAUTH2_LOGIN_STYLE,
  63. clientId: process.env.OAUTH2_CLIENT_ID,
  64. secret: process.env.OAUTH2_SECRET,
  65. serverUrl: process.env.OAUTH2_SERVER_URL,
  66. authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
  67. userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
  68. tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
  69. idTokenWhitelistFields:
  70. process.env.OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  71. requestPermissions: process.env.OAUTH2_REQUEST_PERMISSIONS,
  72. },
  73. // OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  74. // OAUTH2_REQUEST_PERMISSIONS || 'openid profile email',
  75. },
  76. );
  77. }
  78. }
  79. });