2017-09-28 19:51:02 +02:00
|
|
|
const db = require('../models');
|
|
|
|
const logger = require('winston');
|
2017-09-15 23:41:47 +02:00
|
|
|
|
2017-09-28 19:51:02 +02:00
|
|
|
module.exports = {
|
2017-11-13 19:21:39 +01:00
|
|
|
authenticateChannelCredentials (channelName, userPassword) {
|
2017-09-28 19:51:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-05 23:48:08 +02:00
|
|
|
const userName = channelName.substring(1);
|
|
|
|
logger.debug(`authenticateChannelCredentials > channelName: ${channelName} username: ${userName} pass: ${userPassword}`);
|
2017-09-28 19:51:02 +02:00
|
|
|
db.User
|
2018-02-22 02:02:57 +01:00
|
|
|
.findOne({where: { userName }})
|
|
|
|
.then(user => {
|
|
|
|
if (!user) {
|
|
|
|
logger.debug('no user found');
|
|
|
|
resolve(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return user.comparePassword(userPassword, (passwordErr, isMatch) => {
|
|
|
|
if (passwordErr) {
|
|
|
|
logger.error('comparePassword error:', passwordErr);
|
2017-09-28 19:51:02 +02:00
|
|
|
resolve(false);
|
|
|
|
return;
|
|
|
|
}
|
2018-02-22 02:02:57 +01:00
|
|
|
if (!isMatch) {
|
|
|
|
logger.debug('incorrect password');
|
|
|
|
resolve(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.debug('...password was a match...');
|
|
|
|
resolve(true);
|
2017-09-28 19:51:02 +02:00
|
|
|
});
|
2018-02-22 02:02:57 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-09-28 19:51:02 +02:00
|
|
|
});
|
2017-09-15 23:41:47 +02:00
|
|
|
},
|
2017-12-15 19:26:51 +01:00
|
|
|
authenticateIfNoUserToken (channelName, channelPassword, user) {
|
2017-11-13 20:20:37 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-15 20:19:10 +01:00
|
|
|
if (user || !channelName) {
|
2017-11-13 20:20:37 +01:00
|
|
|
return resolve(true);
|
|
|
|
}
|
|
|
|
return resolve(module.exports.authenticateChannelCredentials(channelName, channelPassword));
|
|
|
|
});
|
|
|
|
},
|
2017-09-15 23:41:47 +02:00
|
|
|
};
|