wekan/server/authentication.js

90 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-04-18 16:03:49 +03:00
import Fiber from 'fibers';
Meteor.startup(() => {
// Node Fibers 100% CPU usage issue
// https://github.com/wekan/wekan-mongodb/issues/2#issuecomment-381453161
// https://github.com/meteor/meteor/issues/9796#issuecomment-381676326
// https://github.com/sandstorm-io/sandstorm/blob/0f1fec013fe7208ed0fd97eb88b31b77e3c61f42/shell/server/00-startup.js#L99-L129
Fiber.poolSize = 1e9;
2019-06-28 12:52:09 -05:00
Accounts.validateLoginAttempt(function(options) {
const user = options.user || {};
return !user.loginDisabled;
});
Authentication = {};
2019-06-28 12:52:09 -05:00
Authentication.checkUserId = function(userId) {
if (userId === undefined) {
const error = new Meteor.Error('Unauthorized', 'Unauthorized');
error.statusCode = 401;
throw error;
}
const admin = Users.findOne({ _id: userId, isAdmin: true });
if (admin === undefined) {
const error = new Meteor.Error('Forbidden', 'Forbidden');
error.statusCode = 403;
throw error;
}
};
// This will only check if the user is logged in.
// The authorization checks for the user will have to be done inside each API endpoint
Authentication.checkLoggedIn = function(userId) {
2019-06-28 12:52:09 -05:00
if (userId === undefined) {
const error = new Meteor.Error('Unauthorized', 'Unauthorized');
error.statusCode = 401;
throw error;
}
};
2017-05-15 19:43:15 +02:00
// An admin should be authorized to access everything, so we use a separate check for admins
// This throws an error if otherReq is false and the user is not an admin
Authentication.checkAdminOrCondition = function(userId, otherReq) {
2019-06-28 12:52:09 -05:00
if (otherReq) return;
2017-05-15 19:43:15 +02:00
const admin = Users.findOne({ _id: userId, isAdmin: true });
if (admin === undefined) {
const error = new Meteor.Error('Forbidden', 'Forbidden');
error.statusCode = 403;
throw error;
}
2017-05-15 22:10:46 +02:00
};
2017-05-15 19:43:15 +02:00
2017-05-15 21:02:31 +02:00
// Helper function. Will throw an error if the user does not have read only access to the given board
Authentication.checkBoardAccess = function(userId, boardId) {
Authentication.checkLoggedIn(userId);
const board = Boards.findOne({ _id: boardId });
2019-06-28 12:52:09 -05:00
const normalAccess =
board.permission === 'public' ||
board.members.some(e => e.userId === userId).isActive;
2017-05-15 21:02:31 +02:00
Authentication.checkAdminOrCondition(userId, normalAccess);
2017-05-15 22:10:46 +02:00
};
2017-05-15 21:02:31 +02:00
2018-04-09 16:49:07 +02:00
if (Meteor.isServer) {
2019-06-28 12:52:09 -05:00
if (process.env.OAUTH2_CLIENT_ID !== '') {
ServiceConfiguration.configurations.upsert(
// eslint-disable-line no-undef
{ service: 'oidc' },
{
$set: {
loginStyle: process.env.OAUTH2_LOGIN_STYLE,
clientId: process.env.OAUTH2_CLIENT_ID,
secret: process.env.OAUTH2_SECRET,
serverUrl: process.env.OAUTH2_SERVER_URL,
authorizationEndpoint: process.env.OAUTH2_AUTH_ENDPOINT,
userinfoEndpoint: process.env.OAUTH2_USERINFO_ENDPOINT,
tokenEndpoint: process.env.OAUTH2_TOKEN_ENDPOINT,
2019-06-28 12:52:09 -05:00
idTokenWhitelistFields:
process.env.OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
requestPermissions: process.env.OAUTH2_REQUEST_PERMISSIONS,
},
// OAUTH2_ID_TOKEN_WHITELIST_FIELDS || [],
// OAUTH2_REQUEST_PERMISSIONS || 'openid profile email',
2019-06-28 12:52:09 -05:00
},
);
2018-04-09 16:49:07 +02:00
}
}
});