2
0

authentication.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 && e.isActive);
  53. Authentication.checkAdminOrCondition(userId, normalAccess);
  54. };
  55. if (Meteor.isServer) {
  56. if (
  57. process.env.OAUTH2_ENABLED === 'true' ||
  58. process.env.OAUTH2_ENABLED === true
  59. ) {
  60. ServiceConfiguration.configurations.upsert(
  61. // eslint-disable-line no-undef
  62. { service: 'oidc' },
  63. {
  64. $set: {
  65. loginStyle: process.env.OAUTH2_LOGIN_STYLE,
  66. clientId: process.env.OAUTH2_CLIENT_ID,
  67. secret: process.env.OAUTH2_SECRET,
  68. serverUrl: process.env.OAUTH2_SERVER_URL,
  69. authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
  70. userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
  71. tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
  72. idTokenWhitelistFields:
  73. process.env.OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  74. requestPermissions: process.env.OAUTH2_REQUEST_PERMISSIONS,
  75. },
  76. // OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  77. // OAUTH2_REQUEST_PERMISSIONS || 'openid profile email',
  78. },
  79. );
  80. }
  81. } else if (
  82. process.env.CAS_ENABLED === 'true' ||
  83. process.env.CAS_ENABLED === true
  84. ) {
  85. ServiceConfiguration.configurations.upsert(
  86. // eslint-disable-line no-undef
  87. { service: 'cas' },
  88. {
  89. $set: {
  90. baseUrl: process.env.CAS_BASE_URL,
  91. loginUrl: process.env.CAS_LOGIN_URL,
  92. serviceParam: 'service',
  93. popupWidth: 810,
  94. popupHeight: 610,
  95. popup: true,
  96. autoClose: true,
  97. validateUrl: process.env.CASE_VALIDATE_URL,
  98. casVersion: 3.0,
  99. attributes: {
  100. debug: process.env.DEBUG,
  101. },
  102. },
  103. },
  104. );
  105. } else if (
  106. process.env.SAML_ENABLED === 'true' ||
  107. process.env.SAML_ENABLED === true
  108. ) {
  109. ServiceConfiguration.configurations.upsert(
  110. // eslint-disable-line no-undef
  111. { service: 'saml' },
  112. {
  113. $set: {
  114. provider: process.env.SAML_PROVIDER,
  115. entryPoint: process.env.SAML_ENTRYPOINT,
  116. issuer: process.env.SAML_ISSUER,
  117. cert: process.env.SAML_CERT,
  118. idpSLORedirectURL: process.env.SAML_IDPSLO_REDIRECTURL,
  119. privateKeyFile: process.env.SAML_PRIVATE_KEYFILE,
  120. publicCertFile: process.env.SAML_PUBLIC_CERTFILE,
  121. identifierFormat: process.env.SAML_IDENTIFIER_FORMAT,
  122. localProfileMatchAttribute:
  123. process.env.SAML_LOCAL_PROFILE_MATCH_ATTRIBUTE,
  124. attributesSAML: process.env.SAML_ATTRIBUTES || [
  125. 'sn',
  126. 'givenName',
  127. 'mail',
  128. ],
  129. /*
  130. settings = {"saml":[{
  131. "provider":"openam",
  132. "entryPoint":"https://openam.idp.io/openam/SSORedirect/metaAlias/zimt/idp",
  133. "issuer": "https://sp.zimt.io/", //replace with url of your app
  134. "cert":"MIICizCCAfQCCQCY8tKaMc0 LOTS OF FUNNY CHARS ==",
  135. "idpSLORedirectURL": "http://openam.idp.io/openam/IDPSloRedirect/metaAlias/zimt/idp",
  136. "privateKeyFile": "certs/mykey.pem", // path is relative to $METEOR-PROJECT/private
  137. "publicCertFile": "certs/mycert.pem", // eg $METEOR-PROJECT/private/certs/mycert.pem
  138. "dynamicProfile": true // set to true if we want to create a user in Meteor.users dynamically if SAML assertion is valid
  139. "identifierFormat": "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified", // Defaults to urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress
  140. "localProfileMatchAttribute": "telephoneNumber" // CAUTION: this will be mapped to profile.<localProfileMatchAttribute> attribute in Mongo if identifierFormat (see above) differs from urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress,
  141. "attributesSAML": [telephoneNumber, sn, givenName, mail], // attrs from SAML attr statement, which will be used for local Meteor profile creation. Currently no real attribute mapping. If required use mapping on IdP side.
  142. }]}
  143. */
  144. },
  145. },
  146. );
  147. }
  148. });