2018-04-27 23:29:00 +02:00
|
|
|
const logger = require('winston');
|
2018-09-03 01:48:35 +02:00
|
|
|
const db = require('../../../../models');
|
2018-04-27 23:29:00 +02:00
|
|
|
|
|
|
|
const authenticateChannelCredentials = (channelName, channelId, userPassword) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// hoisted variables
|
|
|
|
let channelData;
|
|
|
|
// build the params for finding the channel
|
|
|
|
let channelFindParams = {};
|
|
|
|
if (channelName) channelFindParams['channelName'] = channelName;
|
|
|
|
if (channelId) channelFindParams['channelClaimId'] = channelId;
|
|
|
|
// find the channel
|
|
|
|
db.Channel
|
|
|
|
.findOne({
|
|
|
|
where: channelFindParams,
|
|
|
|
})
|
|
|
|
.then(channel => {
|
|
|
|
if (!channel) {
|
|
|
|
logger.debug('no channel found');
|
|
|
|
throw new Error('Authentication failed, you do not have access to that channel');
|
|
|
|
}
|
|
|
|
channelData = channel.get();
|
|
|
|
logger.debug('channel data:', channelData);
|
|
|
|
return db.User.findOne({
|
|
|
|
where: { userName: channelData.channelName.substring(1) },
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(user => {
|
|
|
|
if (!user) {
|
|
|
|
logger.debug('no user found');
|
|
|
|
throw new Error('Authentication failed, you do not have access to that channel');
|
|
|
|
}
|
|
|
|
return user.comparePassword(userPassword);
|
|
|
|
})
|
|
|
|
.then(isMatch => {
|
|
|
|
if (!isMatch) {
|
|
|
|
logger.debug('incorrect password');
|
|
|
|
throw new Error('Authentication failed, you do not have access to that channel');
|
|
|
|
}
|
|
|
|
logger.debug('...password was a match...');
|
|
|
|
resolve(channelData);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const authenticateUser = (channelName, channelId, channelPassword, user) => {
|
2018-07-27 21:31:08 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// case: no channelName or channel Id are provided (anonymous), regardless of whether user token is provided
|
|
|
|
if (!channelName && !channelId) {
|
|
|
|
resolve({
|
|
|
|
channelName : null,
|
|
|
|
channelClaimId: null,
|
|
|
|
});
|
|
|
|
return;
|
2018-04-27 23:29:00 +02:00
|
|
|
}
|
2018-07-27 21:31:08 +02:00
|
|
|
// case: channelName or channel Id are provided with user token
|
|
|
|
if (user) {
|
|
|
|
if (channelName && channelName !== user.channelName) {
|
|
|
|
reject(new Error('the provided channel name does not match user credentials'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (channelId && channelId !== user.channelClaimId) {
|
|
|
|
reject(new Error('the provided channel id does not match user credentials'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve({
|
|
|
|
channelName : user.channelName,
|
|
|
|
channelClaimId: user.channelClaimId,
|
|
|
|
});
|
|
|
|
return;
|
2018-04-27 23:29:00 +02:00
|
|
|
}
|
2018-07-27 21:31:08 +02:00
|
|
|
// case: channelName or channel Id are provided with password instead of user token
|
|
|
|
if (!channelPassword) {
|
|
|
|
reject(new Error('no channel password provided'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(authenticateChannelCredentials(channelName, channelId, channelPassword));
|
|
|
|
});
|
2018-04-27 23:29:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = authenticateUser;
|