Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

[aca-4419] add new event once user is logged in #1105

Merged
merged 10 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/alfrescoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ export class AlfrescoApi implements Emitter {
this.processClient = new ProcessClient(this.config);

this.errorListeners();
this.initAuth(config);

if(this.isLoggedIn()){
this.emit('logged-in');
}

return config;
}

private initAuth(config: AlfrescoApiConfig) {
if (this.isOauthConfiguration()) {

if (!this.oauth2Auth) {
Expand All @@ -78,6 +87,10 @@ export class AlfrescoApi implements Emitter {
this.oauth2Auth.setConfig(this.config, this);
}

this.oauth2Auth?.on('logged-in',() => {
this.emit('logged-in');
});

this.setAuthenticationClientECMBPM(this.oauth2Auth.getAuthentication(), this.oauth2Auth.getAuthentication());
} else {

Expand All @@ -87,16 +100,22 @@ export class AlfrescoApi implements Emitter {
this.processAuth.setConfig(this.config);
}

this.processAuth?.on('logged-in',() => {
this.emit('logged-in');
});

if (!this.contentAuth) {
this.contentAuth = new ContentAuth(this.config, this);
} else {
this.contentAuth.setConfig(config);
}

this.contentAuth?.on('logged-in',() => {
this.emit('logged-in');
});

this.setAuthenticationClientECMBPM(this.contentAuth.getAuthentication(), this.processAuth.getAuthentication());
}

return config;
}

private clientsFactory() {
Expand Down Expand Up @@ -144,6 +163,7 @@ export class AlfrescoApi implements Emitter {
}

errorListeners() {

this.contentClient.off('error', () => {
});

Expand Down
2 changes: 2 additions & 0 deletions src/authentication/contentAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class ContentAuth extends AlfrescoApiClient {
this.saveUsername(username);
this.setTicket(data.entry.id);
promise.emit('success');
this.emit('logged-in');
resolve(data.entry.id);
})
.catch((error) => {
Expand Down Expand Up @@ -121,6 +122,7 @@ export class ContentAuth extends AlfrescoApiClient {
this.authApi.validateTicket().then((data: any) => {
this.setTicket(data.entry.id);
promise.emit('success');
this.emit('logged-in');
resolve(data.entry.id);
},
(error) => {
Expand Down
1 change: 1 addition & 0 deletions src/authentication/oauth2Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ export class Oauth2Auth extends AlfrescoApiClient {

if (token) {
this.emit('token_issued');
this.emit('logged-in');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/authentication/oauth2Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Oauth2Config {
secret?: string;
host: string;
scope: string;
implicitFlow: boolean;
implicitFlow?: boolean;
redirectUri: string;
refreshTokenTimeout?: number;
silentLogin?: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/authentication/processAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class ProcessAuth extends AlfrescoApiClient {
let ticket = this.basicAuth( this.authentications.basicAuth.username, this.authentications.basicAuth.password);
this.setTicket(ticket);
promise.emit('success');
this.emit('logged-in');
resolve(ticket);
},
(error) => {
Expand Down
66 changes: 66 additions & 0 deletions test/alfrescoApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { AlfrescoApi } from '../src/alfrescoApi';

const expect = require('chai').expect;
const AuthEcmMock = require('../test/mockObjects/mockAlfrescoApi').Auth;
const AuthBpmMock = require('../test/mockObjects/mockAlfrescoApi').ActivitiMock.Auth;
const Oauth2Mock = require('../test/mockObjects/mockAlfrescoApi').Oauth2Mock.Auth;

describe('Basic configuration test', () => {

Expand Down Expand Up @@ -176,6 +179,69 @@ describe('Basic configuration test', () => {

expect(error).equal('missing username or password');
});

it('Should logged-in be emitted when log in ECM', (done) => {
const hostEcm = 'http://127.0.0.1:8080';

const authEcmMock = new AuthEcmMock(hostEcm);

const alfrescoJsApi = new AlfrescoApi({
hostEcm,
provider: 'ECM'
});

authEcmMock.get201Response();

alfrescoJsApi.on('logged-in', () => {
done();
});

alfrescoJsApi.login('admin', 'admin')
});

it('Should logged-in be emitted when log in BPM', (done) => {
const hostBpm = 'http://127.0.0.1:9999';
const authBpmMock = new AuthBpmMock(hostBpm);

authBpmMock.get200Response();

const alfrescoJsApi = new AlfrescoApi({
hostBpm: hostBpm,
contextRootBpm: 'activiti-app',
provider: 'BPM'
});

alfrescoJsApi.on('logged-in', () => {
done();
});

alfrescoJsApi.login('admin', 'admin')
});

it('Should logged-in be emitted when log in OAUTH', (done) => {
const oauth2Mock = new Oauth2Mock('http://myOauthUrl:30081');

oauth2Mock.get200Response();

const alfrescoJsApi = new AlfrescoApi({
oauth2: {
'host': 'http://myOauthUrl:30081/auth/realms/springboot',
'clientId': 'activiti',
'scope': 'openid',
'secret': '',
'redirectUri': '/',
'redirectUriLogout': '/logout'
},
authType: 'OAUTH'
});

alfrescoJsApi.on('logged-in', () => {
done();
});

alfrescoJsApi.login('admin', 'admin')
});

});

});