This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
bpmAuth.js
184 lines (157 loc) · 5.3 KB
/
bpmAuth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
var AlfrescoApiClient = require('./alfrescoApiClient');
var Emitter = require('event-emitter');
class BpmAuth extends AlfrescoApiClient {
/**
* @param {Object} config
*/
constructor(config) {
super();
this.className = 'BpmAuth';
this.config = config;
this.ticket = undefined;
this.basePath = config.hostBpm + '/' + this.config.contextRootBpm; //Activiti Call
this.authentications = {
'basicAuth': {type: 'activiti', ticket: ''}
};
if (this.config.ticketBpm) {
this.setTicket(config.ticketBpm);
}
Emitter.call(this);
}
changeHost() {
this.basePath = this.config.hostBpm + '/' + this.config.contextRootBpm; //Activiti Call
this.ticket = undefined;
}
changeCsrfConfig(disableCsrf) {
this.config.disableCsrf = disableCsrf;
}
saveUsername(username) {
if (this.storage.supportsStorage()) {
this.storage.setItem('APS_USERNAME', username);
}
}
/**
* login Activiti API
* @param {String} username: // Username to login
* @param {String} password: // Password to login
*
* @returns {Promise} A promise that returns {new authentication ticket} if resolved and {error} if rejected.
* */
login(username, password) {
this.username = username;
this.authentications.basicAuth.username = username;
this.authentications.basicAuth.password = password;
var postBody = {}, pathParams = {}, queryParams = {};
var headerParams = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache'
};
var formParams = {
j_username: this.authentications.basicAuth.username,
j_password: this.authentications.basicAuth.password,
_spring_security_remember_me: true,
submit: 'Login'
};
var authNames = [];
var contentTypes = ['application/x-www-form-urlencoded'];
var accepts = ['application/json'];
this.promise = new Promise((resolve, reject) => {
this.callApi(
'/app/authentication', 'POST',
pathParams, queryParams, headerParams, formParams, postBody,
authNames, contentTypes, accepts
).then(
(data) => {
this.saveUsername(username);
var ticket = 'Basic ' + new Buffer(this.authentications.basicAuth.username + ':' + this.authentications.basicAuth.password).toString('base64');
this.setTicket(ticket);
this.promise.emit('success');
resolve(ticket);
},
(error) => {
this.saveUsername('');
if (error.status === 401) {
this.promise.emit('unauthorized');
} else if (error.status === 403) {
this.promise.emit('forbidden');
} else {
this.promise.emit('error');
}
reject(error);
});
});
Emitter(this.promise); // jshint ignore:line
return this.promise;
}
/**
* logout Alfresco API
*
* @returns {Promise} A promise that returns {new authentication ticket} if resolved and {error} if rejected.
* */
logout() {
this.username = '';
this.saveUsername('');
var postBody = {}, pathParams = {}, queryParams = {}, headerParams = {}, formParams = {};
var authNames = [];
var contentTypes = ['application/json'];
var accepts = ['application/json'];
this.promise = new Promise((resolve, reject) => {
this.callApi(
'/app/logout', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
authNames, contentTypes, accepts
).then(
() => {
this.promise.emit('logout');
this.setTicket(undefined);
resolve('logout');
},
(error) => {
if (error.status === 401) {
this.promise.emit('unauthorized');
}
this.promise.emit('error');
reject(error);
});
});
Emitter(this.promise); // jshint ignore:line
return this.promise;
}
/**
* Set the current Ticket
*
* @param {String} Ticket
* */
setTicket(ticket) {
this.authentications.basicAuth.ticket = ticket;
this.authentications.basicAuth.password = null;
this.ticket = ticket;
}
/**
* Get the current Ticket
*
* @returns {String} Ticket
* */
getTicket() {
return this.ticket;
}
/**
* If the client is logged in retun true
*
* @returns {Boolean} is logged in
*/
isLoggedIn() {
return !!this.ticket;
}
/**
* return the Authentication
*
* @returns {Object} authentications
* */
getAuthentication() {
return this.authentications;
}
}
Emitter(BpmAuth.prototype); // jshint ignore:line
module.exports = BpmAuth;