@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)
128 lines • 5.15 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 });
exports.TokenManagerAuth = void 0;
const cross_fetch_1 = require("cross-fetch");
const debug = require("debug");
const auth_base_1 = require("./auth-base");
const utils_1 = require("./utils");
const log = debug("mindconnect-credentialauth");
/**
* Token Manager Authentication
*
* @see https://developer.mindsphere.io/apis/exchange-tokenmanager/api-tokenmanager-overview.html
*
* @export
* @class TokenManagerAuth
* @extends {AuthBase}
* @implements {TokenRotation}
*/
class TokenManagerAuth extends auth_base_1.AuthBase {
AcquireToken() {
return __awaiter(this, void 0, void 0, function* () {
const headers = Object.assign(Object.assign({}, this._apiHeaders), { "X-SPACE-AUTH-KEY": this._basicAuth });
const url = `${this._gateway}/api/technicaltokenmanager/v3/oauth/token`;
log(`AcquireToken Headers: ${JSON.stringify(headers)} Url: ${url}`);
const body = {
appName: this._appName,
appVersion: this._appVersion,
hostTenant: this._hostTenant,
userTenant: this._userTenant,
};
try {
const response = yield (0, cross_fetch_1.default)(url, {
method: "POST",
body: JSON.stringify(body),
headers: headers,
agent: this._proxyHttpAgent,
});
if (!response.ok) {
throw new Error(`${response.statusText} ${yield response.text()}`);
}
if (response.status >= 200 && response.status <= 299) {
const json = yield response.json();
log(`AcquireToken Response ${JSON.stringify(json)}`);
this._accessToken = json;
}
else {
throw new Error(`invalid response ${JSON.stringify(response)}`);
}
}
catch (err) {
log(err);
throw new Error(`Network error occured ${err.message}`);
}
return true;
});
}
/**
* Returns the current agent token.
* This token can be used in e.g. in Postman to call mindsphere APIs.
*
* @returns {(Promise<string>)}
*
* @memberOf AgentAuth
*/
GetToken() {
return __awaiter(this, void 0, void 0, function* () {
yield this.RenewToken();
if (!this._accessToken || !this._accessToken.access_token)
throw new Error("Error getting the new token!");
return this._accessToken.access_token;
});
}
/**
* User Tenant
*
* @returns {string}
*
* @memberOf TokenManagerAuth
*/
GetUserTenant() {
return this._userTenant;
}
/**
* HostTenant
*
* @returns {string}
*
* @memberOf TokenManagerAuth
*/
GetHostTenant() {
return this._hostTenant;
}
/**
* Creates an instance of TokenManagerAuth.
* @param {string} _gateway
* @param {string} _basicAuth
* @param {string} _hostTenant
*
* @memberOf TokenManagerAuth
*/
constructor(_gateway, _basicAuth, _hostTenant, _userTenant, _appName = "cli", _appVersion = "1.0.0") {
super(_gateway, _basicAuth, _hostTenant);
this._gateway = _gateway;
this._basicAuth = _basicAuth;
this._hostTenant = _hostTenant;
this._userTenant = _userTenant;
this._appName = _appName;
this._appVersion = _appVersion;
(!_basicAuth || !_basicAuth.startsWith("Basic")) &&
(0, utils_1.throwError)("You have to pass the basic authentication header (Basic: <base64encoded login:password> in the constructor. Wrong Passkey in CLI?");
!(0, utils_1.isUrl)(_gateway) && (0, utils_1.throwError)("the gateway must be an URL (e.g. https://gateway.eu1.mindsphere.io");
!_hostTenant && (0, utils_1.throwError)("You have to provide a host tenant");
!_userTenant && (0, utils_1.throwError)("You have to provide a user tenant");
!_appName && (0, utils_1.throwError)("You have to provide the app name");
!_appVersion && (0, utils_1.throwError)("You have to provide the app version");
}
}
exports.TokenManagerAuth = TokenManagerAuth;
//# sourceMappingURL=tokenmanager-auth.js.map