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

Commit

Permalink
feat(project): revert isAuthenticated to just boolean again. use aure…
Browse files Browse the repository at this point in the history
…lia-logger and @deprecated for deprecations.
  • Loading branch information
doktordirk committed Apr 13, 2016
1 parent b2a9a6f commit 49fe1e0
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 78 deletions.
8 changes: 7 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,24 @@
"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"
},
"peerDependencies": {
"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"
}
Expand All @@ -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"
},
Expand Down
36 changes: 14 additions & 22 deletions src/authService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {inject} from 'aurelia-dependency-injection';
import {deprecated} from 'aurelia-metadata';


import {Authentication} from './authentication';
import {BaseConfig} from './baseConfig';
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -82,50 +84,40 @@ 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?
if (!authenticated
&& 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() {
Expand Down
26 changes: 15 additions & 11 deletions src/authentication.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -212,7 +217,6 @@ export class Authentication {
* @param {[{}]} [userData]
*
* @return {Promise<response>}
*
*/
authenticate(name, userData = {}) {
const provider = this.config.providers[name].type === '1.0' ? this.oAuth1 : this.oAuth2;
Expand All @@ -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') {
Expand Down
15 changes: 9 additions & 6 deletions src/baseConfig.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
36 changes: 4 additions & 32 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,6 @@ describe('AuthService', () => {
expect(typeof result).toBe('boolean');
});

it('should return Promise<boolean>', 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;
Expand All @@ -202,35 +192,17 @@ describe('AuthService', () => {
expect(typeof result).toBe('boolean');
expect(result).toBe(true);
});

it('should return Promise<true>', 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');
});
Expand Down
6 changes: 3 additions & 3 deletions test/authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('Authentication', () => {
});


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

Expand All @@ -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);
Expand All @@ -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));
});
Expand Down
3 changes: 3 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
import {initialize} from 'aurelia-pal-browser';
import 'aurelia-polyfills';
import 'fetch';

initialize();

0 comments on commit 49fe1e0

Please sign in to comment.