authentication.js 7.1 KB

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