Skip to content

Commit

Permalink
[MNT-24449] Validate ticket on config initialization (#9882)
Browse files Browse the repository at this point in the history
* [MNT-24449] Validate ticket on config initialization

* [MNT-24449] Added unit test
  • Loading branch information
tiagosalvado10 authored Jul 9, 2024
1 parent 08baf3e commit 3573df0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/js-api/src/alfrescoApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
this.storage = Storage.getInstance();
this.storage.setDomainPrefix(config.domainPrefix);

this.initConfig(config);
this.validateTicket(config);
}

private initConfig(config: AlfrescoApiConfig) {
this.config = new AlfrescoApiConfig(config);

this.clientsFactory();
Expand All @@ -81,8 +86,26 @@ export class AlfrescoApi implements Emitter, AlfrescoApiType {
this.emitBuffer('logged-in');
}
}
}

return config;
private validateTicket(config: AlfrescoApiConfig) {
if (config.ticketEcm && !this.isOauthConfiguration()) {
if (!this.contentAuth) {
this.contentAuth = new 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');
}
});
}
}

private initAuth(config: AlfrescoApiConfig): void {
Expand Down
20 changes: 20 additions & 0 deletions lib/js-api/test/alfrescoApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ describe('Basic configuration test', () => {
'https://testServer.com:1616/strangeContextRoot/api/-default-/public/alfresco/versions/1'
);
});

it('should detect invalid ticket and unset it', (done) => {
const hostEcm = 'https://127.0.0.1:8080';
const authEcmMock = new EcmAuthMock(hostEcm);

const config = {
hostEcm,
authType: 'BASIC',
ticketEcm: 'wrong-ticket'
};

authEcmMock.get401InvalidTicket();

const alfrescoApi = new AlfrescoApi(config);

alfrescoApi.on('ticket_invalidated', () => {
assert.equal(alfrescoApi.config.ticketEcm, null);
done();
});
});
});

describe('setconfig parameter ', () => {
Expand Down
14 changes: 14 additions & 0 deletions lib/js-api/test/mockObjects/content-services/ecm-auth.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ export class EcmAuthMock extends BaseMock {
.reply(200, { entry: { id: returnMockTicket } });
}

get401InvalidTicket(): void {
nock(this.host, { encodedQueryParams: true })
.get('/alfresco/api/-default-/public/authentication/versions/1/tickets/-me-')
.reply(401, {
error: {
errorKey: 'framework.exception.ApiDefault',
statusCode: 401,
briefSummary: '05210059 Authentication failed for Web Script org/alfresco/api/ResourceWebScript.get',
stackTrace: 'For security reasons the stack trace is no longer displayed, but the property is kept for previous versions.',
descriptionURL: 'https://api-explorer.alfresco.com'
}
});
}

get403Response(): void {
nock(this.host, { encodedQueryParams: true })
.post('/alfresco/api/-default-/public/authentication/versions/1/tickets', {
Expand Down

0 comments on commit 3573df0

Please sign in to comment.