authentication.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import Fiber from 'fibers';
  3. Meteor.startup(() => {
  4. // Node Fibers 100% CPU usage issue
  5. // https://github.com/wekan/wekan-mongodb/issues/2#issuecomment-381453161
  6. // https://github.com/meteor/meteor/issues/9796#issuecomment-381676326
  7. // https://github.com/sandstorm-io/sandstorm/blob/0f1fec013fe7208ed0fd97eb88b31b77e3c61f42/shell/server/00-startup.js#L99-L129
  8. Fiber.poolSize = 1e9;
  9. Accounts.validateLoginAttempt(function(options) {
  10. const user = options.user || {};
  11. return !user.loginDisabled;
  12. });
  13. Authentication = {};
  14. Authentication.checkUserId = function(userId) {
  15. if (userId === undefined) {
  16. const error = new Meteor.Error('Unauthorized', 'Unauthorized');
  17. error.statusCode = 401;
  18. throw error;
  19. }
  20. const admin = ReactiveCache.getUser({ _id: userId, isAdmin: true });
  21. if (admin === undefined) {
  22. const error = new Meteor.Error('Forbidden', 'Forbidden');
  23. error.statusCode = 403;
  24. throw error;
  25. }
  26. };
  27. // This will only check if the user is logged in.
  28. // The authorization checks for the user will have to be done inside each API endpoint
  29. Authentication.checkLoggedIn = function(userId) {
  30. if (userId === undefined) {
  31. const error = new Meteor.Error('Unauthorized', 'Unauthorized');
  32. error.statusCode = 401;
  33. throw error;
  34. }
  35. };
  36. // An admin should be authorized to access everything, so we use a separate check for admins
  37. // This throws an error if otherReq is false and the user is not an admin
  38. Authentication.checkAdminOrCondition = function(userId, otherReq) {
  39. if (otherReq) return;
  40. const admin = ReactiveCache.getUser({ _id: userId, isAdmin: true });
  41. if (admin === undefined) {
  42. const error = new Meteor.Error('Forbidden', 'Forbidden');
  43. error.statusCode = 403;
  44. throw error;
  45. }
  46. };
  47. // Helper function. Will throw an error if the user is not active BoardAdmin or active Normal user of the board.
  48. Authentication.checkBoardAccess = function(userId, boardId) {
  49. Authentication.checkLoggedIn(userId);
  50. const board = ReactiveCache.getBoard(boardId);
  51. const normalAccess = board.members.some(e => e.userId === userId && e.isActive && !e.isNoComments && !e.isCommentOnly && !e.isWorker);
  52. Authentication.checkAdminOrCondition(userId, normalAccess);
  53. };
  54. if (Meteor.isServer) {
  55. if (
  56. process.env.ORACLE_OIM_ENABLED === 'true' ||
  57. process.env.ORACLE_OIM_ENABLED === true
  58. ) {
  59. ServiceConfiguration.configurations.upsert(
  60. // eslint-disable-line no-undef
  61. { service: 'oidc' },
  62. {
  63. $set: {
  64. loginStyle: process.env.OAUTH2_LOGIN_STYLE,
  65. clientId: process.env.OAUTH2_CLIENT_ID,
  66. secret: process.env.OAUTH2_SECRET,
  67. serverUrl: process.env.OAUTH2_SERVER_URL,
  68. authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
  69. userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
  70. tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
  71. idTokenWhitelistFields:
  72. process.env.OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  73. requestPermissions: process.env.OAUTH2_REQUEST_PERMISSIONS,
  74. },
  75. },
  76. );
  77. } else if (
  78. process.env.OAUTH2_ENABLED === 'true' ||
  79. process.env.OAUTH2_ENABLED === true
  80. ) {
  81. ServiceConfiguration.configurations.upsert(
  82. // eslint-disable-line no-undef
  83. { service: 'oidc' },
  84. {
  85. $set: {
  86. loginStyle: process.env.OAUTH2_LOGIN_STYLE,
  87. clientId: process.env.OAUTH2_CLIENT_ID,
  88. secret: process.env.OAUTH2_SECRET,
  89. serverUrl: process.env.OAUTH2_SERVER_URL,
  90. authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
  91. userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
  92. tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
  93. idTokenWhitelistFields:
  94. process.env.OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  95. requestPermissions: process.env.OAUTH2_REQUEST_PERMISSIONS,
  96. },
  97. // OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  98. // OAUTH2_REQUEST_PERMISSIONS || 'openid profile email',
  99. },
  100. );
  101. } else if (
  102. process.env.CAS_ENABLED === 'true' ||
  103. process.env.CAS_ENABLED === true
  104. ) {
  105. ServiceConfiguration.configurations.upsert(
  106. // eslint-disable-line no-undef
  107. { service: 'cas' },
  108. {
  109. $set: {
  110. baseUrl: process.env.CAS_BASE_URL,
  111. loginUrl: process.env.CAS_LOGIN_URL,
  112. serviceParam: 'service',
  113. popupWidth: 810,
  114. popupHeight: 610,
  115. popup: true,
  116. autoClose: true,
  117. validateUrl: process.env.CASE_VALIDATE_URL,
  118. casVersion: 3.0,
  119. attributes: {
  120. debug: process.env.DEBUG === 'true',
  121. },
  122. },
  123. },
  124. );
  125. } else if (
  126. process.env.SAML_ENABLED === 'true' ||
  127. process.env.SAML_ENABLED === true
  128. ) {
  129. ServiceConfiguration.configurations.upsert(
  130. // eslint-disable-line no-undef
  131. { service: 'saml' },
  132. {
  133. $set: {
  134. provider: process.env.SAML_PROVIDER,
  135. entryPoint: process.env.SAML_ENTRYPOINT,
  136. issuer: process.env.SAML_ISSUER,
  137. cert: process.env.SAML_CERT,
  138. idpSLORedirectURL: process.env.SAML_IDPSLO_REDIRECTURL,
  139. privateKeyFile: process.env.SAML_PRIVATE_KEYFILE,
  140. publicCertFile: process.env.SAML_PUBLIC_CERTFILE,
  141. identifierFormat: process.env.SAML_IDENTIFIER_FORMAT,
  142. localProfileMatchAttribute:
  143. process.env.SAML_LOCAL_PROFILE_MATCH_ATTRIBUTE,
  144. attributesSAML: process.env.SAML_ATTRIBUTES || [
  145. 'sn',
  146. 'givenName',
  147. 'mail',
  148. ],
  149. /*
  150. settings = {"saml":[{
  151. "provider":"openam",
  152. "entryPoint":"https://openam.idp.io/openam/SSORedirect/metaAlias/zimt/idp",
  153. "issuer": "https://sp.zimt.io/", //replace with url of your app
  154. "cert":"MIICizCCAfQCCQCY8tKaMc0 LOTS OF FUNNY CHARS ==",
  155. "idpSLORedirectURL": "http://openam.idp.io/openam/IDPSloRedirect/metaAlias/zimt/idp",
  156. "privateKeyFile": "certs/mykey.pem", // path is relative to $METEOR-PROJECT/private
  157. "publicCertFile": "certs/mycert.pem", // eg $METEOR-PROJECT/private/certs/mycert.pem
  158. "dynamicProfile": true // set to true if we want to create a user in Meteor.users dynamically if SAML assertion is valid
  159. "identifierFormat": "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified", // Defaults to urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress
  160. "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,
  161. "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.
  162. }]}
  163. */
  164. },
  165. },
  166. );
  167. }
  168. }
  169. });