Skip to content

Commit c7e4a15

Browse files
jann-ccjonkoops
authored andcommitted
feat(keycloak-service): reject instead of forcing login
Add forceLogin flag to KeycloakService.getToken Closes #178
1 parent 2f3c561 commit c7e4a15

3 files changed

Lines changed: 45 additions & 12 deletions

File tree

projects/keycloak-angular/src/lib/core/interceptors/keycloak-bearer.interceptor.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('KeycloakBearerInterceptor', () => {
2626
});
2727
});
2828

29-
it('Should be created', inject(
29+
it('should be created', inject(
3030
[KeycloakBearerInterceptor],
3131
(service: KeycloakBearerInterceptor) => {
3232
expect(service).toBeTruthy();

projects/keycloak-angular/src/lib/core/services/keycloak.service.spec.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ describe('KeycloakService', () => {
1717
});
1818
});
1919

20-
it('Should be created', inject(
20+
it('should be created', inject(
2121
[KeycloakService],
2222
(service: KeycloakService) => {
2323
expect(service).toBeTruthy();
2424
}
2525
));
2626

2727
describe('#loadExcludedUrls', () => {
28-
it('Should create the ExcludedUrlRegex objects if the bearerExcludedUrls arg is a string array', inject(
28+
it('should create the ExcludedUrlRegex objects if the bearerExcludedUrls arg is a string array', inject(
2929
[KeycloakService],
3030
(service: KeycloakService) => {
3131
const loadExcludedUrls = service['loadExcludedUrls'];
@@ -39,7 +39,7 @@ describe('KeycloakService', () => {
3939
}
4040
));
4141

42-
it('Should create the ExcludedUrlRegex objects if the bearerExcludedUrls arg is an mixed array of strings and ExcludedUrl objects', inject(
42+
it('should create the ExcludedUrlRegex objects if the bearerExcludedUrls arg is an mixed array of strings and ExcludedUrl objects', inject(
4343
[KeycloakService],
4444
(service: KeycloakService) => {
4545
const loadExcludedUrls = service['loadExcludedUrls'];
@@ -63,5 +63,33 @@ describe('KeycloakService', () => {
6363
expect(excludedRegex2.httpMethods[0]).toBe('GET');
6464
}
6565
));
66+
67+
it('should not call login in error case if getToken is called with forceLogin false', inject(
68+
[KeycloakService],
69+
async (service: KeycloakService) => {
70+
service.updateToken = async () => Promise.reject(new Error('test'));
71+
spyOn(service, 'login');
72+
73+
let error: Error;
74+
await service.getToken(false).catch(e => error = e);
75+
76+
expect(error).toBeDefined();
77+
expect(service.login).not.toHaveBeenCalled();
78+
}
79+
));
80+
81+
it('should return correct token in success case if getToken is called with forceLogin false', inject(
82+
[KeycloakService],
83+
async (service: KeycloakService) => {
84+
service.updateToken = async () => Promise.resolve(true);
85+
(service['_instance'] as any) = {
86+
token: 'testToken'
87+
};
88+
89+
const token = await service.getToken(false);
90+
91+
expect(token).toEqual('testToken');
92+
}
93+
));
6694
});
6795
});

projects/keycloak-angular/src/lib/core/services/keycloak.service.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,20 +455,25 @@ export class KeycloakService {
455455

456456
/**
457457
* Returns the authenticated token, calling updateToken to get a refreshed one if
458-
* necessary. If the session is expired this method calls the login method for a new login.
458+
* necessary. If the session is expired and the forceLogin flag is set to true,
459+
* this method calls the login method for a new login, otherwise rejects.
459460
*
461+
* @param forceLogin
462+
* Flag whether a login should be enforced if the session is expired.
460463
* @returns
461464
* Promise with the generated token.
462465
*/
463-
getToken(): Promise<string> {
464-
return new Promise(async (resolve, reject) => {
465-
try {
466-
await this.updateToken(10);
467-
resolve(this._instance.token);
468-
} catch (error) {
466+
async getToken(forceLogin = true): Promise<string> {
467+
try {
468+
await this.updateToken(10);
469+
return this._instance.token;
470+
} catch (error) {
471+
if (forceLogin) {
469472
this.login();
473+
} else {
474+
throw error;
470475
}
471-
});
476+
}
472477
}
473478

474479
/**

0 commit comments

Comments
 (0)