Permalink
Browse files

feat(authService): add getIdToken method to authenticaton and authSer…

…vice
  • Loading branch information...
MartinMason committed Aug 16, 2016
1 parent c51bab2 commit 00a736849998c0fda7acd474950f853c332b7b3b
Showing with 72 additions and 3 deletions.
  1. +17 −2 doc/baseConfig.md
  2. +9 −0 src/authService.js
  3. +15 −1 src/authentication.js
  4. +6 −0 src/baseConfig.js
  5. +10 −0 test/authService.spec.js
  6. +15 −0 test/authentication.spec.js
@@ -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';
@@ -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:
@@ -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
// =====================
@@ -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
*
@@ -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;
@@ -82,6 +83,7 @@ export class Authentication {
}
this.accessToken = null;
this.refreshToken = null;
this.idToken = null;
this.payload = null;
this.exp = null;
@@ -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;
@@ -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 (_) {_;}
@@ -162,6 +175,7 @@ export class Authentication {
return {
accessToken: this.accessToken,
refreshToken: this.refreshToken,
idToken: this.idToken,
payload: this.payload,
exp: this.exp
};
@@ -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
// =====================
@@ -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);
@@ -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);

0 comments on commit 00a7368

Please sign in to comment.