From 4709e573f6abc59469f16997b75654e8b826477e Mon Sep 17 00:00:00 2001 From: Kevin Ball Date: Thu, 26 Oct 2017 11:17:06 -0700 Subject: [PATCH 1/6] Don't reference window in default options if it doesn't exist --- src/options.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/options.js b/src/options.js index 5bdf5ef..f733473 100644 --- a/src/options.js +++ b/src/options.js @@ -13,7 +13,7 @@ export default { storageType: 'localStorage', storageNamespace: 'vue-authenticate', cookieStorage: { - domain: window.location.hostname, + domain: typeof(window) === 'undefined' ? '' : window.location.hostname, path: '/', secure: false }, @@ -55,7 +55,7 @@ export default { name: 'facebook', url: '/auth/facebook', authorizationEndpoint: 'https://www.facebook.com/v2.5/dialog/oauth', - redirectUri: window.location.origin + '/', + redirectUri: typeof(window) === 'undefined' ? '/' : window.location.origin + '/', requiredUrlParams: ['display', 'scope'], scope: ['email'], scopeDelimiter: ',', @@ -68,7 +68,7 @@ export default { name: 'google', url: '/auth/google', authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth', - redirectUri: window.location.origin, + redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, requiredUrlParams: ['scope'], optionalUrlParams: ['display'], scope: ['profile', 'email'], @@ -83,7 +83,7 @@ export default { name: 'github', url: '/auth/github', authorizationEndpoint: 'https://github.com/login/oauth/authorize', - redirectUri: window.location.origin, + redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, optionalUrlParams: ['scope'], scope: ['user:email'], scopeDelimiter: ' ', @@ -95,7 +95,7 @@ export default { name: 'instagram', url: '/auth/instagram', authorizationEndpoint: 'https://api.instagram.com/oauth/authorize', - redirectUri: window.location.origin, + redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, requiredUrlParams: ['scope'], scope: ['basic'], scopeDelimiter: '+', @@ -107,7 +107,7 @@ export default { name: 'twitter', url: '/auth/twitter', authorizationEndpoint: 'https://api.twitter.com/oauth/authenticate', - redirectUri: window.location.origin, + redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, oauthType: '1.0', popupOptions: { width: 495, height: 645 } }, @@ -116,7 +116,7 @@ export default { name: 'bitbucket', url: '/auth/bitbucket', authorizationEndpoint: 'https://bitbucket.org/site/oauth2/authorize', - redirectUri: window.location.origin + '/', + redirectUri: typeof(window) === 'undefined' ? '/' : window.location.origin + '/', optionalUrlParams: ['scope'], scope: ['email'], scopeDelimiter: ' ', @@ -128,7 +128,7 @@ export default { name: 'linkedin', url: '/auth/linkedin', authorizationEndpoint: 'https://www.linkedin.com/oauth/v2/authorization', - redirectUri: window.location.origin, + redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, requiredUrlParams: ['state'], scope: ['r_emailaddress'], scopeDelimiter: ' ', @@ -141,7 +141,7 @@ export default { name: 'live', url: '/auth/live', authorizationEndpoint: 'https://login.live.com/oauth20_authorize.srf', - redirectUri: window.location.origin, + redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, requiredUrlParams: ['display', 'scope'], scope: ['wl.emails'], scopeDelimiter: ' ', @@ -154,7 +154,7 @@ export default { name: null, url: '/auth/oauth1', authorizationEndpoint: null, - redirectUri: window.location.origin, + redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, oauthType: '1.0', popupOptions: null }, @@ -163,7 +163,7 @@ export default { name: null, url: '/auth/oauth2', clientId: null, - redirectUri: window.location.origin, + redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, authorizationEndpoint: null, defaultUrlParams: ['response_type', 'client_id', 'redirect_uri'], requiredUrlParams: null, From 77976c8017226f832345576b3bf9a4e575057090 Mon Sep 17 00:00:00 2001 From: Kevin Ball Date: Mon, 30 Oct 2017 13:42:14 -0700 Subject: [PATCH 2/6] Refactor cookie domain url --- src/options.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/options.js b/src/options.js index f733473..f7b1a29 100644 --- a/src/options.js +++ b/src/options.js @@ -1,3 +1,13 @@ +import { isUndefined } from './utils.js'; + + +function getCookieDomain() { + return isUndefined(window) ? '' : `${window.location.hostname}`; +} + +function getCookieDomainUrl(path = '') { + return isUndefined(window) ? path : `${window.location.origin}${path}`; +} /** * Default configuration */ @@ -13,7 +23,7 @@ export default { storageType: 'localStorage', storageNamespace: 'vue-authenticate', cookieStorage: { - domain: typeof(window) === 'undefined' ? '' : window.location.hostname, + domain: getCookieDomain(), path: '/', secure: false }, @@ -55,7 +65,7 @@ export default { name: 'facebook', url: '/auth/facebook', authorizationEndpoint: 'https://www.facebook.com/v2.5/dialog/oauth', - redirectUri: typeof(window) === 'undefined' ? '/' : window.location.origin + '/', + redirectUri: getCookieDomainUrl('/'), requiredUrlParams: ['display', 'scope'], scope: ['email'], scopeDelimiter: ',', @@ -68,7 +78,7 @@ export default { name: 'google', url: '/auth/google', authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth', - redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, + redirectUri: getCookieDomainUrl(), requiredUrlParams: ['scope'], optionalUrlParams: ['display'], scope: ['profile', 'email'], @@ -83,7 +93,7 @@ export default { name: 'github', url: '/auth/github', authorizationEndpoint: 'https://github.com/login/oauth/authorize', - redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, + redirectUri: getCookieDomainUrl(), optionalUrlParams: ['scope'], scope: ['user:email'], scopeDelimiter: ' ', @@ -95,7 +105,7 @@ export default { name: 'instagram', url: '/auth/instagram', authorizationEndpoint: 'https://api.instagram.com/oauth/authorize', - redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, + redirectUri: getCookieDomainUrl(), requiredUrlParams: ['scope'], scope: ['basic'], scopeDelimiter: '+', @@ -107,7 +117,7 @@ export default { name: 'twitter', url: '/auth/twitter', authorizationEndpoint: 'https://api.twitter.com/oauth/authenticate', - redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, + redirectUri: getCookieDomainUrl(), oauthType: '1.0', popupOptions: { width: 495, height: 645 } }, @@ -116,7 +126,7 @@ export default { name: 'bitbucket', url: '/auth/bitbucket', authorizationEndpoint: 'https://bitbucket.org/site/oauth2/authorize', - redirectUri: typeof(window) === 'undefined' ? '/' : window.location.origin + '/', + redirectUri: getCookieDomainUrl('/'), optionalUrlParams: ['scope'], scope: ['email'], scopeDelimiter: ' ', @@ -128,7 +138,7 @@ export default { name: 'linkedin', url: '/auth/linkedin', authorizationEndpoint: 'https://www.linkedin.com/oauth/v2/authorization', - redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, + redirectUri: getCookieDomainUrl(), requiredUrlParams: ['state'], scope: ['r_emailaddress'], scopeDelimiter: ' ', @@ -141,7 +151,7 @@ export default { name: 'live', url: '/auth/live', authorizationEndpoint: 'https://login.live.com/oauth20_authorize.srf', - redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, + redirectUri: getCookieDomainUrl(), requiredUrlParams: ['display', 'scope'], scope: ['wl.emails'], scopeDelimiter: ' ', @@ -154,7 +164,7 @@ export default { name: null, url: '/auth/oauth1', authorizationEndpoint: null, - redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, + redirectUri: getCookieDomainUrl(), oauthType: '1.0', popupOptions: null }, @@ -163,7 +173,7 @@ export default { name: null, url: '/auth/oauth2', clientId: null, - redirectUri: typeof(window) === 'undefined' ? '' : window.location.origin, + redirectUri: getCookieDomainUrl(), authorizationEndpoint: null, defaultUrlParams: ['response_type', 'client_id', 'redirect_uri'], requiredUrlParams: null, From ba3bf2c9165651d1b5c870329cb00a236cb5d308 Mon Sep 17 00:00:00 2001 From: Kevin Ball Date: Thu, 2 Nov 2017 09:16:10 -0700 Subject: [PATCH 3/6] Update to more appropriate name for redirectUri function --- src/options.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/options.js b/src/options.js index f7b1a29..dcd5044 100644 --- a/src/options.js +++ b/src/options.js @@ -5,7 +5,7 @@ function getCookieDomain() { return isUndefined(window) ? '' : `${window.location.hostname}`; } -function getCookieDomainUrl(path = '') { +function getRedirectUri(path = '') { return isUndefined(window) ? path : `${window.location.origin}${path}`; } /** @@ -65,7 +65,7 @@ export default { name: 'facebook', url: '/auth/facebook', authorizationEndpoint: 'https://www.facebook.com/v2.5/dialog/oauth', - redirectUri: getCookieDomainUrl('/'), + redirectUri: getRedirectUri('/'), requiredUrlParams: ['display', 'scope'], scope: ['email'], scopeDelimiter: ',', @@ -78,7 +78,7 @@ export default { name: 'google', url: '/auth/google', authorizationEndpoint: 'https://accounts.google.com/o/oauth2/auth', - redirectUri: getCookieDomainUrl(), + redirectUri: getRedirectUri(), requiredUrlParams: ['scope'], optionalUrlParams: ['display'], scope: ['profile', 'email'], @@ -93,7 +93,7 @@ export default { name: 'github', url: '/auth/github', authorizationEndpoint: 'https://github.com/login/oauth/authorize', - redirectUri: getCookieDomainUrl(), + redirectUri: getRedirectUri(), optionalUrlParams: ['scope'], scope: ['user:email'], scopeDelimiter: ' ', @@ -105,7 +105,7 @@ export default { name: 'instagram', url: '/auth/instagram', authorizationEndpoint: 'https://api.instagram.com/oauth/authorize', - redirectUri: getCookieDomainUrl(), + redirectUri: getRedirectUri(), requiredUrlParams: ['scope'], scope: ['basic'], scopeDelimiter: '+', @@ -117,7 +117,7 @@ export default { name: 'twitter', url: '/auth/twitter', authorizationEndpoint: 'https://api.twitter.com/oauth/authenticate', - redirectUri: getCookieDomainUrl(), + redirectUri: getRedirectUri(), oauthType: '1.0', popupOptions: { width: 495, height: 645 } }, @@ -126,7 +126,7 @@ export default { name: 'bitbucket', url: '/auth/bitbucket', authorizationEndpoint: 'https://bitbucket.org/site/oauth2/authorize', - redirectUri: getCookieDomainUrl('/'), + redirectUri: getRedirectUri('/'), optionalUrlParams: ['scope'], scope: ['email'], scopeDelimiter: ' ', @@ -138,7 +138,7 @@ export default { name: 'linkedin', url: '/auth/linkedin', authorizationEndpoint: 'https://www.linkedin.com/oauth/v2/authorization', - redirectUri: getCookieDomainUrl(), + redirectUri: getRedirectUri(), requiredUrlParams: ['state'], scope: ['r_emailaddress'], scopeDelimiter: ' ', @@ -151,7 +151,7 @@ export default { name: 'live', url: '/auth/live', authorizationEndpoint: 'https://login.live.com/oauth20_authorize.srf', - redirectUri: getCookieDomainUrl(), + redirectUri: getRedirectUri(), requiredUrlParams: ['display', 'scope'], scope: ['wl.emails'], scopeDelimiter: ' ', @@ -164,7 +164,7 @@ export default { name: null, url: '/auth/oauth1', authorizationEndpoint: null, - redirectUri: getCookieDomainUrl(), + redirectUri: getRedirectUri(), oauthType: '1.0', popupOptions: null }, @@ -173,7 +173,7 @@ export default { name: null, url: '/auth/oauth2', clientId: null, - redirectUri: getCookieDomainUrl(), + redirectUri: getRedirectUri(), authorizationEndpoint: null, defaultUrlParams: ['response_type', 'client_id', 'redirect_uri'], requiredUrlParams: null, From 3435bf9c40bc5a2d11d0f0fb2512eec704e7e662 Mon Sep 17 00:00:00 2001 From: Kevin Ball Date: Thu, 2 Nov 2017 14:20:35 -0700 Subject: [PATCH 4/6] Refactor cookie & redirect utils into utils and use for src/storage/cookie-storage --- src/options.js | 9 +-------- src/storage/cookie-storage.js | 5 +++-- src/utils.js | 22 +++++++++++++++------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/options.js b/src/options.js index dcd5044..380e3c5 100644 --- a/src/options.js +++ b/src/options.js @@ -1,13 +1,6 @@ -import { isUndefined } from './utils.js'; +import { getCookieDomain, getRedirectUri } from './utils.js'; -function getCookieDomain() { - return isUndefined(window) ? '' : `${window.location.hostname}`; -} - -function getRedirectUri(path = '') { - return isUndefined(window) ? path : `${window.location.origin}${path}`; -} /** * Default configuration */ diff --git a/src/storage/cookie-storage.js b/src/storage/cookie-storage.js index 4d76e12..a263602 100644 --- a/src/storage/cookie-storage.js +++ b/src/storage/cookie-storage.js @@ -1,13 +1,14 @@ import { objectExtend, formatCookie, + getCookieDomain, parseCookies } from '../utils.js'; class CookieStorage { constructor(defaultOptions) { this._defaultOptions = objectExtend({ - domain: window.location.hostname, + domain: getCookieDomain(), expires: null, path: '/', secure: false @@ -46,4 +47,4 @@ class CookieStorage { } } -export default CookieStorage \ No newline at end of file +export default CookieStorage diff --git a/src/utils.js b/src/utils.js index 883999d..5815dab 100644 --- a/src/utils.js +++ b/src/utils.js @@ -77,10 +77,10 @@ export function objectExtend(a, b) { /** * Assemble url from two segments - * + * * @author Sahat Yalkabov * @copyright Method taken from https://github.com/sahat/satellizer - * + * * @param {String} baseUrl Base url * @param {String} url URI * @return {String} @@ -102,10 +102,10 @@ export function joinUrl(baseUrl, url) { /** * Get full path based on current location - * + * * @author Sahat Yalkabov * @copyright Method taken from https://github.com/sahat/satellizer - * + * * @param {Location} location * @return {String} */ @@ -118,10 +118,10 @@ export function getFullUrlPath(location) { /** * Parse query string variables - * + * * @author Sahat Yalkabov * @copyright Method taken from https://github.com/sahat/satellizer - * + * * @param {String} Query string * @return {String} */ @@ -143,7 +143,7 @@ export function parseQueryString(str) { * Decode base64 string * @author Sahat Yalkabov * @copyright Method taken from https://github.com/sahat/satellizer - * + * * @param {String} str base64 encoded string * @return {Object} */ @@ -244,3 +244,11 @@ export function formatCookie(key, value, options) { formatOptions(options) ].join(''); }; + +export function getCookieDomain() { + return isUndefined(window) ? '' : `${window.location.hostname}`; +} + +export function getRedirectUri(path = '') { + return isUndefined(window) ? path : `${window.location.origin}${path}`; +} From 8d398d4c1608b311671ee22e32150a66c1dfbea5 Mon Sep 17 00:00:00 2001 From: Kevin Ball Date: Thu, 2 Nov 2017 14:47:24 -0700 Subject: [PATCH 5/6] Update to use typeof directly --- src/utils.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils.js b/src/utils.js index 5815dab..d27f1d2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -246,9 +246,10 @@ export function formatCookie(key, value, options) { }; export function getCookieDomain() { - return isUndefined(window) ? '' : `${window.location.hostname}`; + // Directly check typeof as going through isUndefined seems to break in server environment + return typeof window === 'undefined' ? '' : `${window.location.hostname}`; } export function getRedirectUri(path = '') { - return isUndefined(window) ? path : `${window.location.origin}${path}`; + return typeof window === 'undefined' ? path : `${window.location.origin}${path}`; } From 3794148e8fd0938c65e80d58a9eb6cf214ce196e Mon Sep 17 00:00:00 2001 From: Kevin Ball Date: Fri, 10 Nov 2017 10:24:57 -0800 Subject: [PATCH 6/6] Add ability to pass in a function to get cookies --- src/options.js | 3 ++- src/storage/cookie-storage.js | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/options.js b/src/options.js index 380e3c5..49d8226 100644 --- a/src/options.js +++ b/src/options.js @@ -18,7 +18,8 @@ export default { cookieStorage: { domain: getCookieDomain(), path: '/', - secure: false + secure: false, + getCookieFn: null, }, requestDataKey: 'data', responseDataKey: 'data', diff --git a/src/storage/cookie-storage.js b/src/storage/cookie-storage.js index a263602..4adc9d5 100644 --- a/src/storage/cookie-storage.js +++ b/src/storage/cookie-storage.js @@ -11,7 +11,8 @@ class CookieStorage { domain: getCookieDomain(), expires: null, path: '/', - secure: false + secure: false, + getCookieFn: this._getCookie, }, defaultOptions); } @@ -22,7 +23,8 @@ class CookieStorage { } getItem(key) { - const cookies = parseCookies(this._getCookie()); + const options = objectExtend({}, this._defaultOptions); + const cookies = parseCookies(options.getCookieFn()); return cookies.hasOwnProperty(key) ? cookies[key] : null; }