@alfresco/js-api
Version:
JavaScript client library for the Alfresco REST API
151 lines • 5.68 kB
JavaScript
"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.ProcessAuth = void 0;
const tslib_1 = require("tslib");
const event_emitter_1 = tslib_1.__importDefault(require("event-emitter"));
const alfrescoApiClient_1 = require("../alfrescoApiClient");
const storage_1 = require("../storage");
const utils_1 = require("../utils");
class ProcessAuth extends alfrescoApiClient_1.AlfrescoApiClient {
constructor(config, httpClient) {
super(undefined, httpClient);
this.authentications = {
basicAuth: { ticket: '' },
type: 'activiti'
};
this.storage = storage_1.Storage.getInstance();
this.storage.setDomainPrefix(config.domainPrefix);
this.className = 'ProcessAuth';
if (!(0, utils_1.isBrowser)()) {
this.defaultHeaders = {
'user-agent': 'alfresco-js-api'
};
}
this.setConfig(config);
}
setConfig(config) {
this.config = config;
this.ticket = undefined;
this.basePath = config.hostBpm + '/' + this.config.contextRootBpm;
if (this.config.ticketBpm) {
this.setTicket(config.ticketBpm);
}
else if (this.storage.getItem('ticket-BPM')) {
this.setTicket(this.storage.getItem('ticket-BPM'));
}
}
changeHost() {
this.basePath = this.config.hostBpm + '/' + this.config.contextRootBpm;
this.ticket = undefined;
}
changeCsrfConfig(disableCsrf) {
this.config.disableCsrf = disableCsrf;
}
saveUsername(username) {
if (this.storage.supportsStorage()) {
this.storage.setItem('APS_USERNAME', username);
}
}
login(username, password) {
this.authentications.basicAuth.username = username;
this.authentications.basicAuth.password = password;
const headerParams = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
};
const formParams = {
j_username: this.authentications.basicAuth.username,
j_password: this.authentications.basicAuth.password,
_spring_security_remember_me: true,
submit: 'Login'
};
const contentTypes = ['application/x-www-form-urlencoded'];
const accepts = ['application/json'];
const promise = new Promise((resolve, reject) => {
this.callApi('/app/authentication', 'POST', {}, {}, headerParams, formParams, {}, contentTypes, accepts).then(() => {
this.saveUsername(username);
const ticket = this.basicAuth(this.authentications.basicAuth.username, this.authentications.basicAuth.password);
this.setTicket(ticket);
promise.emit('success');
this.emit('logged-in');
resolve(ticket);
}, (error) => {
this.saveUsername('');
if (error.status === 401) {
promise.emit('unauthorized');
}
else if (error.status === 403) {
promise.emit('forbidden');
}
else {
promise.emit('error');
}
reject(error);
});
});
(0, event_emitter_1.default)(promise);
return promise;
}
logout() {
this.saveUsername('');
const contentTypes = ['application/json'];
const accepts = ['application/json'];
const promise = new Promise((resolve, reject) => {
this.callApi('/app/logout', 'GET', {}, {}, {}, {}, {}, contentTypes, accepts).then(() => {
this.invalidateSession();
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;
}
setTicket(ticket) {
this.authentications.basicAuth.ticket = ticket;
this.authentications.basicAuth.password = null;
this.config.ticketBpm = ticket;
this.storage.setItem('ticket-BPM', ticket);
this.ticket = ticket;
}
invalidateSession() {
this.storage.removeItem('ticket-BPM');
this.authentications.basicAuth.ticket = null;
this.authentications.basicAuth.password = null;
this.authentications.basicAuth.username = null;
this.config.ticketBpm = null;
this.ticket = null;
}
getTicket() {
return this.ticket;
}
isLoggedIn() {
return !!this.ticket;
}
getAuthentication() {
return this.authentications;
}
}
exports.ProcessAuth = ProcessAuth;
//# sourceMappingURL=../../../../../lib/js-api/src/authentication/processAuth.js.map