UNPKG

xsa_node_client

Version:

Node XSA UAA Client - A package to access resources secured by XSA UAA using technical user login credentials

69 lines (67 loc) 2.38 kB
var request = require('request'); var debug = require('debug')('Node XSA UAA Client'); var getContent = function (options, callback) { /************************************************************************** * To Add: * Debug Capabilities * Override default XSA Ports * Test Scripts ****************************************************************************/ //Use Cookie Jar by default request = request.defaults({ jar: true }) //Validate Options /* 1. Check for HTTP(S) 2. HOST/PORT Check 3. Default XSA port check 4. User and Password Check */ debug("Connecting"); if (options.https) { //Incase of HTTPS URLs, this needs to be set to '0' debug("HTTPs mode"); process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; } //Check for empty host or port configurations if (!(options.host && options.port)) { callback({ body: body, res: res, err: 'Specify Host / Port of the target XSA resource to access!' }); } else { debug("Host/Port check passed"); } if (!options.defaultXSAPorts) { //Need to take port number for XSA security endpoint } else { debug("Default XSA ports are used"); } if (!(options.user && options.pass)) { callback({ body: body, res: res, err: 'User/Password missing.Please specify' }); } else { debug("User and Password is set"); } //Create the target URL var targetURL = (options.https ? 'https://' : 'http://') + options.host + ':' + options.port + options.path; debug("Accessing : " + targetURL); request(targetURL, { followAllRedirects: true }, function (err, response, body) { debug("Logging into ", response.request.href); debug(response.headers['set-cookie']); var cook = response.headers['set-cookie']; var csrf = cook[0].split(";")[0].split("=")[1]; debug("CSRF is " + csrf); request({ followAllRedirects: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, uri: (options.https ? 'https://' : 'http://') + options.host + ':30232/uaa-security/login.do', body: 'username=' + encodeURIComponent(options.user) + '&password=' + encodeURIComponent(options.pass) + '&X-Uaa-Csrf=' + csrf, method: 'POST' }, function (err, res, body) { if (!err) debug("Error " + err); callback({ body: body, res: res, err: err }); }); }); } module.exports.getContent = getContent;