Permalink
Browse files

feat(project): name auth appropriatly and refactor

BREAKING CHANGE: AuthService instance renamed to authService and Authentication instance renamed to authentication
  • Loading branch information...
doktordirk committed Apr 2, 2016
1 parent c6af3e1 commit 95259767d5903fe7b3d1dc05efb7ed55163f3c62
Showing with 104 additions and 115 deletions.
  1. +15 −19 src/app.fetch-httpClient.config.js
  2. +43 −44 src/authService.js
  3. +31 −27 src/authUtils.js
  4. +5 −5 src/authentication.js
  5. +5 −5 src/authorizeStep.js
  6. +5 −15 src/popup.js
@@ -17,7 +17,7 @@ export class FetchConfig {
constructor(httpClient, clientConfig, authService, config) {
this.httpClient = httpClient;
this.clientConfig = clientConfig;
this.auth = authService;
this.authService = authService;
this.config = config.current;
}
@@ -27,46 +27,42 @@ export class FetchConfig {
* @return {{request: Function}}
*/
get interceptor() {
let auth = this.auth;
let config = this.config;
let client = this.httpClient;
return {
request(request) {
if (!auth.isAuthenticated() || !config.httpInterceptor) {
request: (request) => {
if (!this.authService.isAuthenticated() || !this.config.httpInterceptor) {
return request;
}
let token = auth.getCurrentToken();
let token = this.authService.getCurrentToken();
if (config.authHeader && config.authToken) {
token = `${config.authToken} ${token}`;
if (this.config.authHeader && this.config.authToken) {
token = `${this.config.authToken} ${token}`;
}
request.headers.set(config.authHeader, token);
request.headers.set(this.config.authHeader, token);
return request;
},
response(response, request) {
response: (response, request) => {
return new Promise((resolve, reject) => {
if (response.ok) {
return resolve(response);
}
if (response.status !== 401) {
return resolve(response);
}
if (!auth.isTokenExpired() || !config.httpInterceptor) {
if (!this.authService.isTokenExpired() || !this.config.httpInterceptor) {
return resolve(response);
}
if (!auth.getRefreshToken()) {
if (!this.authService.getRefreshToken()) {
return resolve(response);
}
auth.updateToken().then(() => {
let token = auth.getCurrentToken();
if (config.authHeader && config.authToken) {
token = `${config.authToken} ${token}`;
this.authService.updateToken().then(() => {
let token = this.authService.getCurrentToken();
if (this.config.authHeader && this.config.authToken) {
token = `${this.config.authToken} ${token}`;
}
request.headers.append('Authorization', token);
return client.fetch(request).then(resolve);
return this.client.fetch(request).then(resolve);
});
});
}
@@ -7,59 +7,60 @@ import {authUtils} from './authUtils';
@inject(Authentication, OAuth1, OAuth2, BaseConfig)
export class AuthService {
constructor(auth, oAuth1, oAuth2, config) {
this.auth = auth;
this.oAuth1 = oAuth1;
this.oAuth2 = oAuth2;
this.config = config.current;
this.client = this.config.client;
this.isRefreshing = false;
constructor(authentication, oAuth1, oAuth2, config) {
this.authentication = authentication;
this.oAuth1 = oAuth1;
this.oAuth2 = oAuth2;
this.config = config.current;
this.client = this.config.client;
this.isRefreshing = false;
}
getMe(criteria) {
if (typeof criteria === 'string' || typeof criteria === 'number') {
criteria = {id: criteria};
}
return this.client.find(this.auth.getProfileUrl(), criteria);
}
getCurrentToken() {
return this.auth.getToken();
}
getRefreshToken() {
return this.auth.getRefreshToken();
return this.client.find(this.authentication.getProfileUrl(), criteria);
}
updateMe(body, criteria) {
if (typeof criteria === 'string' || typeof criteria === 'number') {
criteria = { id: criteria };
}
return this.client.update(this.auth.getProfileUrl(), criteria, body);
return this.client.update(this.authentication.getProfileUrl(), criteria, body);
}
getCurrentToken() {
return this.authentication.getToken();
}
getRefreshToken() {
return this.authentication.getRefreshToken();
}
isAuthenticated() {
let isExpired = this.auth.isTokenExpired();
let isExpired = this.authentication.isTokenExpired();
if (isExpired && this.config.autoUpdateToken) {
if (this.isRefreshing) {
return true;
}
this.updateToken();
}
return this.auth.isAuthenticated();
return this.authentication.isAuthenticated();
}
isTokenExpired() {
return this.auth.isTokenExpired();
return this.authentication.isTokenExpired();
}
getTokenPayload() {
return this.auth.getPayload();
return this.authentication.getPayload();
}
signup(displayName, email, password) {
let signupUrl = this.auth.getSignupUrl();
let signupUrl = this.authentication.getSignupUrl();
let content;
if (typeof arguments[0] === 'object') {
content = arguments[0];
} else {
@@ -72,7 +73,7 @@ export class AuthService {
return this.client.post(signupUrl, content)
.then(response => {
if (this.config.loginOnSignup) {
this.auth.setTokenFromResponse(response);
this.authentication.setTokenFromResponse(response);
} else if (this.config.signupRedirect) {
window.location.href = this.config.signupRedirect;
}
@@ -82,55 +83,53 @@ export class AuthService {
}
login(email, password) {
let loginUrl = this.auth.getLoginUrl();
let config = this.config;
let clientId = this.config.clientId;
let content = {};
if (typeof arguments[1] !== 'string') {
content = arguments[0];
} else {
content = {email: email, password: password};
if (clientId) {
content.client_id = clientId;
if (this.config.clientId) {
content.client_id = this.config.clientId;
}
}
return this.client.post(loginUrl, content)
return this.client.post(this.authentication.getLoginUrl(), content)
.then(response => {
this.auth.setTokenFromResponse(response);
if (config.useRefreshToken) {
this.auth.setRefreshTokenFromResponse(response);
this.authentication.setTokenFromResponse(response);
if (this.config.useRefreshToken) {
this.authentication.setRefreshTokenFromResponse(response);
}
return response;
});
}
logout(redirectUri) {
return this.auth.logout(redirectUri);
return this.authentication.logout(redirectUri);
}
updateToken() {
this.isRefreshing = true;
let loginUrl = this.auth.getLoginUrl();
let refreshToken = this.auth.getRefreshToken();
let clientId = this.config.clientId;
let refreshToken = this.authentication.getRefreshToken();
let content = {};
if (refreshToken) {
content = {grant_type: 'refresh_token', refresh_token: refreshToken};
if (clientId) {
content.client_id = clientId;
if (this.config.clientId) {
content.client_id = this.config.clientId;
}
return this.client.post(loginUrl, content)
return this.client.post(this.authentication.getLoginUrl(), content)
.then(response => {
this.auth.setRefreshToken(response);
this.auth.setToken(response);
this.authentication.setRefreshToken(response);
this.authentication.setToken(response);
this.isRefreshing = false;
return response;
}).catch((err) => {
this.auth.removeToken();
this.auth.removeRefreshToken();
this.authentication.removeToken();
this.authentication.removeRefreshToken();
this.isRefreshing = false;
throw err;
@@ -146,7 +145,7 @@ export class AuthService {
return provider.open(this.config.providers[name], userData || {})
.then(response => {
this.auth.setTokenFromResponse(response, redirect);
this.authentication.setTokenFromResponse(response, redirect);
return response;
});
}
@@ -41,6 +41,34 @@ export class authUtils {
return typeof value !== 'undefined';
}
static isString(value) {
return typeof value === 'string';
}
static isObject(value) {
return value !== null && typeof value === 'object';
}
static isArray = Array.isArray
static isFunction(value) {
return typeof value === 'function';
}
static isBlankObject(value) {
return value !== null && typeof value === 'object' && !Object.getPrototypeOf(value);
}
static isArrayLike(obj) {
if (obj === null || authUtils.isWindow(obj)) {
return false;
}
}
static isWindow(obj) {
return obj && obj.window === obj;
}
static camelCase(name) {
return name.replace(/([\:\-\_]+(.))/g, function(_, separator, letter, offset) {
return offset ? letter.toUpperCase() : letter;
@@ -62,21 +90,11 @@ export class authUtils {
return obj;
}
static isString(value) {
return typeof value === 'string';
}
static isObject(value) {
return value !== null && typeof value === 'object';
}
static isArray = Array.isArray
static isFunction(value) {
return typeof value === 'function';
static addTokenPrefix(prefix = '', tokenNme) {
return prefix ? prefix + _ + tokenNme : tokenNme;
}
static joinUrl(baseUrl, url) {
static joinUrl(baseUrl = '', url = '') {
if (/^(?:[a-z]+:)?\/\//i.test(url)) {
return url;
}
@@ -94,20 +112,6 @@ export class authUtils {
return normalize(joined);
}
static isBlankObject(value) {
return value !== null && typeof value === 'object' && !Object.getPrototypeOf(value);
}
static isArrayLike(obj) {
if (obj === null || authUtils.isWindow(obj)) {
return false;
}
}
static isWindow(obj) {
return obj && obj.window === obj;
}
static extend(dst) {
return baseExtend(dst, slice.call(arguments, 1), false);
}
@@ -11,11 +11,11 @@ export class Authentication {
}
get refreshTokenName() {
return this.config.refreshTokenPrefix ? this.config.refreshTokenPrefix + '_' + this.config.refreshTokenName : this.config.refreshTokenName;
return authUtils.addTokenPrefix(this.config.refreshTokenPrefix, this.config.refreshTokenName);
}
get tokenName() {
return this.config.tokenPrefix ? this.config.tokenPrefix + '_' + this.config.tokenName : this.config.tokenName;
return authUtils.addTokenPrefix(this.config.tokenPrefix, this.config.tokenName);
}
getLoginRoute() {
@@ -27,15 +27,15 @@ export class Authentication {
}
getLoginUrl() {
return this.config.baseUrl ? authUtils.joinUrl(this.config.baseUrl, this.config.loginUrl) : this.config.loginUrl;
return authUtils.joinUrl(this.config.baseUrl, this.config.loginUrl);
}
getSignupUrl() {
return this.config.baseUrl ? authUtils.joinUrl(this.config.baseUrl, this.config.signupUrl) : this.config.signupUrl;
return authUtils.joinUrl(this.config.baseUrl, this.config.signupUrl);
}
getProfileUrl() {
return this.config.baseUrl ? authUtils.joinUrl(this.config.baseUrl, this.config.profileUrl) : this.config.profileUrl;
return authUtils.joinUrl(this.config.baseUrl, this.config.profileUrl);
}
getToken() {
@@ -4,20 +4,20 @@ import {Redirect} from 'aurelia-router';
@inject(Authentication)
export class AuthorizeStep {
constructor(auth) {
this.auth = auth;
constructor(authentication) {
this.authentication = authentication;
}
run(routingContext, next) {
let isLoggedIn = this.auth.isAuthenticated();
let loginRoute = this.auth.getLoginRoute();
let isLoggedIn = this.authentication.isAuthenticated();
let loginRoute = this.authentication.getLoginRoute();
if (routingContext.getAllInstructions().some(i => i.config.auth)) {
if (!isLoggedIn) {
return next.cancel(new Redirect(loginRoute));
}
} else if (isLoggedIn && routingContext.getAllInstructions().some(i => i.fragment === loginRoute)) {
let loginRedirect = this.auth.getLoginRedirect();
let loginRedirect = this.authentication.getLoginRedirect();
return next.cancel(new Redirect(loginRedirect));
}
Oops, something went wrong.

0 comments on commit 9525976

Please sign in to comment.