UNPKG

@alfresco/js-api

Version:

JavaScript client library for the Alfresco REST API

501 lines 19.5 kB
"use strict"; /*! * @license * Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.AlfrescoApi = void 0; const tslib_1 = require("tslib"); const event_emitter_1 = tslib_1.__importDefault(require("event-emitter")); const contentAuth_1 = require("./authentication/contentAuth"); const processAuth_1 = require("./authentication/processAuth"); const oauth2Auth_1 = require("./authentication/oauth2Auth"); const contentClient_1 = require("./contentClient"); const processClient_1 = require("./processClient"); const storage_1 = require("./storage"); const alfrescoApiConfig_1 = require("./alfrescoApiConfig"); class AlfrescoApi { constructor(config, httpClient) { this.httpClient = httpClient; this.__type = 'legacy-client'; this.bufferEvents = []; (0, event_emitter_1.default)(this); if (config) { this.setConfig(config); } } setConfig(config) { if (!config) { config = {}; } this.storage = storage_1.Storage.getInstance(); this.storage.setDomainPrefix(config.domainPrefix); this.initConfig(config); this.validateTicket(config); } initConfig(config) { this.config = new alfrescoApiConfig_1.AlfrescoApiConfig(config); this.clientsFactory(); this.errorListeners(); this.ticketMismatchListeners(); if (this.config.oauthInit) { this.initAuth(config); if (this.isLoggedIn()) { this.emitBuffer('logged-in'); } } } validateTicket(config) { if (config.ticketEcm && !this.isOauthConfiguration()) { if (!this.contentAuth) { this.contentAuth = new contentAuth_1.ContentAuth(this.config, this, this.httpClient); } this.contentAuth .validateTicket() .then((ticket) => { config.ticketEcm = ticket; }) .catch((error) => { if (error.status === 401) { config.ticketEcm = null; this.initConfig(config); this.emitBuffer('ticket_invalidated'); } }); } } initAuth(config) { var _a, _b, _c, _d, _e; if (this.isOauthConfiguration()) { if (!this.oauth2Auth) { this.oauth2Auth = new oauth2Auth_1.Oauth2Auth(this.config, this, this.httpClient); } else { this.oauth2Auth.setConfig(this.config, this); } (_a = this.oauth2Auth) === null || _a === void 0 ? void 0 : _a.on('logged-in', () => { this.emitBuffer('logged-in'); }); this.setAuthenticationClientECMBPM(this.oauth2Auth.getAuthentication(), this.oauth2Auth.getAuthentication()); } else { if (!this.processAuth) { this.processAuth = new processAuth_1.ProcessAuth(this.config, this.httpClient); } else { this.processAuth.setConfig(this.config); } (_b = this.processAuth) === null || _b === void 0 ? void 0 : _b.on('logged-in', () => { this.emitBuffer('logged-in'); }); (_c = this.processAuth) === null || _c === void 0 ? void 0 : _c.on('ticket_mismatch', (error) => { this.ticketMismatchHandler(error); }); if (!this.contentAuth) { this.contentAuth = new contentAuth_1.ContentAuth(this.config, this, this.httpClient); } else { this.contentAuth.setConfig(config); } (_d = this.contentAuth) === null || _d === void 0 ? void 0 : _d.on('logged-in', () => { this.emitBuffer('logged-in'); }); (_e = this.contentAuth) === null || _e === void 0 ? void 0 : _e.on('ticket_mismatch', (error) => { this.ticketMismatchHandler(error); }); this.setAuthenticationClientECMBPM(this.contentAuth.getAuthentication(), this.processAuth.getAuthentication()); } } clientsFactory() { if (!this.contentPrivateClient) { this.contentPrivateClient = new contentClient_1.ContentClient(this.config, `/api/${this.config.tenant}/private/alfresco/versions/1`, this.httpClient); } else { this.contentPrivateClient.setConfig(this.config, `/api/${this.config.tenant}/private/alfresco/versions/1`); } if (!this.contentClient) { this.contentClient = new contentClient_1.ContentClient(this.config, `/api/${this.config.tenant}/public/alfresco/versions/1`, this.httpClient); } else { this.contentClient.setConfig(this.config, `/api/${this.config.tenant}/public/alfresco/versions/1`); } if (!this.authClient) { this.authClient = new contentClient_1.ContentClient(this.config, `/api/${this.config.tenant}/public/authentication/versions/1`, this.httpClient); } else { this.authClient.setConfig(this.config, `/api/${this.config.tenant}/public/authentication/versions/1`); } if (!this.searchClient) { this.searchClient = new contentClient_1.ContentClient(this.config, `/api/${this.config.tenant}/public/search/versions/1`, this.httpClient); } else { this.searchClient.setConfig(this.config, `/api/${this.config.tenant}/public/search/versions/1`); } if (!this.discoveryClient) { this.discoveryClient = new contentClient_1.ContentClient(this.config, `/api`, this.httpClient); } else { this.discoveryClient.setConfig(this.config, `/api`); } if (!this.gsClient) { this.gsClient = new contentClient_1.ContentClient(this.config, `/api/${this.config.tenant}/public/gs/versions/1`, this.httpClient); } else { this.gsClient.setConfig(this.config, `/api/${this.config.tenant}/public/gs/versions/1`); } if (!this.processClient) { this.processClient = new processClient_1.ProcessClient(this.config, this.httpClient); } else { this.processClient.setConfig(this.config); } if (!this.hxiConnectorClient) { this.hxiConnectorClient = new contentClient_1.ContentClient(this.config, `/api/${this.config.tenant}/private/hxi/versions/1`, this.httpClient); } else { this.hxiConnectorClient.setConfig(this.config, `/api/${this.config.tenant}/private/hxi/versions/1`); } } errorListeners() { this.contentClient.off('error', () => { }); this.authClient.off('error', () => { }); this.contentPrivateClient.off('error', () => { }); this.processClient.off('error', () => { }); this.searchClient.off('error', () => { }); this.discoveryClient.off('error', () => { }); this.gsClient.off('error', () => { }); this.hxiConnectorClient.off('error', () => { }); this.contentClient.on('error', (error) => { this.errorHandler(error); }); this.authClient.on('error', (error) => { this.errorHandler(error); }); this.contentPrivateClient.on('error', (error) => { this.errorHandler(error); }); this.processClient.on('error', (error) => { this.errorHandler(error); }); this.searchClient.on('error', (error) => { this.errorHandler(error); }); this.discoveryClient.on('error', (error) => { this.errorHandler(error); }); this.gsClient.on('error', (error) => { this.errorHandler(error); }); this.hxiConnectorClient.on('error', (error) => { this.errorHandler(error); }); } ticketMismatchListeners() { var _a, _b, _c, _d; (_a = this.contentClient) === null || _a === void 0 ? void 0 : _a.off('ticket_mismatch', () => { }); (_b = this.processClient) === null || _b === void 0 ? void 0 : _b.off('ticket_mismatch', () => { }); (_c = this.contentClient) === null || _c === void 0 ? void 0 : _c.on('ticket_mismatch', (error) => { this.ticketMismatchHandler(error); }); (_d = this.processClient) === null || _d === void 0 ? void 0 : _d.on('ticket_mismatch', (error) => { this.ticketMismatchHandler(error); }); } errorHandler(error) { if (this.config.oauthInit && error.status === 401) { this.invalidateSession(); } this.emitBuffer('error', error); } ticketMismatchHandler(error) { if (error.newTicket) { this.config.ticketEcm = error.newTicket; this.initConfig(this.config); } this.emitBuffer('ticket_mismatch', error); } changeWithCredentialsConfig(withCredentials) { this.config.withCredentials = withCredentials; } changeCsrfConfig(disableCsrf) { this.config.disableCsrf = disableCsrf; this.processAuth.changeCsrfConfig(disableCsrf); } changeEcmHost(hostEcm) { this.config.hostEcm = hostEcm; this.contentAuth.changeHost(); this.contentClient.changeHost(); this.authClient.changeHost(); } changeBpmHost(hostBpm) { this.config.hostBpm = hostBpm; this.processAuth.changeHost(); this.processClient.changeHost(); } login(username, password) { if (!this.isCredentialValid(username) || !this.isCredentialValid(password)) { return Promise.reject(new Error('missing username or password')); } if (username) { username = username.trim(); } this.username = username; if (this.isOauthConfiguration()) { const promise = this.oauth2Auth.login(username, password); promise.then((accessToken) => { this.config.accessToken = accessToken; }); return promise; } else { if (this.isBpmConfiguration()) { const promise = this.processAuth.login(username, password); promise.then((ticketBpm) => { this.config.ticketBpm = ticketBpm; }); return promise; } else if (this.isEcmConfiguration()) { const promise = this.contentAuth.login(username, password); promise.then((ticketEcm) => { this.setAuthenticationClientECMBPM(this.contentAuth.getAuthentication(), null); this.config.ticketEcm = ticketEcm; }); return promise; } else if (this.isEcmBpmConfiguration()) { const contentProcessPromise = this.loginBPMECM(username, password); contentProcessPromise.then((data) => { this.config.ticketEcm = data[0]; this.config.ticketBpm = data[1]; }); return contentProcessPromise; } else { return Promise.reject(new Error('Unknown configuration')); } } } isCredentialValid(credential) { return credential !== undefined && credential !== null && credential !== ''; } implicitLogin() { if (!this.isOauthConfiguration()) { return Promise.reject(new Error('Missing the required oauth2 configuration')); } return new Promise(() => { this.oauth2Auth.implicitLogin(); }); } setAuthenticationClientECMBPM(authECM, authBPM) { this.contentClient.setAuthentications(authECM); this.authClient.setAuthentications(authECM); this.searchClient.setAuthentications(authECM); this.contentPrivateClient.setAuthentications(authECM); this.processClient.setAuthentications(authBPM); this.searchClient.setAuthentications(authECM); this.discoveryClient.setAuthentications(authECM); this.gsClient.setAuthentications(authECM); this.hxiConnectorClient.setAuthentications(authECM); } loginTicket(ticketEcm, ticketBpm) { this.config.ticketEcm = ticketEcm; this.config.ticketBpm = ticketBpm; return this.contentAuth.validateTicket(); } loginBPMECM(username, password) { const contentPromise = this.contentAuth.login(username, password); const processPromise = this.processAuth.login(username, password); const promise = new Promise((resolve, reject) => { Promise.all([contentPromise, processPromise]).then((data) => { promise.emit('success'); resolve(data); }, (error) => { this.contentAuth.invalidateSession(); this.processAuth.invalidateSession(); if (error.status === 401) { promise.emit('unauthorized'); } promise.emit('error'); reject(error); }); }); (0, event_emitter_1.default)(promise); return promise; } logout() { this.username = null; if (this.isOauthConfiguration()) { return this.oauth2Auth.logOut(); } if (this.isBpmConfiguration()) { return this.processAuth.logout(); } if (this.isEcmConfiguration()) { const contentPromise = this.contentAuth.logout(); contentPromise.then(() => (this.config.ticket = undefined), () => { }); return contentPromise; } if (this.isEcmBpmConfiguration()) { return this._logoutBPMECM(); } return Promise.resolve(); } _logoutBPMECM() { const contentPromise = this.contentAuth.logout(); const processPromise = this.processAuth.logout(); const promise = new Promise((resolve, reject) => { Promise.all([contentPromise, processPromise]).then(() => { this.config.ticket = undefined; promise.emit('logout'); resolve(); }, (error) => { if (error.status === 401) { promise.emit('unauthorized'); } promise.emit('error'); reject(error); }); }); (0, event_emitter_1.default)(promise); return promise; } isLoggedIn() { if (this.isOauthConfiguration()) { return this.oauth2Auth.isLoggedIn(); } if (this.isBpmConfiguration()) { return this.processAuth.isLoggedIn(); } if (this.isEcmConfiguration()) { return this.config.withCredentials ? true : this.contentAuth.isLoggedIn(); } if (this.isEcmBpmConfiguration()) { return this.config.withCredentials ? true : this.contentAuth.isLoggedIn() && this.processAuth.isLoggedIn(); } return false; } isBpmLoggedIn() { if (this.isBpmConfiguration() || this.isEcmBpmConfiguration()) { if (this.isOauthConfiguration()) { return this.oauth2Auth.isLoggedIn(); } else { return this.processAuth.isLoggedIn(); } } return false; } isEcmLoggedIn() { if (this.isEcmConfiguration() || this.isEcmBpmConfiguration()) { if (this.isOauthConfiguration()) { return this.oauth2Auth.isLoggedIn(); } else { return this.config.withCredentials ? true : this.contentAuth.isLoggedIn(); } } return false; } getBpmUsername() { if (this.isOauthConfiguration()) { return this.username || this.oauth2Auth.storage.getItem('USERNAME'); } else { return this.username || this.processAuth.storage.getItem('APS_USERNAME'); } } getEcmUsername() { if (this.isOauthConfiguration()) { return this.username || this.oauth2Auth.storage.getItem('USERNAME'); } else { return this.username || this.contentAuth.storage.getItem('ACS_USERNAME'); } } refreshToken() { if (!this.isOauthConfiguration()) { return Promise.reject(new Error('Missing the required oauth2 configuration')); } if (this.config.oauth2.implicitFlow) { return Promise.reject(new Error('Manual refresh token not possible in implicit flow')); } return this.oauth2Auth.refreshToken(); } getTicketAuth() { var _a; return (_a = this.oauth2Auth) === null || _a === void 0 ? void 0 : _a.getToken(); } setTicket(ticketEcm, ticketBpm) { if (this.contentAuth) { this.contentAuth.setTicket(ticketEcm); } if (this.processAuth) { this.processAuth.setTicket(ticketBpm); } } invalidateSession() { if (this.oauth2Auth) { this.oauth2Auth.invalidateSession(); } else { this.contentAuth.invalidateSession(); this.processAuth.invalidateSession(); } } getTicketBpm() { var _a; return (_a = this.processAuth) === null || _a === void 0 ? void 0 : _a.getTicket(); } getTicketEcm() { var _a; return (_a = this.contentAuth) === null || _a === void 0 ? void 0 : _a.getTicket(); } getTicket() { return [this.contentAuth.getTicket(), this.processAuth.getTicket()]; } isBpmConfiguration() { return this.config.provider && this.config.provider.toUpperCase() === 'BPM'; } isEcmConfiguration() { return this.config.provider && this.config.provider.toUpperCase() === 'ECM'; } isOauthConfiguration() { return this.config.authType === 'OAUTH'; } isPublicUrl() { if (this.isOauthConfiguration()) { return this.oauth2Auth.isPublicUrl(); } return false; } isEcmBpmConfiguration() { return this.config.provider && this.config.provider.toUpperCase() === 'ALL'; } emitBuffer(event, callback) { this.emit(event, callback); this.bufferEvents.push(event); } reply(event, callback) { if (this.bufferEvents.indexOf(event) >= 0) { Function.prototype.apply.call(callback, this, arguments); } else { this.on(event, callback); } } } exports.AlfrescoApi = AlfrescoApi; //# sourceMappingURL=../../../../lib/js-api/src/alfrescoApi.js.map