@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)
83 lines • 3.5 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.CredentialAuth = void 0;
const debug = require("debug");
const cross_fetch_1 = require("cross-fetch");
const auth_base_1 = require("./auth-base");
const utils_1 = require("./utils");
const log = debug("mindconnect-credentialauth");
class CredentialAuth extends auth_base_1.AuthBase {
AcquireToken() {
return __awaiter(this, void 0, void 0, function* () {
const headers = Object.assign(Object.assign({}, this._urlEncodedHeaders), { Authorization: this._basicAuth });
const url = `${(0, utils_1.getPiamUrl)(this._gateway, this._tenant)}oauth/token`;
log(`AcquireToken Headers: ${JSON.stringify(headers)} Url: ${url}`);
const body = "grant_type=client_credentials";
try {
const response = yield (0, cross_fetch_1.default)(url, {
method: "POST",
body: 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 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;
});
}
/**
* Creates an instance of CredentialAuth.
* @param {string} _gateway
* @param {string} _basicAuth
* @param {string} _tenant
*
* @memberOf CredentialAuth
*/
constructor(_gateway, _basicAuth, _tenant) {
super(_gateway, _basicAuth, _tenant);
this._gateway = _gateway;
this._basicAuth = _basicAuth;
this._tenant = _tenant;
}
}
exports.CredentialAuth = CredentialAuth;
//# sourceMappingURL=credential-auth.js.map