Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(authService): added getIdTokenPayload method
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Apr 28, 2018
1 parent 9fa7eff commit 0651f00
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
18 changes: 17 additions & 1 deletion doc/api_authService.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

----------
Expand Down
11 changes: 10 additions & 1 deletion src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
23 changes: 19 additions & 4 deletions src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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;
}
13 changes: 13 additions & 0 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions test/authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0651f00

Please sign in to comment.