Ride-The-Lightning-RTL/controllers/authenticate.js

83 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-02-09 15:37:36 -05:00
var common = require('../common');
2019-04-05 22:52:00 -04:00
var connect = require('../connect');
2019-01-01 11:26:51 -05:00
const jwt = require("jsonwebtoken");
var crypto = require('crypto');
2019-01-13 17:55:25 -05:00
var logger = require('./logger');
const otplib = require("otplib");
2019-01-01 11:26:51 -05:00
exports.authenticateUser = (req, res, next) => {
2019-02-12 08:36:04 -05:00
if(+common.rtl_sso) {
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-05 22:52:00 -04:00
connect.refreshCookie(common.rtl_cookie_path);
const token = jwt.sign(
{ user: 'SSO_USER', configPath: common.nodes[0].config_path, macaroonPath: common.nodes[0].macaroon_path },
2019-02-24 12:00:39 -05:00
common.secret_key
2019-02-12 08:36:04 -05:00
);
res.status(200).json({ token: token });
2019-01-01 11:26:51 -05:00
} else {
2019-07-27 14:20:17 -04:00
logger.error({fileName: 'Authenticate', lineNum: 21, msg: 'Password Validation Failed!'});
2019-02-12 08:36:04 -05:00
res.status(401).json({
message: "Login Failure!",
error: "SSO Authentication Failed!"
2019-02-12 08:36:04 -05:00
});
}
} else {
const password = req.body.authenticationValue;
if (common.rtl_pass === password) {
var rpcUser = 'NODE_USER';
const token = jwt.sign(
{ user: rpcUser, configPath: common.nodes[0].config_path, macaroonPath: common.nodes[0].macaroon_path },
common.secret_key
);
res.status(200).json({ token: token });
} else {
logger.error({fileName: 'Authenticate', lineNum: 38, msg: 'Password Validation Failed!'});
res.status(401).json({
message: "Authentication Failed!",
error: "Password Validation Failed!"
});
2019-02-16 17:43:12 -05:00
}
2019-02-12 08:36:04 -05:00
}
2019-02-24 09:28:02 -05:00
};
exports.resetPassword = (req, res, next) => {
if(+common.rtl_sso) {
logger.error({fileName: 'Authenticate', lineNum: 46, msg: 'Password Reset Failed!'});
res.status(402).json({
message: "Password Reset Failure!",
error: "Password cannot be reset for SSO authentication!"
});
} else {
const currPassword = req.body.currPassword;
if (common.rtl_pass === currPassword) {
common.rtl_pass = connect.replacePasswordWithHash(req.body.newPassword);
var rpcUser = 'NODE_USER';
const token = jwt.sign(
{ user: rpcUser, configPath: common.nodes[0].config_path, macaroonPath: common.nodes[0].macaroon_path },
common.secret_key
);
res.status(200).json({ token: token });
} else {
logger.error({fileName: 'Authenticate', lineNum: 62, msg: 'Password Reset Failed!'});
res.status(402).json({
message: "Password Reset Failed!",
error: "Old password is not correct!"
});
}
}
};
exports.verifyToken = (req, res, next) => {
const token2fa = req.body.authentication2FA;
if (!common.rtl_secret2fa || otplib.authenticator.check(token2fa, common.rtl_secret2fa)) {
res.status(200).json({ isValidToken: true });
} else {
logger.error({fileName: 'Authenticate', lineNum: 77, msg: 'Token Verification Failed!'});
res.status(401).json({
message: "Authentication Failed!",
error: "Token Verification Failed!"
});
}
};