@softvision/webpdf-wsclient-typescript
Version:
A simplified and optimized API client library for the webPDF server
107 lines • 5.38 kB
JavaScript
"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 });
exports.AbstractAuthenticationProvider = void 0;
const material_1 = require("./material");
const rest_1 = require("../rest");
const connection_1 = require("../connection");
const generated_sources_1 = require("../../generated-sources");
const exception_1 = require("../../exception");
const DataFormat_1 = require("../DataFormat");
class AbstractAuthenticationProvider {
constructor(authMaterial, resumeAuthMaterial) {
this.initialAuthMaterial = authMaterial;
this.authMaterial = typeof resumeAuthMaterial !== "undefined" ? resumeAuthMaterial : authMaterial;
this.updating = false;
}
getSession() {
return this.session;
}
getInitialAuthMaterial() {
return this.initialAuthMaterial;
}
getAuthMaterial() {
return this.authMaterial;
}
setAuthMaterial(authMaterial) {
this.authMaterial = authMaterial;
}
refresh(session) {
return __awaiter(this, void 0, void 0, function* () {
if (this.updating || !(0, rest_1.instanceOfRestSession)(session)) {
return this.getAuthMaterial();
}
if (!(0, material_1.instanceOfSessionToken)(this.getAuthMaterial())) {
return yield this.provide(session);
}
try {
this.updating = true;
this.session = session;
let restSession = session;
this.getAuthMaterial().refresh();
let loginOptions = generated_sources_1.LoginOptions.fromJson({
createRefreshToken: true
});
let request = yield connection_1.HttpRestRequest.createRequest(restSession)
.buildRequest(connection_1.HttpMethod.POST, restSession.getURL(AbstractAuthenticationProvider.REFRESH_PATH), JSON.stringify(loginOptions.toJson()), DataFormat_1.DataFormats.JSON.getMimeType());
let token = generated_sources_1.SessionToken.fromJson(yield request.executeRequest());
this.setAuthMaterial(new material_1.WSClientSessionToken(token.token, token.refreshToken, token.expiresIn));
this.updating = false;
}
catch (ex) {
throw new exception_1.AuthResultException(ex);
}
return this.getAuthMaterial();
});
}
login(session) {
return __awaiter(this, void 0, void 0, function* () {
if (this.updating || !(0, rest_1.instanceOfRestSession)(session) || (0, material_1.instanceOfSessionToken)(this.getAuthMaterial())) {
return this.getAuthMaterial();
}
try {
this.updating = true;
this.session = session;
let restSession = session;
let loginOptions = generated_sources_1.LoginOptions.fromJson({
createRefreshToken: true
});
let request = yield connection_1.HttpRestRequest.createRequest(restSession)
.buildRequest(connection_1.HttpMethod.POST, restSession.getURL(AbstractAuthenticationProvider.LOGIN_PATH), JSON.stringify(loginOptions.toJson()), DataFormat_1.DataFormats.JSON.getMimeType(), this.getInitialAuthMaterial());
let token = generated_sources_1.SessionToken.fromJson(yield request.executeRequest());
this.setAuthMaterial(new material_1.WSClientSessionToken(token.token, token.refreshToken, token.expiresIn));
this.updating = false;
}
catch (ex) {
throw new exception_1.AuthResultException(ex);
}
return this.getAuthMaterial();
});
}
provide(session) {
return __awaiter(this, void 0, void 0, function* () {
if (this.updating || !(0, rest_1.instanceOfRestSession)(session)) {
return this.authMaterial;
}
if (!((0, material_1.instanceOfSessionToken)(this.authMaterial))) {
yield this.login(session);
}
if ((0, material_1.instanceOfSessionToken)(this.authMaterial) && (this.authMaterial.isExpired(session.getSessionContext().getSkewTime()))) {
yield this.refresh(session);
}
return this.authMaterial;
});
}
}
exports.AbstractAuthenticationProvider = AbstractAuthenticationProvider;
AbstractAuthenticationProvider.LOGIN_PATH = "authentication/user/login/";
AbstractAuthenticationProvider.REFRESH_PATH = "authentication/user/refresh/";
//# sourceMappingURL=AbstractAuthenticationProvider.js.map