From 49fe1e0f2bf145e045cfba168750ded70f35e573 Mon Sep 17 00:00:00 2001 From: doktordirk Date: Wed, 13 Apr 2016 14:13:33 +0200 Subject: [PATCH] feat(project): revert isAuthenticated to just boolean again. use aurelia-logger and @deprecated for deprecations. --- config.js | 8 +++++++- package.json | 13 ++++++++++--- src/authService.js | 36 ++++++++++++++---------------------- src/authentication.js | 26 +++++++++++++++----------- src/baseConfig.js | 15 +++++++++------ test/authService.spec.js | 36 ++++-------------------------------- test/authentication.spec.js | 6 +++--- test/setup.js | 3 +++ 8 files changed, 65 insertions(+), 78 deletions(-) diff --git a/config.js b/config.js index 5017da2..2e15595 100644 --- a/config.js +++ b/config.js @@ -10,7 +10,10 @@ System.config({ "aurelia-api": "npm:aurelia-api@2.2.0", "aurelia-dependency-injection": "npm:aurelia-dependency-injection@1.0.0-beta.1.2.0", "aurelia-fetch-client": "npm:aurelia-fetch-client@1.0.0-beta.1.2.1", - "aurelia-path": "npm:aurelia-path@1.0.0-beta.1.2.0", + "aurelia-logging": "npm:aurelia-logging@1.0.0-beta.1.2.0", + "aurelia-metadata": "npm:aurelia-metadata@1.0.0-beta.1.2.0", + "aurelia-pal-browser": "npm:aurelia-pal-browser@1.0.0-beta.1.2.0", + "aurelia-path": "npm:aurelia-path@1.0.0-beta.1.2.1", "aurelia-polyfills": "npm:aurelia-polyfills@1.0.0-beta.1.1.1", "aurelia-router": "npm:aurelia-router@1.0.0-beta.1.2.0", "extend": "npm:extend@3.0.0", @@ -32,6 +35,9 @@ System.config({ "npm:aurelia-metadata@1.0.0-beta.1.2.0": { "aurelia-pal": "npm:aurelia-pal@1.0.0-beta.1.2.0" }, + "npm:aurelia-pal-browser@1.0.0-beta.1.2.0": { + "aurelia-pal": "npm:aurelia-pal@1.0.0-beta.1.2.0" + }, "npm:aurelia-polyfills@1.0.0-beta.1.1.1": { "aurelia-pal": "npm:aurelia-pal@1.0.0-beta.1.2.0" }, diff --git a/package.json b/package.json index e83cbb1..a0cc8c6 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,9 @@ "aurelia-api": "^2.2.0", "aurelia-dependency-injection": "^1.0.0-beta.1.2.0", "aurelia-fetch-client": "^1.0.0-beta.1.2.1", - "aurelia-path": "^1.0.0-beta.1.2.0", + "aurelia-logging": "^1.0.0-beta.1.2.0", + "aurelia-metadata": "^1.0.0-beta.1.2.0", + "aurelia-path": "^1.0.0-beta.1.2.1", "aurelia-router": "^1.0.0-beta.1.2.0", "extend": "^3.0.0" }, @@ -41,11 +43,14 @@ "aurelia-api": "^2.2.0", "aurelia-dependency-injection": "^1.0.0-beta.1.2.0", "aurelia-fetch-client": "^1.0.0-beta.1.2.1", - "aurelia-path": "^1.0.0-beta.1.2.0", + "aurelia-logging": "^1.0.0-beta.1.2.0", + "aurelia-metadata": "^1.0.0-beta.1.2.0", + "aurelia-path": "^1.0.0-beta.1.2.1", "aurelia-router": "^1.0.0-beta.1.2.0", "extend": "^3.0.0" }, "devDependencies": { + "aurelia-pal-browser": "^1.0.0-beta.1.2.0", "aurelia-polyfills": "^1.0.0-beta.1.1.0", "fetch": "github:github/fetch@^0.11.0" } @@ -54,7 +59,9 @@ "aurelia-api": "^2.2.0", "aurelia-dependency-injection": "^1.0.0-beta.1.2.0", "aurelia-fetch-client": "^1.0.0-beta.1.2.1", - "aurelia-path": "^1.0.0-beta.1.2.0", + "aurelia-logging": "^1.0.0-beta.1.2.0", + "aurelia-metadata": "^1.0.0-beta.1.2.0", + "aurelia-path": "^1.0.0-beta.1.2.1", "aurelia-router": "^1.0.0-beta.1.2.0", "extend": "^3.0.0" }, diff --git a/src/authService.js b/src/authService.js index cb4076e..2bd966b 100644 --- a/src/authService.js +++ b/src/authService.js @@ -1,4 +1,6 @@ import {inject} from 'aurelia-dependency-injection'; +import {deprecated} from 'aurelia-metadata'; + import {Authentication} from './authentication'; import {BaseConfig} from './baseConfig'; @@ -20,8 +22,8 @@ export class AuthService { return this.config.client; } + @deprecated({message: 'Use .authentication instead.'}) get auth() { - console.warn('DEPRECATED: AuthService.auth. Use .authentication instead.'); return this.authentication; } @@ -66,8 +68,8 @@ export class AuthService { return this.authentication.getAccessToken(); } + @deprecated({message: 'Use .getAccessToken() instead.'}) getCurrentToken() { - console.warn('DEPRECATED: AuthService.getCurrentToken(). Use .getAccessToken() instead.'); return this.getAccessToken(); } @@ -82,13 +84,12 @@ export class AuthService { } /** - * Gets authentication status from token. If autoUpdateToken === true, - * updates token and returns true meanwhile + * Gets authentication status * - * @returns {Boolean} true: for Non-JWT tokens and unexpired JWT tokens, false: else + * @returns {Boolean} true: for Non-JWT and unexpired JWT, false: else * */ - isAuthenticated(asPromise) { + isAuthenticated() { let authenticated = this.authentication.isAuthenticated(); // auto-update token? @@ -96,36 +97,27 @@ export class AuthService { && this.config.autoUpdateToken && this.authentication.getAccessToken() && this.authentication.getRefreshToken()) { - authenticated = this.updateToken(); - } - - // return as boolean or Promise - if (asPromise) { - if (authenticated instanceof Promise) return authenticated; - return Promise.resolve(authenticated); + this.updateToken(); + authenticated = true; } - if (authenticated instanceof Promise) { - authenticated.catch(()=>{}).then(Promise.resolve); - return true; - } return authenticated; } /** - * Gets remaining time in seconds + * Gets ttl in seconds * - * @returns {Number} remaing time for JWT tokens, NaN for all other tokesn + * @returns {Number} ttl for JWT tokens, NaN for all other tokens * */ - getTimeLeft() { - return this.authentication.getTimeLeft(); + getTtl() { + return this.authentication.getTtl(); } /** * Gets exp from token payload and compares to current time * - * @returns {Boolean} getTimeLeft>0 time for JWT tokens, undefined other tokesn + * @returns {Boolean} returns (ttl > 0)? for JWT, undefined other tokens * */ isTokenExpired() { diff --git a/src/authentication.js b/src/authentication.js index 246fbde..4c298f4 100644 --- a/src/authentication.js +++ b/src/authentication.js @@ -1,5 +1,9 @@ import {inject} from 'aurelia-dependency-injection'; +import * as LogManager from 'aurelia-logging'; +const logger = LogManager.getLogger('authentication'); +import {deprecated} from 'aurelia-metadata'; + import {BaseConfig} from './baseConfig'; import {Storage} from './storage'; import {OAuth1} from './oAuth1'; @@ -26,6 +30,7 @@ export class Authentication { const oldToken = storage.get(oldStorageKey); if (oldToken) { + logger.info('Found token with deprecated format in storage. Converting it to new format. No further action required.'); let fakeOldResponse = {}; fakeOldResponse[config.accessTokenProp] = oldToken; this.responseObject = fakeOldResponse; @@ -36,33 +41,33 @@ export class Authentication { /* deprecated methods */ + @deprecated({message: 'Use baseConfig.loginRoute instead.'}) getLoginRoute() { - console.warn('DEPRECATED: Authentication.getLoginRoute. Use baseConfig.loginRoute instead.'); return this.config.loginRoute; } + @deprecated({message: 'Use baseConfig.loginRedirect instead.'}) getLoginRedirect() { - console.warn('DEPRECATED: Authentication.getLoginRedirect. Use baseConfig.loginRedirect instead.'); return this.config.loginRedirect; } + @deprecated({message: 'Use baseConfig.withBase(baseConfig.loginUrl) instead.'}) getLoginUrl() { - console.warn('DEPRECATED: Authentication.getLoginUrl. Use baseConfig.withBase(baseConfig.loginUrl) instead.'); return this.config.withBase(this.config.loginUrl); } + @deprecated({message: 'Use baseConfig.withBase(baseConfig.signupUrl) instead.'}) getSignupUrl() { - console.warn('DEPRECATED: Authentication.getSignupUrl. Use baseConfig.withBase(baseConfig.signupUrl) instead.'); return this.config.withBase(this.config.signupUrl); } + @deprecated({message: 'Use baseConfig.withBase(baseConfig.profileUrl) instead.'}) getProfileUrl() { - console.warn('DEPRECATED: Authentication.getProfileUrl. Use baseConfig.withBase(baseConfig.profileUrl) instead.'); return this.config.withBase(this.config.profileUrl); } + @deprecated({message: 'Use .getAccessToken() instead.'}) getToken() { - console.warn('DEPRECATED: Authentication.getToken. Use .getAccessToken() instead.'); return this.getAccessToken(); } @@ -107,13 +112,13 @@ export class Authentication { /* get status from data */ - getTimeLeft() { + getTtl() { const exp = this.getExp(); return Number.isNaN(exp) ? NaN : exp - Math.round(new Date().getTime() / 1000); } isTokenExpired() { - const timeLeft = this.getTimeLeft(); + const timeLeft = this.getTtl(); return Number.isNaN(timeLeft) ? undefined : timeLeft < 0; } @@ -212,7 +217,6 @@ export class Authentication { * @param {[{}]} [userData] * * @return {Promise} - * */ authenticate(name, userData = {}) { const provider = this.config.providers[name].type === '1.0' ? this.oAuth1 : this.oAuth2; @@ -223,12 +227,12 @@ export class Authentication { redirect(redirectUrl, defaultRedirectUrl) { // stupid rule to keep it BC if (redirectUrl === true) { - console.warn('DEPRECATED: Setting redirectUrl === true to actually *not redirect* is deprecated. Set redirectUrl === false instead.'); + logger.warn('DEPRECATED: Setting redirectUrl === true to actually *not redirect* is deprecated. Set redirectUrl === false instead.'); return; } // explicit false means don't redirect if (redirectUrl === false) { - console.warn('BREAKING CHANGE: redirectUrl === false means "Do not redirect" now! Set redirectUrl to undefined or null to use the defaultRedirectUrl if so desired.'); + logger.warn('BREAKING CHANGE: redirectUrl === false means "Do not redirect" now! Set redirectUrl to undefined or null to use the defaultRedirectUrl if so desired.'); return; } if (typeof redirectUrl === 'string') { diff --git a/src/baseConfig.js b/src/baseConfig.js index 2fd26a4..b02e53b 100644 --- a/src/baseConfig.js +++ b/src/baseConfig.js @@ -1,6 +1,9 @@ import {join} from 'aurelia-path'; import extend from 'extend'; +import * as LogManager from 'aurelia-logging'; +const logger = LogManager.getLogger('authentication'); + export class BaseConfig { // prepends baseUrl withBase(url) { @@ -255,12 +258,12 @@ export class BaseConfig { /* deprecated methods and parameteres */ get current() { - console.warn('BaseConfig.current() is deprecated. Use BaseConfig directly instead.'); + logger.warn('BaseConfig.current() is deprecated. Use BaseConfig directly instead.'); return this; } set authToken(authToken) { - console.warn('BaseConfig.authToken is deprecated. Use BaseConfig.authTokenType instead.'); + logger.warn('BaseConfig.authToken is deprecated. Use BaseConfig.authTokenType instead.'); this._authTokenType = authToken; this.authTokenType = authToken; return authToken; @@ -270,7 +273,7 @@ export class BaseConfig { } set responseTokenProp(responseTokenProp) { - console.warn('BaseConfig.responseTokenProp is deprecated. Use BaseConfig.accessTokenProp instead.'); + logger.warn('BaseConfig.responseTokenProp is deprecated. Use BaseConfig.accessTokenProp instead.'); this._responseTokenProp = responseTokenProp; this.accessTokenProp = responseTokenProp; return responseTokenProp; @@ -280,7 +283,7 @@ export class BaseConfig { } set tokenRoot(tokenRoot) { - console.warn('BaseConfig.tokenRoot is deprecated. Use BaseConfig.accessTokenRoot instead.'); + logger.warn('BaseConfig.tokenRoot is deprecated. Use BaseConfig.accessTokenRoot instead.'); this._tokenRoot = tokenRoot; this.accessTokenRoot = tokenRoot; return tokenRoot; @@ -290,7 +293,7 @@ export class BaseConfig { } set tokenName(tokenName) { - console.warn('BaseConfig.tokenName is deprecated. Use BaseConfig.accessTokenName instead.'); + logger.warn('BaseConfig.tokenName is deprecated. Use BaseConfig.accessTokenName instead.'); this._tokenName = tokenName; this.accessTokenName = tokenName; return tokenName; @@ -300,7 +303,7 @@ export class BaseConfig { } set tokenPrefix(tokenPrefix) { - console.warn('BaseConfig.tokenPrefix is obsolete. Use BaseConfig.storageKey instead.'); + logger.warn('BaseConfig.tokenPrefix is obsolete. Use BaseConfig.storageKey instead.'); this._tokenPrefix = tokenPrefix; return tokenPrefix; } diff --git a/test/authService.spec.js b/test/authService.spec.js index eb9e144..ea1f6b9 100644 --- a/test/authService.spec.js +++ b/test/authService.spec.js @@ -178,16 +178,6 @@ describe('AuthService', () => { expect(typeof result).toBe('boolean'); }); - it('should return Promise', done => { - const result = authService.isAuthenticated(true); - - expect(result instanceof Promise).toBe(true); - result.then(authenticated => { - expect(typeof authenticated).toBe('boolean'); - done(); - }); - }); - describe('with autoUpdateToken=true', () => { it('should return boolean true', () => { authService.config.useRefreshToken = true; @@ -202,35 +192,17 @@ describe('AuthService', () => { expect(typeof result).toBe('boolean'); expect(result).toBe(true); }); - - it('should return Promise', done => { - authService.config.useRefreshToken = true; - baseConfig.autoUpdateToken = true; - authService.authentication.responseObject = {token: 'some', refresh_token: 'another'}; - - spyOn(authService, 'updateToken').and.returnValue(Promise.resolve(true)); - spyOn(authentication, 'isAuthenticated').and.returnValue(false); - - const result = authService.isAuthenticated(true); - - expect(result instanceof Promise).toBe(true); - result.then(authenticated => { - expect(typeof authenticated).toBe('boolean'); - expect(authenticated).toBe(true); - done(); - }); - }); }); }); - describe('.getTimeLeft()', () => { + describe('.getTtl()', () => { const container = getContainer(); const authService = container.get(AuthService); - it('should return authentication.getTimeLeft() result', () => { - spyOn(authService.authentication, 'getTimeLeft').and.returnValue('any'); + it('should return authentication.getTtl() result', () => { + spyOn(authService.authentication, 'getTtl').and.returnValue('any'); - const expired = authService.getTimeLeft(); + const expired = authService.getTtl(); expect(expired).toBe('any'); }); diff --git a/test/authentication.spec.js b/test/authentication.spec.js index ed65273..8ce4fe6 100644 --- a/test/authentication.spec.js +++ b/test/authentication.spec.js @@ -165,7 +165,7 @@ describe('Authentication', () => { }); - describe('.getTimeLeft()', () => { + describe('.getTtl()', () => { const container = new Container(); const authentication = container.get(Authentication); @@ -176,7 +176,7 @@ describe('Authentication', () => { it('Should be NaN for Non-JWT', () => { authentication.responseObject = {token: 'some'}; - const timeLeft = authentication.getTimeLeft(); + const timeLeft = authentication.getTtl(); expect(typeof timeLeft === 'number').toBe(true); expect(Number.isNaN(timeLeft)).toBe(true); @@ -185,7 +185,7 @@ describe('Authentication', () => { it('Should be exp-currentTime for JWT', () => { authentication.responseObject = {token: tokenPast.jwt}; - const timeLeft = authentication.getTimeLeft(); + const timeLeft = authentication.getTtl(); expect(typeof timeLeft === 'number').toBe(true); expect(timeLeft).toBe(tokenPast.payload.exp - Math.round(new Date().getTime() / 1000)); }); diff --git a/test/setup.js b/test/setup.js index 11a7f0a..18b1bbd 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,2 +1,5 @@ +import {initialize} from 'aurelia-pal-browser'; import 'aurelia-polyfills'; import 'fetch'; + +initialize();