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

Commit

Permalink
feat(authService): add getIdToken method to authenticaton and authSer…
Browse files Browse the repository at this point in the history
…vice
  • Loading branch information
MartinMason committed Aug 16, 2016
1 parent c51bab2 commit 00a7368
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
19 changes: 17 additions & 2 deletions doc/baseConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ unlinkMethod = 'get';
authHeader = 'Authorization';
// The token name used in the header of API requests that require authentication
authTokenType = 'Bearer';
// The the property from which to get the access token after a successful login or signup
// The property from which to get the access token after a successful login or signup
accessTokenProp = 'access_token';


Expand All @@ -80,7 +80,7 @@ useRefreshToken = false;
autoUpdateToken = true;
// Oauth Client Id
clientId = false;
// The the property from which to get the refresh token after a successful token refresh
// The property from which to get the refresh token after a successful token refresh
refreshTokenProp = 'refresh_token';

// If the property defined by `refreshTokenProp` is an object:
Expand All @@ -92,6 +92,21 @@ refreshTokenName = 'token';
refreshTokenRoot = false;


// Id Token Options
// =====================

// The property from which to get the id token after a successful login
idTokenProp = 'id_token';

// If the property defined by `idTokenProp` is an object:
// -----------------------------------------------------------

// This is the property from which to get the token `{ "idTokenProp": { "idTokenName" : '...' } }`
idTokenName = 'token';
// This allows the id token to be a further object deeper `{ "idTokenProp": { "idTokenRoot" : { "idTokenName" : '...' } } }`
idTokenRoot = false;


// Miscellaneous Options
// =====================

Expand Down
9 changes: 9 additions & 0 deletions src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ export class AuthService {
return this.authentication.getRefreshToken();
}

/**
* Get idToken from storage
*
* @returns {String} Current idToken
*/
getIdToken() {
return this.authentication.getIdToken();
}

/**
* Gets authentication status
*
Expand Down
16 changes: 15 additions & 1 deletion src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Authentication {
this.updateTokenCallstack = [];
this.accessToken = null;
this.refreshToken = null;
this.idToken = null;
this.payload = null;
this.exp = null;
this.hasDataStored = false;
Expand Down Expand Up @@ -82,6 +83,7 @@ export class Authentication {
}
this.accessToken = null;
this.refreshToken = null;
this.idToken = null;
this.payload = null;
this.exp = null;

Expand All @@ -103,6 +105,11 @@ export class Authentication {
return this.refreshToken;
}

getIdToken() {
if (!this.hasDataStored) this.getDataFromResponse(this.getResponseObject());
return this.idToken;
}

getPayload() {
if (!this.hasDataStored) this.getDataFromResponse(this.getResponseObject());
return this.payload;
Expand Down Expand Up @@ -149,8 +156,14 @@ export class Authentication {
}
}

this.payload = null;
this.idToken = null;
try {
this.idToken = this.getTokenFromResponse(response, config.idTokenProp, config.idTokenName, config.idTokenRoot);
} catch (e) {
this.idToken = null;
}

this.payload = null;
try {
this.payload = this.accessToken ? jwtDecode(this.accessToken) : null;
} catch (_) {_;}
Expand All @@ -162,6 +175,7 @@ export class Authentication {
return {
accessToken: this.accessToken,
refreshToken: this.refreshToken,
idToken: this.idToken,
payload: this.payload,
exp: this.exp
};
Expand Down
6 changes: 6 additions & 0 deletions src/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export class BaseConfig {
// This allows the refresh token to be a further object deeper `{ "refreshTokenProp": { "refreshTokenRoot" : { "refreshTokenName" : '...' } } }`
refreshTokenRoot = false;

// The property name from which to get the user authentication token. Can also be dotted idTokenProp.idTokenName
idTokenProp = 'id_token';
// This is the property from which to get the id token `{ "idTokenProp": { "idTokenName" : '...' } }`
idTokenName = 'token';
// This allows the id_token to be a further object deeper `{ "idTokenProp": { "idTokenRoot" : { "idTokenName" : '...' } } }`
idTokenRoot = false;

// Miscellaneous Options
// =====================
Expand Down
10 changes: 10 additions & 0 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,16 @@ describe('AuthService', () => {
});
});

describe('.getIdToken()', () => {
const container = getContainer();
const authService = container.get(AuthService);

it('should return authentication.idToken', () => {
authService.setResponseObject({token: 'some', id_token: 'another'});
expect(authService.getIdToken()).toBe('another');
});
});

describe('.isAuthenticated()', () => {
const container = getContainer();
const authService = container.get(AuthService);
Expand Down
15 changes: 15 additions & 0 deletions test/authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ describe('Authentication', () => {
});
});

describe('.getIdToken()', () => {
const container = new Container();
const authentication = container.get(Authentication);

afterEach(() => {
authentication.setResponseObject(null);
});

it('Should analyze response first and return idToken', () => {
authentication.setResponseObject({access_token: 'some', id_token: 'another'});

expect(authentication.getIdToken()).toBe('another');
});
});

describe('.getPayload()', () => {
const container = new Container();
const authentication = container.get(Authentication);
Expand Down

0 comments on commit 00a7368

Please sign in to comment.