@mindconnect/mindconnect-nodejs
Version:
NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)
68 lines • 4.18 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const console_1 = require("console");
const fs = require("fs");
const jwt = require("jsonwebtoken");
const path = require("path");
const __1 = require("../..");
const utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const color = (0, command_utils_1.getColor)("cyan");
exports.default = (program) => {
program
.command("agent-token")
.alias("atk")
.option("-c, --config <agentconfig>", "config file with agent configuration", "agentconfig.json")
.option("-k, --passkey <passkey>", "passkey")
.option("-r, --cert [privatekey]", "required for agents with RSA_3072 profile. create with: openssl genrsa -out private.key 3072")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(color(`displays the agent token for use in other tools (e.g. postman)`))
.action((options) => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
(0, command_utils_1.homeDirLog)(options.verbose, color);
(0, command_utils_1.proxyLog)(options.verbose, color);
const configFile = path.resolve(options.config);
(0, command_utils_1.verboseLog)(`Agent Token for the agent configuration in: ${color(configFile)}.`, options.verbose);
if (!fs.existsSync(configFile)) {
throw new Error(`Can't find file ${configFile}`);
}
const configuration = require(configFile);
const profile = (0, utils_1.checkCertificate)(configuration, options);
const agentFolder = (0, utils_1.getAgentDir)(path.dirname(options.config));
(0, command_utils_1.verboseLog)(`Using .mc folder for agent: ${color(agentFolder)}`, options.verbose);
const agent = new __1.MindConnectAgent(configuration, undefined, agentFolder);
if (profile) {
agent.SetupAgentCertificate(fs.readFileSync(options.cert));
}
(0, command_utils_1.verboseLog)("encoded token:\n", options.verbose);
let token = "";
yield (0, utils_1.retry)(options.retry, () => __awaiter(void 0, void 0, void 0, function* () { return (token = yield agent.GetAgentToken()); }), 300, (0, command_utils_1.retrylog)("GetAgentToken"));
(0, console_1.log)(token);
(0, command_utils_1.verboseLog)("decoded token:\n", options.verbose);
(0, command_utils_1.verboseLog)(JSON.stringify(jwt.decode(token, { complete: true }), null, 2), options.verbose);
}
catch (err) {
(0, command_utils_1.errorLog)(err, options.verbose);
}
}))();
})
.on("--help", () => {
(0, console_1.log)("\n Examples:\n");
(0, console_1.log)(` mdsp agent-token \t\t\t\tuses default ${color("agentconfig.json")}`);
(0, console_1.log)(` mdsp agent-token --config agent.json \t\tuses specified configuration file`);
(0, console_1.log)(` mdsp agent-token --cert private.key \t\tuses specified key for RSA_3072 profile`);
(0, console_1.log)(` mdsp agent-token --verbose \t\t\tdisplays encoded and decoded version of the token`);
});
};
//# sourceMappingURL=mc-agent-token.js.map