groupdocs-conversion-cloud
Version:
GroupDocs.Conversion Cloud SDK for Node.js
97 lines (96 loc) • 4.02 kB
JavaScript
;
/*
* The MIT License (MIT)
*
* Copyright (c) Aspose Pty Ltd
*
* 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.
*/
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.OAuth = void 0;
const jwt = require("jsonwebtoken");
const api_client_1 = require("./api_client");
const qs = require("qs");
/**
* Implements OAuth authentication
*/
class OAuth {
/**
* Apply authentication settings to header and query params
*/
applyToRequest(requestOptions, configuration) {
return __awaiter(this, void 0, void 0, function* () {
if (this.accessToken == null || !this.isTokenValid()) {
yield this._requestToken(configuration);
}
if (requestOptions && requestOptions.headers) {
requestOptions.headers.Authorization = "Bearer " + this.accessToken;
}
return Promise.resolve();
});
}
_requestToken(configuration) {
return __awaiter(this, void 0, void 0, function* () {
const requestOptions = {
method: "post",
headers: { "content-type": "application/x-www-form-urlencoded" },
maxBodyLength: Infinity,
url: configuration.apiBaseUrl + "/connect/token",
data: qs.stringify({
grant_type: "client_credentials",
client_id: configuration.appSid,
client_secret: configuration.appKey,
}),
};
const response = yield (0, api_client_1.invokeApiMethod)(requestOptions, configuration, true);
this.accessToken = response.data.access_token;
return Promise.resolve();
});
}
isTokenValid() {
try {
const decodedToken = jwt.decode(this.accessToken);
if (!decodedToken) {
console.log("Invalid token.");
return;
}
const expirationTime = decodedToken.exp;
const currentTime = Math.floor(Date.now() / 1000);
if (currentTime <= expirationTime) {
return true;
}
else {
return false;
}
}
catch (error) {
return false;
}
}
}
exports.OAuth = OAuth;