From 0651f001fd6fd890665b1e5b748d0619fa719d60 Mon Sep 17 00:00:00 2001 From: doktordirk Date: Sat, 28 Apr 2018 13:39:23 +0200 Subject: [PATCH] feat(authService): added getIdTokenPayload method --- doc/api_authService.md | 18 +++++++++++++++++- src/authService.js | 11 ++++++++++- src/authentication.js | 23 +++++++++++++++++++---- test/authService.spec.js | 13 +++++++++++++ test/authentication.spec.js | 31 +++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 6 deletions(-) diff --git a/doc/api_authService.md b/doc/api_authService.md index 872a419..675560a 100644 --- a/doc/api_authService.md +++ b/doc/api_authService.md @@ -314,7 +314,23 @@ A `Object` for JWT or `null` for other tokens. #### Example ```js -let isExpired = this.authService.getTokenPayload(); +let payload = this.authService.getTokenPayload(); +``` + +---------- + +### .getIdTokenPayload() + +Gets the current id token payload from storage + +#### Returns + +A `Object` for JWT or `null` for other tokens. + +#### Example + +```js +let payload = this.authService.getIdTokenPayload(); ``` ---------- diff --git a/src/authService.js b/src/authService.js index 3d4ee60..59f5696 100644 --- a/src/authService.js +++ b/src/authService.js @@ -354,7 +354,7 @@ export class AuthService { } /** - * Get payload from tokens + * Get payload from access token * * @returns {{}} Payload for JWT, else null */ @@ -362,6 +362,15 @@ export class AuthService { return this.authentication.getPayload(); } + /** + * Get payload from id token + * + * @returns {{}} Payload for JWT, else null + */ + getIdTokenPayload(): {} { + return this.authentication.getIdPayload(); + } + /** * Request new access token * diff --git a/src/authentication.js b/src/authentication.js index ed85521..8991772 100644 --- a/src/authentication.js +++ b/src/authentication.js @@ -123,6 +123,12 @@ export class Authentication { return this.payload; } + getIdPayload(): {} { + if (!this.responseAnalyzed) this.getDataFromResponse(this.getResponseObject()); + + return this.idPayload; + } + getExp(): number { if (!this.responseAnalyzed) this.getDataFromResponse(this.getResponseObject()); @@ -176,10 +182,8 @@ export class Authentication { this.idToken = null; } - this.payload = null; - try { - this.payload = this.accessToken ? jwtDecode(this.accessToken) : null; - } catch (_) {} // eslint-disable-line no-empty + this.payload = getPayload(this.accessToken); + this.idPayload = getPayload(this.idToken); // get exp either with from jwt or with supplied function this.exp = parseInt((typeof this.config.getExpirationDateFromResponse === 'function' @@ -344,3 +348,14 @@ export class Authentication { } } } + +/* get payload from a token */ +function getPayload(token: string): {} { + let payload = null; + + try { + payload =token ? jwtDecode(token) : null; + } catch (_) {} // eslint-disable-line no-empty + + return payload; +} diff --git a/test/authService.spec.js b/test/authService.spec.js index 2cd6a04..5f01b0b 100644 --- a/test/authService.spec.js +++ b/test/authService.spec.js @@ -608,6 +608,19 @@ describe('AuthService', () => { }); }); + describe('.getIdTokenPayload()', () => { + const container = getContainer(); + const authService = container.get(AuthService); + + it('should return authentication.getIdTokenPayload() result ', () => { + spyOn(authService.authentication, 'getIdPayload').and.returnValue('payload'); + + const payload = authService.getIdTokenPayload(); + + expect(payload).toBe('payload'); + }); + }); + describe('.updateToken()', () => { const container = new Container(); const authService = container.get(AuthService); diff --git a/test/authentication.spec.js b/test/authentication.spec.js index 247f06d..5b486fa 100644 --- a/test/authentication.spec.js +++ b/test/authentication.spec.js @@ -176,6 +176,37 @@ describe('Authentication', () => { }); }); + describe('.getIdPayload()', () => { + const container = new Container(); + const authentication = container.get(Authentication); + + afterEach(() => { + authentication.setResponseObject(null); + }); + + it('Should return null for JWT-like token', () => { + authentication.setResponseObject({token: tokenFuture.jwt, id_token: 'xx.yy.zz'}); + const payload = authentication.idPayload; + + expect(payload).toBe(null); + }); + + it('Should return null for non-JWT-like token', () => { + authentication.setResponseObject({'token': tokenFuture.jwt, id_token: 'some'}); + const payload = authentication.idPayload; + + expect(payload).toBe(null); + }); + + it('Should analyze response first and return payload', () => { + authentication.setResponseObject({token: 'some', id_token: tokenFuture.jwt}); + + const payload = authentication.getIdPayload(); + expect(typeof payload === 'object').toBe(true); + expect(JSON.stringify(payload)).toBe(JSON.stringify(tokenFuture.payload)); + }); + }); + describe('.getExp()', () => { const container = new Container(); const authentication = container.get(Authentication);