2019-01-01 16:26:51 +00:00
|
|
|
var ini = require('ini');
|
|
|
|
var fs = require('fs');
|
2019-02-09 20:37:36 +00:00
|
|
|
var common = require('../common');
|
2019-04-06 02:52:00 +00:00
|
|
|
var connect = require('../connect');
|
2019-01-01 16:26:51 +00:00
|
|
|
const jwt = require("jsonwebtoken");
|
|
|
|
var upperCase = require('upper-case');
|
2019-05-12 18:09:07 +00:00
|
|
|
var crypto = require('crypto');
|
|
|
|
var hash = crypto.createHash('sha256');
|
2019-01-13 22:55:25 +00:00
|
|
|
var logger = require('./logger');
|
2019-10-12 22:23:46 +00:00
|
|
|
var rpcPass = '';
|
2019-01-01 16:26:51 +00:00
|
|
|
|
|
|
|
exports.authenticateUser = (req, res, next) => {
|
2019-02-12 13:36:04 +00:00
|
|
|
if(+common.rtl_sso) {
|
2019-09-19 02:47:18 +00:00
|
|
|
if(req.body.authenticateWith === 'TOKEN' && jwt.verify(req.body.authenticationValue, common.secret_key)) {
|
|
|
|
res.status(200).json({ token: token });
|
|
|
|
} else if (req.body.authenticateWith === 'PASSWORD' && crypto.createHash('sha256').update(common.cookie).digest('hex') === req.body.authenticationValue) {
|
2019-04-06 02:52:00 +00:00
|
|
|
connect.refreshCookie(common.rtl_cookie_path);
|
2019-09-22 18:39:11 +00:00
|
|
|
const token = jwt.sign(
|
2019-09-02 04:11:37 +00:00
|
|
|
{ user: 'Custom_User', configPath: common.nodes[0].config_path, macaroonPath: common.nodes[0].macaroon_path },
|
2019-02-24 17:00:39 +00:00
|
|
|
common.secret_key
|
2019-02-12 13:36:04 +00:00
|
|
|
);
|
2019-02-24 03:58:51 +00:00
|
|
|
res.status(200).json({ token: token });
|
2019-01-01 16:26:51 +00:00
|
|
|
} else {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Authenticate', lineNum: 21, msg: 'Password Validation Failed!'});
|
2019-02-12 13:36:04 +00:00
|
|
|
res.status(401).json({
|
2019-02-14 02:31:26 +00:00
|
|
|
message: "Login Failure!",
|
|
|
|
error: "SSO Authentication Failed!"
|
2019-02-12 13:36:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-19 02:47:18 +00:00
|
|
|
const password = req.body.authenticationValue;
|
2019-04-06 22:42:09 +00:00
|
|
|
if (common.multi_node_setup) {
|
|
|
|
if (common.rtl_pass === password) {
|
|
|
|
var rpcUser = 'Multi_Node_User';
|
|
|
|
const token = jwt.sign(
|
2019-09-02 04:11:37 +00:00
|
|
|
{ user: rpcUser, configPath: common.nodes[0].config_path, macaroonPath: common.nodes[0].macaroon_path },
|
2019-04-06 22:42:09 +00:00
|
|
|
common.secret_key
|
|
|
|
);
|
|
|
|
res.status(200).json({ token: token });
|
|
|
|
} else {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Authenticate', lineNum: 38, msg: 'Password Validation Failed!'});
|
2019-04-06 22:42:09 +00:00
|
|
|
res.status(401).json({
|
|
|
|
message: "Authentication Failed!",
|
|
|
|
error: "Password Validation Failed!"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-31 15:39:41 +00:00
|
|
|
if(upperCase(common.node_auth_type) === 'CUSTOM') {
|
|
|
|
if (common.rtl_pass === password) {
|
2019-04-06 22:42:09 +00:00
|
|
|
var rpcUser = 'Single_Node_User';
|
2019-03-31 15:39:41 +00:00
|
|
|
const token = jwt.sign(
|
2019-09-02 04:11:37 +00:00
|
|
|
{ user: rpcUser, configPath: common.nodes[0].config_path, macaroonPath: common.nodes[0].macaroon_path },
|
2019-03-31 15:39:41 +00:00
|
|
|
common.secret_key
|
|
|
|
);
|
|
|
|
res.status(200).json({ token: token });
|
2019-02-16 22:43:12 +00:00
|
|
|
} else {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Authenticate', lineNum: 54, msg: 'Password Validation Failed!'});
|
2019-03-31 15:39:41 +00:00
|
|
|
res.status(401).json({
|
|
|
|
message: "Authentication Failed!",
|
|
|
|
error: "Password Validation Failed!"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-02 04:11:37 +00:00
|
|
|
fs.readFile(common.nodes[0].config_path, 'utf8', function (err, data) {
|
2019-03-31 15:39:41 +00:00
|
|
|
if (err) {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Authenticate', lineNum: 60, msg: 'LND Config Reading Failed!'});
|
2019-04-06 02:52:00 +00:00
|
|
|
err.description = 'You might be connecting RTL remotely to your LND node OR You might be missing rpcpass in your lnd.conf.';
|
|
|
|
err.description = err.description + ' If the former modify the RTL.conf for remote setting.';
|
|
|
|
err.description = err.description + ' If the later modify the lnd.conf to include rpcpass';
|
2019-03-31 15:39:41 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: "LND Config Reading Failed!",
|
|
|
|
error: err
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const jsonLNDConfig = ini.parse(data);
|
2019-10-12 22:23:46 +00:00
|
|
|
if (rpcPass === '') {
|
|
|
|
if (undefined !== jsonLNDConfig.Bitcoind && undefined !== jsonLNDConfig.Bitcoind['bitcoind.rpcpass']) {
|
|
|
|
rpcPass = jsonLNDConfig.Bitcoind['bitcoind.rpcpass'];
|
|
|
|
} else if (undefined !== jsonLNDConfig['bitcoind.rpcpass']) {
|
|
|
|
rpcPass = jsonLNDConfig['bitcoind.rpcpass'];
|
|
|
|
}
|
|
|
|
rpcPass = hash.update(rpcPass).digest('hex');
|
2019-05-12 18:09:07 +00:00
|
|
|
}
|
|
|
|
if (rpcPass === password) {
|
|
|
|
var rpcUser = (undefined !== jsonLNDConfig.Bitcoind && undefined !== jsonLNDConfig.Bitcoind['bitcoind.rpcuser']) ? jsonLNDConfig.Bitcoind['bitcoind.rpcuser'] : '';
|
|
|
|
rpcUser = (rpcUser === '' && undefined !== jsonLNDConfig['bitcoind.rpcuser']) ? jsonLNDConfig['bitcoind.rpcuser'] : '';
|
|
|
|
const token = jwt.sign(
|
2019-09-02 04:11:37 +00:00
|
|
|
{ user: rpcUser, configPath: common.nodes[0].config_path, macaroonPath: common.nodes[0].macaroon_path },
|
2019-05-12 18:09:07 +00:00
|
|
|
common.secret_key
|
|
|
|
);
|
|
|
|
res.status(200).json({ token: token });
|
2019-02-16 22:43:12 +00:00
|
|
|
} else {
|
2019-07-27 18:20:17 +00:00
|
|
|
logger.error({fileName: 'Authenticate', lineNum: 89, msg: 'Password Validation Failed!'});
|
2019-02-16 22:43:12 +00:00
|
|
|
res.status(401).json({
|
|
|
|
message: "Authentication Failed!",
|
2019-05-12 18:09:07 +00:00
|
|
|
error: "Password Validation Failed!"
|
2019-02-16 22:43:12 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-12 13:36:04 +00:00
|
|
|
}
|
2019-03-31 15:39:41 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-16 22:43:12 +00:00
|
|
|
}
|
2019-02-12 13:36:04 +00:00
|
|
|
}
|
2019-02-24 14:28:02 +00:00
|
|
|
};
|