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-01-01 16:26:51 +00:00
|
|
|
const jwt = require("jsonwebtoken");
|
|
|
|
var upperCase = require('upper-case');
|
|
|
|
var atob = require('atob');
|
2019-01-13 22:55:25 +00:00
|
|
|
var logger = require('./logger');
|
2019-01-01 16:26:51 +00:00
|
|
|
|
2019-02-24 13:16:41 +00:00
|
|
|
exports.authenticateUserWithCookie = (req, res, next) => {
|
|
|
|
if(+common.rtl_sso) {
|
2019-02-24 15:03:03 +00:00
|
|
|
res.cookie('access-key', req.query['access-key'], { httpOnly: true, sameSite: true, secure: true });
|
2019-02-24 13:16:41 +00:00
|
|
|
res.set(
|
|
|
|
{
|
|
|
|
'Cache-Control': 'private, no-cache'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
res.redirect(301, '/rtl/');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res.status(404).json({
|
|
|
|
message: "Login Failure!",
|
|
|
|
error: "SSO not available"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-02-24 14:28:02 +00:00
|
|
|
const access_key = req.cookies['access-key'];
|
|
|
|
res.clearCookie("access-key");
|
|
|
|
if (common.cookie === access_key) {
|
2019-02-12 13:36:04 +00:00
|
|
|
const token = jwt.sign(
|
|
|
|
{ user: 'Custom_User', lndConfigPath: common.lnd_config_path, macaroonPath: common.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-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-02-24 14:28:02 +00:00
|
|
|
password = atob(req.body.password);
|
2019-02-16 22:43:12 +00:00
|
|
|
if(upperCase(common.node_auth_type) === 'CUSTOM') {
|
|
|
|
if (common.rtl_pass === password) {
|
|
|
|
var rpcUser = 'Custom_User';
|
|
|
|
const token = jwt.sign(
|
|
|
|
{ user: rpcUser, lndConfigPath: common.lnd_config_path, macaroonPath: common.macaroon_path },
|
2019-02-24 17:00:39 +00:00
|
|
|
common.secret_key
|
2019-02-16 22:43:12 +00:00
|
|
|
);
|
|
|
|
res.status(200).json({ token: token });
|
2019-01-01 16:26:51 +00:00
|
|
|
} else {
|
2019-02-16 22:43:12 +00:00
|
|
|
res.status(401).json({
|
|
|
|
message: "Authentication Failed!",
|
|
|
|
error: "Password Validation Failed!"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.readFile(common.lnd_config_path, 'utf8', function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('\r\nAuthenticate: 45: ' + JSON.stringify(Date.now()) + ': ERROR: RTL Config Reading Failed!');
|
|
|
|
res.status(500).json({
|
|
|
|
message: "LND Config Reading Failed!",
|
|
|
|
error: err
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const jsonLNDConfig = ini.parse(data);
|
|
|
|
if (undefined !== jsonLNDConfig.Bitcoind && undefined !== jsonLNDConfig.Bitcoind['bitcoind.rpcpass']) {
|
|
|
|
if (jsonLNDConfig.Bitcoind['bitcoind.rpcpass'] === password) {
|
|
|
|
var rpcUser = (undefined !== jsonLNDConfig.Bitcoind['bitcoind.rpcuser']) ? jsonLNDConfig.Bitcoind['bitcoind.rpcuser'] : '';
|
|
|
|
const token = jwt.sign(
|
|
|
|
{ user: rpcUser, lndConfigPath: common.lnd_config_path, macaroonPath: common.macaroon_path },
|
2019-02-24 17:00:39 +00:00
|
|
|
common.secret_key
|
2019-02-16 22:43:12 +00:00
|
|
|
);
|
|
|
|
res.status(200).json({ token: token });
|
|
|
|
} else {
|
|
|
|
res.status(401).json({
|
|
|
|
message: "Authentication Failed!",
|
|
|
|
error: "Password Validation Failed!"
|
|
|
|
});
|
|
|
|
}
|
2019-01-01 16:26:51 +00:00
|
|
|
} else {
|
2019-02-12 13:36:04 +00:00
|
|
|
res.status(401).json({
|
|
|
|
message: "Authentication Failed!",
|
2019-02-16 22:43:12 +00:00
|
|
|
error: "Password Not Found In LND Config!"
|
2019-02-12 13:36:04 +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
|
|
|
};
|