Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 17d5239

Browse files
authored
Merge pull request #66 from Alfresco/development
Development
2 parents 70674a1 + bfbbad0 commit 17d5239

File tree

10 files changed

+126
-13
lines changed

10 files changed

+126
-13
lines changed

CHANGELOG.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@ Alfresco JS API
66

77
_This project provides a JavaScript client API into the v1 Alfresco REST API_
88

9-
<a name="0.3.2"></a>
10-
# [0.3.3](https://github.com/Alfresco/alfresco-js-api/releases/tag/0.3.2) (2016-09-26)
9+
<a name="0.3.5"></a>
10+
# [0.3.5](https://github.com/Alfresco/alfresco-js-api/releases/tag/0.3.5) (2016-09-26)
11+
12+
## Fix
13+
- [Library no longer works with ECM](https://github.com/Alfresco/alfresco-js-api/issues/63)
14+
- [Flag to enable/disable CSRF behaviour](https://github.com/Alfresco/alfresco-js-api/issues/62)
15+
16+
<a name="0.3.4"></a>
17+
# [0.3.4](https://github.com/Alfresco/alfresco-js-api/releases/tag/0.3.4) (2016-09-26)
18+
19+
## Fix
20+
- [csrf token for activiti doesn't work with Node.js](https://github.com/Alfresco/alfresco-js-api/issues/61)
21+
22+
<a name="0.3.3"></a>
23+
# [0.3.3](https://github.com/Alfresco/alfresco-js-api/releases/tag/0.3.3) (2016-09-26)
1124

1225
## Fix
1326
- [Add csrf token for activiti](https://github.com/Alfresco/alfresco-js-api/issues/59)
@@ -72,14 +85,14 @@ Separation between constructor and login phase, decoupling login from constructo
7285
Before:
7386

7487
```javascript
75-
this.alfrescoJsApi = new AlfrescoApi({username, password, host, contextRoot, ticket});
88+
this.alfrescoJsApi = new AlfrescoApi({username, password, alfrescoHost, contextRoot, ticket});
7689
this.alfrescoJsApi.login();
7790
```
7891

7992
After:
8093

8194
```javascript
82-
this.alfrescoJsApi = new AlfrescoApi({hostECM, hostBPM, contextRoot, ticketEcm, ticketBpm});
95+
this.alfrescoJsApi = new AlfrescoApi({hostECM, hostBPM, contextRoot, ticket});
8396
this.alfrescoJsApi.login(username, password);
8497
```
8598

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ hostBpm| (Optional value The Ip or Name of the host where your Activiti instance
121121
contextRoot| (Optional value that define the context Root of the API default value is alfresco )|alfresco |
122122
provider| (Optional value default value is ECM. This parameter can accept as value ECM BPM or ALL to use the API and Login in the ECM, Activiti BPM or Both )|alfresco |
123123
ticket| (Optional only if you want login with the ticket see example below)| |
124+
disableCsrf| To disable CSRF Token to be submitted. Only for Activiti call.| false |
124125

125126
### Login with Username and Password BPM and ECM
126127

dist/alfresco-js-api.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72265,6 +72265,7 @@ var AlfrescoApi = function () {
7226572265
* provider: // ECM BPM ALL, default ECM
7226672266
* ticketEcm: // Ticket if you already have a ECM ticket you can pass only the ticket and skip the login, in this case you don't need username and password
7226772267
* ticketBpm: // Ticket if you already have a BPM ticket you can pass only the ticket and skip the login, in this case you don't need username and password
72268+
* disableCsrf: // To disable CSRF Token to be submitted. Only for Activiti call, by default is false.
7226872269
* };
7226972270
*/
7227072271
function AlfrescoApi(config) {
@@ -72280,7 +72281,8 @@ var AlfrescoApi = function () {
7228072281
contextRoot: config.contextRoot || 'alfresco',
7228172282
provider: config.provider || 'ECM',
7228272283
ticketEcm: config.ticketEcm,
72283-
ticketBpm: config.ticketBpm
72284+
ticketBpm: config.ticketBpm,
72285+
disableCsrf: config.disableCsrf || false
7228472286
};
7228572287

7228672288
this.bpmAuth = new BpmAuth(this.config);
@@ -72292,6 +72294,12 @@ var AlfrescoApi = function () {
7229272294
}
7229372295

7229472296
_createClass(AlfrescoApi, [{
72297+
key: 'changeCsrfConfig',
72298+
value: function changeCsrfConfig(disableCsrf) {
72299+
this.config.disableCsrf = disableCsrf;
72300+
this.bpmAuth.changeCsrfConfig(disableCsrf);
72301+
}
72302+
}, {
7229572303
key: 'changeEcmHost',
7229672304
value: function changeEcmHost(hostEcm) {
7229772305
this.config.hostEcm = hostEcm;
@@ -72670,7 +72678,9 @@ var AlfrescoApiClient = function (_ApiClient) {
7267072678
// set header parameters
7267172679
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
7267272680

72673-
this.setCsrfToken(request);
72681+
if (this.isBpmRequest() && this.isCsrfEnabled()) {
72682+
this.setCsrfToken(request);
72683+
}
7267472684

7267572685
// set request timeout
7267672686
request.timeout(this.timeout);
@@ -72773,11 +72783,27 @@ var AlfrescoApiClient = function (_ApiClient) {
7277372783

7277472784
return this.promise;
7277572785
}
72786+
}, {
72787+
key: 'isBpmRequest',
72788+
value: function isBpmRequest() {
72789+
return this.constructor.name === 'BpmAuth';
72790+
}
72791+
}, {
72792+
key: 'isCsrfEnabled',
72793+
value: function isCsrfEnabled() {
72794+
if (this.config) {
72795+
return !this.config.disableCsrf;
72796+
} else {
72797+
return true;
72798+
}
72799+
}
7277672800
}, {
7277772801
key: 'setCsrfToken',
7277872802
value: function setCsrfToken(request) {
7277972803
var token = this.token();
7278072804
request.set('X-CSRF-TOKEN', token);
72805+
request.set('Cookie', 'CSRF-TOKEN=' + token + ';path=/');
72806+
7278172807
try {
7278272808
document.cookie = 'CSRF-TOKEN=' + token + ';path=/';
7278372809
} catch (err) {}
@@ -73223,6 +73249,11 @@ var BpmAuth = function (_AlfrescoApiClient) {
7322373249
this.config.hostBpm = host;
7322473250
this.basePath = this.config.hostBpm + '/activiti-app'; //Activiti Call
7322573251
}
73252+
}, {
73253+
key: 'changeCsrfConfig',
73254+
value: function changeCsrfConfig(disableCsrf) {
73255+
this.config.disableCsrf = disableCsrf;
73256+
}
7322673257

7322773258
/**
7322873259
* login Activiti API

dist/alfresco-js-api.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "alfresco-js-api",
3-
"version": "0.3.3",
3+
"version": "0.3.5",
44
"description": "JavaScript client library for the Alfresco REST API",
55
"main": "main.js",
66
"typings": "dist/alfresco-js-api.d.ts",

src/alfrescoApi.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class AlfrescoApi {
2424
* provider: // ECM BPM ALL, default ECM
2525
* ticketEcm: // Ticket if you already have a ECM ticket you can pass only the ticket and skip the login, in this case you don't need username and password
2626
* ticketBpm: // Ticket if you already have a BPM ticket you can pass only the ticket and skip the login, in this case you don't need username and password
27+
* disableCsrf: // To disable CSRF Token to be submitted. Only for Activiti call, by default is false.
2728
* };
2829
*/
2930
constructor(config) {
@@ -38,7 +39,8 @@ class AlfrescoApi {
3839
contextRoot: config.contextRoot || 'alfresco',
3940
provider: config.provider || 'ECM',
4041
ticketEcm: config.ticketEcm,
41-
ticketBpm: config.ticketBpm
42+
ticketBpm: config.ticketBpm,
43+
disableCsrf: config.disableCsrf || false
4244
};
4345

4446
this.bpmAuth = new BpmAuth(this.config);
@@ -49,6 +51,11 @@ class AlfrescoApi {
4951
Emitter.call(this);
5052
}
5153

54+
changeCsrfConfig(disableCsrf) {
55+
this.config.disableCsrf = disableCsrf;
56+
this.bpmAuth.changeCsrfConfig(disableCsrf);
57+
}
58+
5259
changeEcmHost(hostEcm) {
5360
this.config.hostEcm = hostEcm;
5461
this.ecmAuth.changeHost(hostEcm);

src/alfrescoApiClient.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ class AlfrescoApiClient extends ApiClient {
5858
// set header parameters
5959
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
6060

61-
this.setCsrfToken(request);
61+
if (this.isBpmRequest() && this.isCsrfEnabled()) {
62+
this.setCsrfToken(request);
63+
}
6264

6365
// set request timeout
6466
request.timeout(this.timeout);
@@ -161,9 +163,23 @@ class AlfrescoApiClient extends ApiClient {
161163
return this.promise;
162164
}
163165

166+
isBpmRequest() {
167+
return this.constructor.name === 'BpmAuth';
168+
}
169+
170+
isCsrfEnabled() {
171+
if (this.config) {
172+
return !this.config.disableCsrf;
173+
}else {
174+
return true;
175+
}
176+
}
177+
164178
setCsrfToken(request) {
165179
var token = this.token();
166180
request.set('X-CSRF-TOKEN', token);
181+
request.set('Cookie', 'CSRF-TOKEN=' + token + ';path=/');
182+
167183
try {
168184
document.cookie = 'CSRF-TOKEN=' + token + ';path=/';
169185
} catch (err) {

src/bpmAuth.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class BpmAuth extends AlfrescoApiClient {
2626
this.basePath = this.config.hostBpm + '/activiti-app'; //Activiti Call
2727
}
2828

29+
changeCsrfConfig(disableCsrf) {
30+
this.config.disableCsrf = disableCsrf;
31+
}
32+
2933
/**
3034
* login Activiti API
3135
* @param {String} username: // Username to login

test/bpmAuth.spec.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/*global describe, it, beforeEach */
1+
/*global describe, it, beforeEach, afterEach */
22

33
var BpmAuth = require('../src/bpmAuth');
44
var AuthBpmMock = require('../test/mockObjects/mockAlfrescoApi').ActivitiMock.Auth;
55
var expect = require('chai').expect;
6+
var sinon = require('sinon');
67

78
describe('Bpm Auth test', function () {
89

@@ -159,5 +160,43 @@ describe('Bpm Auth test', function () {
159160
});
160161

161162
});
163+
164+
describe('CSRF Token', function () {
165+
166+
beforeEach(function() {
167+
this.setCsrfTokenStub = sinon.stub(BpmAuth.prototype, 'setCsrfToken');
168+
});
169+
170+
afterEach(function() {
171+
this.setCsrfTokenStub.restore();
172+
});
173+
174+
it('should be enabled by default', function (done) {
175+
this.authBpmMock.get200Response();
176+
177+
this.bpmAuth = new BpmAuth({
178+
hostBpm: this.hostBpm
179+
});
180+
181+
this.bpmAuth.login('admin', 'admin').then(() => {
182+
expect(this.setCsrfTokenStub.called).to.be.equal(true);
183+
done();
184+
});
185+
});
186+
187+
it('should be disabled if disableCsrf is true', function (done) {
188+
this.authBpmMock.get200Response();
189+
190+
this.bpmAuth = new BpmAuth({
191+
hostBpm: this.hostBpm,
192+
disableCsrf: true
193+
});
194+
195+
this.bpmAuth.login('admin', 'admin').then(() => {
196+
expect(this.setCsrfTokenStub.called).to.be.equal(false);
197+
done();
198+
});
199+
});
200+
});
162201
});
163202
});

typescript/alfresco-js-api.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ export class AlfrescoApiConfig {
545545
provider: string;
546546
ticketEcm: string;
547547
ticketBpm: string;
548+
disableCsrf: boolean;
548549
}
549550

550551
export interface ContentApi {
@@ -604,6 +605,7 @@ export interface AlfrescoJsApi {
604605

605606
changeEcmHost(ecmHost: string);
606607
changeBpmHost(bpmHost: string);
608+
changeCsrfConfig(disableCsrf: boolean);
607609

608610
getNodeInfo(nodeId: string): Promise<MinimalNodeEntryEntity>;
609611
deleteNode(nodeId: string): any;

0 commit comments

Comments
 (0)