Permalink
Browse files

feat(authService): added getIdTokenPayload method

  • Loading branch information...
doktordirk committed Apr 28, 2018
1 parent 9fa7eff commit 0651f001fd6fd890665b1e5b748d0619fa719d60
Showing with 90 additions and 6 deletions.
  1. +17 −1 doc/api_authService.md
  2. +10 −1 src/authService.js
  3. +19 −4 src/authentication.js
  4. +13 −0 test/authService.spec.js
  5. +31 −0 test/authentication.spec.js
@@ -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();
```
----------
@@ -354,14 +354,23 @@ export class AuthService {
}
/**
* Get payload from tokens
* Get payload from access token
*
* @returns {{}} Payload for JWT, else null
*/
getTokenPayload(): {} {
return this.authentication.getPayload();
}
/**
* Get payload from id token
*
* @returns {{}} Payload for JWT, else null
*/
getIdTokenPayload(): {} {
return this.authentication.getIdPayload();
}
/**
* Request new access token
*
@@ -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;
}
@@ -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);
@@ -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);

0 comments on commit 0651f00

Please sign in to comment.