node-red-contrib-opentext
Version:
node-red-contrib-opentext - An Opentext Core client
171 lines (143 loc) • 5.6 kB
JavaScript
/**
* MIT License
*
* Copyright (c) 2019 Marcos Caputo <caputo.marcos@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**/
module.exports = function (RED) {
"use strict";
// require any external libraries we may need....
let request = require("request");
var querystring = require("querystring");
// The main node definition - most things happen in here
function OAuth2Node(oauth2Node) {
// Create a RED node
RED.nodes.createNode(this, oauth2Node);
// Store local copies of the node configuration (as defined in the .html)
this.name = oauth2Node.name || "";
this.access_token_url = oauth2Node.access_token_url || "";
this.client_id = oauth2Node.client_id || "";
this.client_secret = oauth2Node.client_secret || "";
// copy "this" object in case we need it in context of callbacks of other functions.
let node = this;
let msg = {};
msg.payload = this.payload;
// respond to inputs....
this.on("input", function (msg) {
// set an empty form
node.status({
fill: "yellow",
shape: "dot",
text: "Getting token",
});
let Form = {};
// TODO - ??? =)
let Method = "Post";
let Authorization = "";
if (msg.ot2authRequest){
node.access_token_url = msg.ot2authRequest.access_token_url;
node.client_id = msg.ot2authRequest.credentials.client_id;
node.client_secret = msg.ot2authRequest.credentials.client_secret;
}
Form = {
grant_type: "client_credentials",
client_id: node.client_id,
client_secret: node.client_secret,
};
Authorization =
"Basic " +
Buffer.from(`${node.client_id}:${node.client_secret}`).toString(
"base64"
);
// }
//When the client secret and client id are passed to the as Authorization Basic for many API's it shouldn't shouldn't be sent in form.
delete Form.client_secret;
delete Form.client_id;
let Body = querystring.stringify(Form);
// set Headers
let Headers = {
// 'Accept': 'application/json',
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(Body),
Authorization: Authorization,
};
// Put all together
let Options = {
method: Method,
url: node.access_token_url,
headers: Headers,
body: Body,
json: false,
};
// make a post request
request.post(Options, function (err, response, body) {
if (msg.ot2authRequest) delete msg.ot2authRequest;
try {
let oauth2Body = JSON.parse(body ? body : JSON.stringify("{}"));
if (response && response.statusCode < 299 && response.statusCode > 199) {
msg.payload = oauth2Body;
msg.payload.statusCode = response.statusCode;
node.status({
fill: "green",
shape: "dot",
text: `HTTP ${response.statusCode}, token retrieved!`,
});
} else if (
response &&
response.statusCode &&
response.statusCode !== 200
) {
msg.payload = {
statusCode: response.statusCode,
statusMessage: response.statusMessage,
body: oauth2Body,
};
node.status({
fill: "red",
shape: "dot",
text: `HTTP ${response.statusCode}, error retreiving token!`,
});
}
if (err && err.code) {
msg.err = JSON.parse(JSON.stringify(err));
node.status({ fill: "yellow", shape: "dot", text: `ERR ${err.code}` });
} else if (err && err.message && err.stack) {
msg.err = { message: err.message, stack: err.stack };
node.status({
fill: "black",
shape: "dot",
text: `ERR ${err.message}`,
});
}
node.send(msg);
} catch (err) {
var d = new Date();
var n = d.toISOString();
console.log(n + ": " + err.message);
console.log(n + ": " + body.replace(/ |\r\n|\n|\r/gm, ""));
msg.err = JSON.parse(JSON.stringify(err));
node.status({ fill: "blue", shape: "dot", text: `ERR ${err.code}` });
}
});
});
}
RED.nodes.registerType("ot2auth", OAuth2Node);
};