UNPKG

@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)

78 lines 4.83 kB
"use strict"; 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 uuid = require("uuid"); const mqtt_opcua_auth_1 = require("../../api/mqtt-opcua-auth"); const command_utils_1 = require("./command-utils"); const color = (0, command_utils_1.getColor)("green"); exports.default = (program) => { program .command("mqtt-createjwt") .alias("jw") .option("-i, --clientid <clientid>", "MQTT ClientId", uuid.v4()) .option("-e, --expiration <expiration>", "time until the token is valid in seconds", `${1 * 60 * 60}`) .option("-c, --rootca <rootca>", "path to CA root certificate", "CA-root.pem") .option("-d, --devicecrt <devicecrt>", "path to device certificate", "device.crt.pem") .option("-k, --devicekey <devicekey>", "path to device certificate key", "device.key.pem") .option("-p, --passphrase [passphrase]", "passphrase for device certificate key") .option("-n, --intermediate [intermediate]", "intermediate ca") .option("-t, --tenant [tenant]", "tenant name") .option("-v, --verbose", "verbose output") .description(color("creates a signed token for opcua pub sub authentication #")) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkParameters(options); const rootca = fs.readFileSync(path.resolve(options.rootca)).toString(); const devicecrt = fs.readFileSync(path.resolve(options.devicecrt)).toString(); const devicekey = fs.readFileSync(path.resolve(options.devicekey)).toString(); // console.log(options.intermediate); let intermediate = undefined; if (options.intermediate) { intermediate = fs.readFileSync(path.resolve(options.intermediate)).toString(); } const mqttTokenRotation = new mqtt_opcua_auth_1.MqttOpcUaAuth(options.clientid, rootca, devicecrt, options.expiration, devicekey, intermediate, options.passphrase, options.tenant); const token = mqttTokenRotation.GetMqttToken(); console.log(token); (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 mqtt-createjwt --clientid "12345...ef" \\`); (0, console_1.log)(` --rootca path/to/root.cer.pem \\`); (0, console_1.log)(` --devicecrt path/to/device.cer.pem \\`); (0, console_1.log)(` --devicekey path/to/devicekey.pem \\`); (0, console_1.log)(` --passphrase "device key passphrase" \\`); (0, console_1.log)(` --tenant yourtenant`); (0, console_1.log)(`\n More Information: \n`); (0, console_1.log)(` ${color("https://developer.mindsphere.io/howto/howto-connect-via-mqtt.html#security-concept-onboarding-certificatebearer")}\n`); }); }; function checkParameters(options) { !options.rootca && (0, command_utils_1.errorLog)("You have to specify the path to the root certificate. Run mdsp jw --help for full syntax and examples.", options.verbose); !options.devicecrt && (0, command_utils_1.errorLog)("You have to specify the path to the device certificate. Run mdsp jw --help for full syntax and examples.", options.verbose); !options.devicekey && (0, command_utils_1.errorLog)("You have to specify the path to the device key. Run mdsp jw --help for full syntax and examples.", options.verbose); !options.tenant && (0, command_utils_1.errorLog)("You have to specify the name of your tenant. Run mdsp jw --help for full syntax and examples.", options.verbose); } //# sourceMappingURL=mqtt-create-jwt.js.map