authentication.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 = Users.findOne({ _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 = Users.findOne({ _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 does not have read only access to the given board
  48. Authentication.checkBoardAccess = function(userId, boardId) {
  49. Authentication.checkLoggedIn(userId);
  50. const board = ReactiveCache.getBoard(boardId);
  51. const normalAccess =
  52. board.permission === 'public' ||
  53. board.members.some(e => e.userId === userId && e.isActive);
  54. Authentication.checkAdminOrCondition(userId, normalAccess);
  55. };
  56. if (Meteor.isServer) {
  57. if (
  58. process.env.ORACLE_OIM_ENABLED === 'true' ||
  59. process.env.ORACLE_OIM_ENABLED === true
  60. ) {
  61. ServiceConfiguration.configurations.upsert(
  62. // eslint-disable-line no-undef
  63. { service: 'oidc' },
  64. {
  65. $set: {
  66. loginStyle: process.env.OAUTH2_LOGIN_STYLE,
  67. clientId: process.env.OAUTH2_CLIENT_ID,
  68. secret: process.env.OAUTH2_SECRET,
  69. serverUrl: process.env.OAUTH2_SERVER_URL,
  70. authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
  71. userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
  72. tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
  73. idTokenWhitelistFields:
  74. process.env.OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  75. requestPermissions: process.env.OAUTH2_REQUEST_PERMISSIONS,
  76. },
  77. },
  78. );
  79. } else if (
  80. process.env.OAUTH2_ENABLED === 'true' ||
  81. process.env.OAUTH2_ENABLED === true
  82. ) {
  83. ServiceConfiguration.configurations.upsert(
  84. // eslint-disable-line no-undef
  85. { service: 'oidc' },
  86. {
  87. $set: {
  88. loginStyle: process.env.OAUTH2_LOGIN_STYLE,
  89. clientId: process.env.OAUTH2_CLIENT_ID,
  90. secret: process.env.OAUTH2_SECRET,
  91. serverUrl: process.env.OAUTH2_SERVER_URL,
  92. authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
  93. userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
  94. tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
  95. idTokenWhitelistFields:
  96. process.env.OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  97. requestPermissions: process.env.OAUTH2_REQUEST_PERMISSIONS,
  98. },
  99. // OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
  100. // OAUTH2_REQUEST_PERMISSIONS || 'openid profile email',
  101. },
  102. );
  103. } else if (
  104. process.env.CAS_ENABLED === 'true' ||
  105. process.env.CAS_ENABLED === true
  106. ) {
  107. ServiceConfiguration.configurations.upsert(
  108. // eslint-disable-line no-undef
  109. { service: 'cas' },
  110. {
  111. $set: {
  112. baseUrl: process.env.CAS_BASE_URL,
  113. loginUrl: process.env.CAS_LOGIN_URL,
  114. serviceParam: 'service',
  115. popupWidth: 810,
  116. popupHeight: 610,
  117. popup: true,
  118. autoClose: true,
  119. validateUrl: process.env.CASE_VALIDATE_URL,
  120. casVersion: 3.0,
  121. attributes: {
  122. debug: process.env.DEBUG,
  123. },
  124. },
  125. },
  126. );
  127. } else if (
  128. process.env.SAML_ENABLED === 'true' ||
  129. process.env.SAML_ENABLED === true
  130. ) {
  131. ServiceConfiguration.configurations.upsert(
  132. // eslint-disable-line no-undef
  133. { service: 'saml' },
  134. {
  135. $set: {
  136. provider: process.env.SAML_PROVIDER,
  137. entryPoint: process.env.SAML_ENTRYPOINT,
  138. issuer: process.env.SAML_ISSUER,
  139. cert: process.env.SAML_CERT,
  140. idpSLORedirectURL: process.env.SAML_IDPSLO_REDIRECTURL,
  141. privateKeyFile: process.env.SAML_PRIVATE_KEYFILE,
  142. publicCertFile: process.env.SAML_PUBLIC_CERTFILE,
  143. identifierFormat: process.env.SAML_IDENTIFIER_FORMAT,
  144. localProfileMatchAttribute:
  145. process.env.SAML_LOCAL_PROFILE_MATCH_ATTRIBUTE,
  146. attributesSAML: process.env.SAML_ATTRIBUTES || [
  147. 'sn',
  148. 'givenName',
  149. 'mail',
  150. ],
  151. /*
  152. settings = {"saml":[{
  153. "provider":"openam",
  154. "entryPoint":"https://openam.idp.io/openam/SSORedirect/metaAlias/zimt/idp",
  155. "issuer": "https://sp.zimt.io/", //replace with url of your app
  156. "cert":"MIICizCCAfQCCQCY8tKaMc0 LOTS OF FUNNY CHARS ==",
  157. "idpSLORedirectURL": "http://openam.idp.io/openam/IDPSloRedirect/metaAlias/zimt/idp",
  158. "privateKeyFile": "certs/mykey.pem", // path is relative to $METEOR-PROJECT/private
  159. "publicCertFile": "certs/mycert.pem", // eg $METEOR-PROJECT/private/certs/mycert.pem
  160. "dynamicProfile": true // set to true if we want to create a user in Meteor.users dynamically if SAML assertion is valid
  161. "identifierFormat": "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified", // Defaults to urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress
  162. "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,
  163. "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.
  164. }]}
  165. */
  166. },
  167. },
  168. );
  169. }
  170. }
  171. });