From eb6ba61e992c2b5251b1c1756768bb0727bf96df Mon Sep 17 00:00:00 2001 From: Sahat Yalkabov Date: Mon, 15 Sep 2014 20:29:07 -0700 Subject: [PATCH] pass isLinking param to oauth.authenticate() to prevent redirect after authentication (linking) --- examples/client/vendor/satellizer.js | 10 +++------- satellizer.js | 10 +++------- satellizer.min.js | 2 +- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/examples/client/vendor/satellizer.js b/examples/client/vendor/satellizer.js index 855fb86d..b0b4b103 100644 --- a/examples/client/vendor/satellizer.js +++ b/examples/client/vendor/satellizer.js @@ -194,12 +194,12 @@ function($q, $window, $location, config) { var shared = {}; - shared.saveToken = function(response, deferred) { + shared.saveToken = function(response, deferred, isLinking) { var token = response.data[config.tokenName]; var namespace = [config.tokenPrefix, config.tokenName].join('_'); $window.localStorage[namespace] = token; - if (config.loginRedirect) { + if (config.loginRedirect && !isLinking) { $location.path(config.loginRedirect); } @@ -252,11 +252,7 @@ provider.open(config.providers[name]) .then(function(response) { - if (isLinking) { - deferred.resolve(response); - } else { - shared.saveToken(response, deferred); - } + shared.saveToken(response, deferred, isLinking); }) .catch(function(response) { deferred.reject(response); diff --git a/satellizer.js b/satellizer.js index 855fb86d..b0b4b103 100644 --- a/satellizer.js +++ b/satellizer.js @@ -194,12 +194,12 @@ function($q, $window, $location, config) { var shared = {}; - shared.saveToken = function(response, deferred) { + shared.saveToken = function(response, deferred, isLinking) { var token = response.data[config.tokenName]; var namespace = [config.tokenPrefix, config.tokenName].join('_'); $window.localStorage[namespace] = token; - if (config.loginRedirect) { + if (config.loginRedirect && !isLinking) { $location.path(config.loginRedirect); } @@ -252,11 +252,7 @@ provider.open(config.providers[name]) .then(function(response) { - if (isLinking) { - deferred.resolve(response); - } else { - shared.saveToken(response, deferred); - } + shared.saveToken(response, deferred, isLinking); }) .catch(function(response) { deferred.reject(response); diff --git a/satellizer.min.js b/satellizer.min.js index 1e1b41ad..c344bc44 100644 --- a/satellizer.min.js +++ b/satellizer.min.js @@ -1 +1 @@ -!function(e,t){"use strict";t.module("satellizer",[]).constant("satellizer.config",{logoutRedirect:"/",loginRedirect:"/",signupRedirect:"/login",loginOnSignup:!0,loginUrl:"/auth/login",signupUrl:"/auth/signup",loginRoute:"/login",signupRoute:"/signup",tokenName:"token",tokenPrefix:"satellizer",unlinkUrl:"/auth/unlink/",providers:{google:{url:"/auth/google",authorizationEndpoint:"https://accounts.google.com/o/oauth2/auth",redirectUri:e.location.origin,scope:["profile","email"],scopePrefix:"openid",scopeDelimiter:" ",requiredUrlParams:["scope"],optionalUrlParams:["display"],display:"popup",type:"2.0",popupOptions:{width:452,height:633}},facebook:{url:"/auth/facebook",authorizationEndpoint:"https://www.facebook.com/dialog/oauth",redirectUri:e.location.origin+"/",scope:["email"],scopeDelimiter:",",requiredUrlParams:["display","scope"],display:"popup",type:"2.0",popupOptions:{width:481,height:269}},linkedin:{url:"/auth/linkedin",authorizationEndpoint:"https://www.linkedin.com/uas/oauth2/authorization",redirectUri:e.location.origin,requiredUrlParams:["state"],scope:[],scopeDelimiter:" ",state:"STATE",type:"2.0",popupOptions:{width:527,height:582}},github:{name:"github",url:"/auth/github",authorizationEndpoint:"https://github.com/login/oauth/authorize",redirectUri:e.location.origin,scope:[],scopeDelimiter:" ",type:"2.0",popupOptions:{width:1020,height:618}},twitter:{url:"/auth/twitter",type:"1.0"},yahoo:{url:"/auth/yahoo",type:"1.0",popupOptions:{width:559,height:519}}}}).provider("$auth",["satellizer.config",function(e){Object.defineProperties(this,{logoutRedirect:{get:function(){return e.logoutRedirect},set:function(t){e.logoutRedirect=t}},loginRedirect:{set:function(t){e.loginRedirect=t},get:function(){return e.loginRedirect}},signupRedirect:{get:function(){return e.signupRedirect},set:function(t){e.signupRedirect=t}},loginOnSignup:{get:function(){return e.loginOnSignup},set:function(t){e.loginOnSignup=t}},loginUrl:{get:function(){return e.loginUrl},set:function(t){e.loginUrl=t}},signupUrl:{get:function(){return e.signupUrl},set:function(t){e.signupUrl=t}},loginRoute:{get:function(){return e.loginRoute},set:function(t){e.loginRoute=t}},signupRoute:{get:function(){return e.signupRoute},set:function(t){e.signupRoute=t}},tokenName:{get:function(){return e.tokenName},set:function(t){e.tokenName=t}},tokenPrefix:{get:function(){return e.tokenPrefix},set:function(t){e.tokenPrefix=t}},unlinkUrl:{get:function(){return e.unlinkUrl},set:function(t){e.unlinkUrl=t}}}),t.forEach(Object.keys(e.providers),function(n){this[n]=function(o){return t.extend(e.providers[n],o)}},this);var n=function(n){e.providers[n.name]=e.providers[n.name]||{},t.extend(e.providers[n.name],n)};this.oauth1=function(t){n(t),e.providers[t.name].type="1.0"},this.oauth2=function(t){n(t),e.providers[t.name].type="2.0"},this.$get=["$q","satellizer.shared","satellizer.local","satellizer.oauth",function(e,t,n,o){var i={};return i.authenticate=function(e){return o.authenticate(e)},i.login=function(e){return n.login(e)},i.signup=function(e){return n.signup(e)},i.logout=function(){return t.logout()},i.isAuthenticated=function(){return t.isAuthenticated()},i.link=function(e){return o.authenticate(e,!0)},i.unlink=function(e){return o.unlink(e)},i}]}]).factory("satellizer.shared",["$q","$window","$location","satellizer.config",function(e,t,n,o){var i={};return i.saveToken=function(e,i){var r=e.data[o.tokenName],u=[o.tokenPrefix,o.tokenName].join("_");t.localStorage[u]=r,o.loginRedirect&&n.path(o.loginRedirect),i.resolve(e)},i.isAuthenticated=function(){var e=[o.tokenPrefix,o.tokenName].join("_"),n=t.localStorage[e];if(n){var i=n.split(".")[1],r=i.replace("-","+").replace("_","/"),u=JSON.parse(t.atob(r)).exp;return Date.now()<=u}return!1},i.logout=function(){var i=e.defer(),r=[o.tokenPrefix,o.tokenName].join("_");return delete t.localStorage[r],o.logoutRedirect&&n.path(o.logoutRedirect),i.resolve(),i.promise},i}]).factory("satellizer.oauth",["$q","$http","satellizer.config","satellizer.shared","satellizer.Oauth1","satellizer.Oauth2",function(e,t,n,o,i,r){var u={};return u.authenticate=function(t,u){var a=e.defer(),l="1.0"===n.providers[t].type?new i:new r;return l.open(n.providers[t]).then(function(e){u?a.resolve(e):o.saveToken(e,a)}).catch(function(e){a.reject(e)}),a.promise},u.unlink=function(e){return t.get(n.unlinkUrl+e)},u}]).factory("satellizer.local",["$q","$http","$location","satellizer.utils","satellizer.shared","satellizer.config",function(e,t,n,o,i,r){var u={};return u.login=function(n){var o=e.defer();return t.post(r.loginUrl,n).then(function(e){i.saveToken(e,o)}).catch(function(e){o.reject(e)}),o.promise},u.signup=function(o){var u=e.defer();return t.post(r.signupUrl,o).then(function(e){r.loginOnSignup?i.saveToken(e,u):(n.path(r.signupRedirect),u.resolve(e))}).catch(function(e){u.reject(e)}),u.promise},u}]).factory("satellizer.Oauth2",["$q","$http","satellizer.popup","satellizer.utils",function(e,n,o,i){return function(){var r={url:null,name:null,scope:null,scopeDelimiter:null,clientId:null,redirectUri:null,popupOptions:null,authorizationEndpoint:null,requiredUrlParams:null,optionalUrlParams:null,defaultUrlParams:["response_type","client_id","redirect_uri"],responseType:"code"},u={};return u.open=function(n){t.extend(r,n);var i=e.defer(),a=u.buildUrl();return o.open(a,r.popupOptions).then(function(e){u.exchangeForToken(e).then(function(e){i.resolve(e)}).catch(function(e){i.reject(e)})}).catch(function(e){i.reject(e)}),i.promise},u.exchangeForToken=function(e){return n.post(r.url,{code:e.code,clientId:r.clientId,redirectUri:r.redirectUri})},u.buildUrl=function(){var e=r.authorizationEndpoint,t=u.buildQueryString();return[e,t].join("?")},u.buildQueryString=function(){var e=[],n=["defaultUrlParams","requiredUrlParams","optionalUrlParams"];return t.forEach(n,function(n){t.forEach(r[n],function(t){var n=i.camelCase(t),o=r[n];"scope"===t&&Array.isArray(o)&&(o=o.join(r.scopeDelimiter),r.scopePrefix&&(o=[r.scopePrefix,o].join(r.scopeDelimiter))),e.push([t,encodeURIComponent(o)])})}),e.map(function(e){return e.join("=")}).join("&")},u}}]).factory("satellizer.Oauth1",["$q","$http","satellizer.popup",function(e,n,o){return function(){var i={url:null,name:null,popupOptions:null},r={};return r.open=function(n){t.extend(i,n);var u=e.defer();return o.open(i.url,i.popupOptions).then(function(e){r.exchangeForToken(e).then(function(e){u.resolve(e)}).catch(function(e){u.reject(e)})}).catch(function(e){u.reject(e)}),u.promise},r.exchangeForToken=function(e){return e=r.buildQueryString(e),n.get(i.url+"?"+e)},r.buildQueryString=function(e){var n=[];return t.forEach(e,function(e,t){n.push(encodeURIComponent(t)+"="+encodeURIComponent(e))}),n.join("&")},r}}]).factory("satellizer.popup",["$q","$interval","$window",function(e,n,o){var i=null,r=null,u={};return u.popupWindow=i,u.open=function(t,n){var r=e.defer(),a=u.stringifyOptions(u.prepareOptions(n||{}));return i=o.open(t,"_blank",a),i.focus(),u.postMessageHandler(r),u.pollPopup(r),r.promise},u.pollPopup=function(e){r=n(function(){i.closed&&(n.cancel(r),e.reject({data:"Authorization Failed"}))},35)},u.postMessageHandler=function(e){o.addEventListener("message",function(t){t.origin===o.location.origin&&(i.close(),t.data.error?e.reject({data:t.data.error}):e.resolve(t.data))},!1)},u.prepareOptions=function(e){var n=e.width||500,i=e.height||500;return t.extend({width:n,height:i,left:o.screenX+(o.outerWidth-n)/2,top:o.screenY+(o.outerHeight-i)/2.5},e)},u.stringifyOptions=function(e){var n=[];return t.forEach(e,function(e,t){n.push(t+"="+e)}),n.join(",")},u}]).service("satellizer.utils",function(){this.camelCase=function(e){return e.replace(/([\:\-\_]+(.))/g,function(e,t,n,o){return o?n.toUpperCase():n})},this.parseQueryString=function(e){var n,o,i={};return t.forEach((e||"").split("&"),function(e){e&&(o=e.split("="),n=decodeURIComponent(o[0]),i[n]=t.isDefined(o[1])?decodeURIComponent(o[1]):!0)}),i}}).config(["$httpProvider","satellizer.config",function(e,t){e.interceptors.push(["$q",function(e){return{request:function(e){return localStorage.getItem([t.tokenPrefix,t.tokenName].join("_"))&&(e.headers.Authorization="Bearer "+localStorage.getItem([t.tokenPrefix,t.tokenName].join("_"))),e},responseError:function(n){return 401===n.status&&localStorage.removeItem([t.tokenPrefix,t.tokenName].join("_")),e.reject(n)}}}])}]).run(["$window","$location","satellizer.utils",function(e,t,n){var o=e.location.search.substring(1),i=Object.keys(t.search()).length?t.search():n.parseQueryString(o);try{e.opener&&e.opener.location.origin===e.location.origin&&(i.oauth_token&&i.oauth_verifier?e.opener.postMessage({oauth_token:i.oauth_token,oauth_verifier:i.oauth_verifier},e.location.origin):i.code?e.opener.postMessage({code:i.code},e.location.origin):i.error&&e.opener.postMessage({error:i.error},e.location.origin))}catch(r){}}])}(window,window.angular); \ No newline at end of file +!function(e,t){"use strict";t.module("satellizer",[]).constant("satellizer.config",{logoutRedirect:"/",loginRedirect:"/",signupRedirect:"/login",loginOnSignup:!0,loginUrl:"/auth/login",signupUrl:"/auth/signup",loginRoute:"/login",signupRoute:"/signup",tokenName:"token",tokenPrefix:"satellizer",unlinkUrl:"/auth/unlink/",providers:{google:{url:"/auth/google",authorizationEndpoint:"https://accounts.google.com/o/oauth2/auth",redirectUri:e.location.origin,scope:["profile","email"],scopePrefix:"openid",scopeDelimiter:" ",requiredUrlParams:["scope"],optionalUrlParams:["display"],display:"popup",type:"2.0",popupOptions:{width:452,height:633}},facebook:{url:"/auth/facebook",authorizationEndpoint:"https://www.facebook.com/dialog/oauth",redirectUri:e.location.origin+"/",scope:["email"],scopeDelimiter:",",requiredUrlParams:["display","scope"],display:"popup",type:"2.0",popupOptions:{width:481,height:269}},linkedin:{url:"/auth/linkedin",authorizationEndpoint:"https://www.linkedin.com/uas/oauth2/authorization",redirectUri:e.location.origin,requiredUrlParams:["state"],scope:[],scopeDelimiter:" ",state:"STATE",type:"2.0",popupOptions:{width:527,height:582}},github:{name:"github",url:"/auth/github",authorizationEndpoint:"https://github.com/login/oauth/authorize",redirectUri:e.location.origin,scope:[],scopeDelimiter:" ",type:"2.0",popupOptions:{width:1020,height:618}},twitter:{url:"/auth/twitter",type:"1.0"},yahoo:{url:"/auth/yahoo",type:"1.0",popupOptions:{width:559,height:519}}}}).provider("$auth",["satellizer.config",function(e){Object.defineProperties(this,{logoutRedirect:{get:function(){return e.logoutRedirect},set:function(t){e.logoutRedirect=t}},loginRedirect:{set:function(t){e.loginRedirect=t},get:function(){return e.loginRedirect}},signupRedirect:{get:function(){return e.signupRedirect},set:function(t){e.signupRedirect=t}},loginOnSignup:{get:function(){return e.loginOnSignup},set:function(t){e.loginOnSignup=t}},loginUrl:{get:function(){return e.loginUrl},set:function(t){e.loginUrl=t}},signupUrl:{get:function(){return e.signupUrl},set:function(t){e.signupUrl=t}},loginRoute:{get:function(){return e.loginRoute},set:function(t){e.loginRoute=t}},signupRoute:{get:function(){return e.signupRoute},set:function(t){e.signupRoute=t}},tokenName:{get:function(){return e.tokenName},set:function(t){e.tokenName=t}},tokenPrefix:{get:function(){return e.tokenPrefix},set:function(t){e.tokenPrefix=t}},unlinkUrl:{get:function(){return e.unlinkUrl},set:function(t){e.unlinkUrl=t}}}),t.forEach(Object.keys(e.providers),function(n){this[n]=function(o){return t.extend(e.providers[n],o)}},this);var n=function(n){e.providers[n.name]=e.providers[n.name]||{},t.extend(e.providers[n.name],n)};this.oauth1=function(t){n(t),e.providers[t.name].type="1.0"},this.oauth2=function(t){n(t),e.providers[t.name].type="2.0"},this.$get=["$q","satellizer.shared","satellizer.local","satellizer.oauth",function(e,t,n,o){var i={};return i.authenticate=function(e){return o.authenticate(e)},i.login=function(e){return n.login(e)},i.signup=function(e){return n.signup(e)},i.logout=function(){return t.logout()},i.isAuthenticated=function(){return t.isAuthenticated()},i.link=function(e){return o.authenticate(e,!0)},i.unlink=function(e){return o.unlink(e)},i}]}]).factory("satellizer.shared",["$q","$window","$location","satellizer.config",function(e,t,n,o){var i={};return i.saveToken=function(e,i,r){var u=e.data[o.tokenName],a=[o.tokenPrefix,o.tokenName].join("_");t.localStorage[a]=u,o.loginRedirect&&!r&&n.path(o.loginRedirect),i.resolve(e)},i.isAuthenticated=function(){var e=[o.tokenPrefix,o.tokenName].join("_"),n=t.localStorage[e];if(n){var i=n.split(".")[1],r=i.replace("-","+").replace("_","/"),u=JSON.parse(t.atob(r)).exp;return Date.now()<=u}return!1},i.logout=function(){var i=e.defer(),r=[o.tokenPrefix,o.tokenName].join("_");return delete t.localStorage[r],o.logoutRedirect&&n.path(o.logoutRedirect),i.resolve(),i.promise},i}]).factory("satellizer.oauth",["$q","$http","satellizer.config","satellizer.shared","satellizer.Oauth1","satellizer.Oauth2",function(e,t,n,o,i,r){var u={};return u.authenticate=function(t,u){var a=e.defer(),l="1.0"===n.providers[t].type?new i:new r;return l.open(n.providers[t]).then(function(e){o.saveToken(e,a,u)}).catch(function(e){a.reject(e)}),a.promise},u.unlink=function(e){return t.get(n.unlinkUrl+e)},u}]).factory("satellizer.local",["$q","$http","$location","satellizer.utils","satellizer.shared","satellizer.config",function(e,t,n,o,i,r){var u={};return u.login=function(n){var o=e.defer();return t.post(r.loginUrl,n).then(function(e){i.saveToken(e,o)}).catch(function(e){o.reject(e)}),o.promise},u.signup=function(o){var u=e.defer();return t.post(r.signupUrl,o).then(function(e){r.loginOnSignup?i.saveToken(e,u):(n.path(r.signupRedirect),u.resolve(e))}).catch(function(e){u.reject(e)}),u.promise},u}]).factory("satellizer.Oauth2",["$q","$http","satellizer.popup","satellizer.utils",function(e,n,o,i){return function(){var r={url:null,name:null,scope:null,scopeDelimiter:null,clientId:null,redirectUri:null,popupOptions:null,authorizationEndpoint:null,requiredUrlParams:null,optionalUrlParams:null,defaultUrlParams:["response_type","client_id","redirect_uri"],responseType:"code"},u={};return u.open=function(n){t.extend(r,n);var i=e.defer(),a=u.buildUrl();return o.open(a,r.popupOptions).then(function(e){u.exchangeForToken(e).then(function(e){i.resolve(e)}).catch(function(e){i.reject(e)})}).catch(function(e){i.reject(e)}),i.promise},u.exchangeForToken=function(e){return n.post(r.url,{code:e.code,clientId:r.clientId,redirectUri:r.redirectUri})},u.buildUrl=function(){var e=r.authorizationEndpoint,t=u.buildQueryString();return[e,t].join("?")},u.buildQueryString=function(){var e=[],n=["defaultUrlParams","requiredUrlParams","optionalUrlParams"];return t.forEach(n,function(n){t.forEach(r[n],function(t){var n=i.camelCase(t),o=r[n];"scope"===t&&Array.isArray(o)&&(o=o.join(r.scopeDelimiter),r.scopePrefix&&(o=[r.scopePrefix,o].join(r.scopeDelimiter))),e.push([t,encodeURIComponent(o)])})}),e.map(function(e){return e.join("=")}).join("&")},u}}]).factory("satellizer.Oauth1",["$q","$http","satellizer.popup",function(e,n,o){return function(){var i={url:null,name:null,popupOptions:null},r={};return r.open=function(n){t.extend(i,n);var u=e.defer();return o.open(i.url,i.popupOptions).then(function(e){r.exchangeForToken(e).then(function(e){u.resolve(e)}).catch(function(e){u.reject(e)})}).catch(function(e){u.reject(e)}),u.promise},r.exchangeForToken=function(e){return e=r.buildQueryString(e),n.get(i.url+"?"+e)},r.buildQueryString=function(e){var n=[];return t.forEach(e,function(e,t){n.push(encodeURIComponent(t)+"="+encodeURIComponent(e))}),n.join("&")},r}}]).factory("satellizer.popup",["$q","$interval","$window",function(e,n,o){var i=null,r=null,u={};return u.popupWindow=i,u.open=function(t,n){var r=e.defer(),a=u.stringifyOptions(u.prepareOptions(n||{}));return i=o.open(t,"_blank",a),i.focus(),u.postMessageHandler(r),u.pollPopup(r),r.promise},u.pollPopup=function(e){r=n(function(){i.closed&&(n.cancel(r),e.reject({data:"Authorization Failed"}))},35)},u.postMessageHandler=function(e){o.addEventListener("message",function(t){t.origin===o.location.origin&&(i.close(),t.data.error?e.reject({data:t.data.error}):e.resolve(t.data))},!1)},u.prepareOptions=function(e){var n=e.width||500,i=e.height||500;return t.extend({width:n,height:i,left:o.screenX+(o.outerWidth-n)/2,top:o.screenY+(o.outerHeight-i)/2.5},e)},u.stringifyOptions=function(e){var n=[];return t.forEach(e,function(e,t){n.push(t+"="+e)}),n.join(",")},u}]).service("satellizer.utils",function(){this.camelCase=function(e){return e.replace(/([\:\-\_]+(.))/g,function(e,t,n,o){return o?n.toUpperCase():n})},this.parseQueryString=function(e){var n,o,i={};return t.forEach((e||"").split("&"),function(e){e&&(o=e.split("="),n=decodeURIComponent(o[0]),i[n]=t.isDefined(o[1])?decodeURIComponent(o[1]):!0)}),i}}).config(["$httpProvider","satellizer.config",function(e,t){e.interceptors.push(["$q",function(e){return{request:function(e){return localStorage.getItem([t.tokenPrefix,t.tokenName].join("_"))&&(e.headers.Authorization="Bearer "+localStorage.getItem([t.tokenPrefix,t.tokenName].join("_"))),e},responseError:function(n){return 401===n.status&&localStorage.removeItem([t.tokenPrefix,t.tokenName].join("_")),e.reject(n)}}}])}]).run(["$window","$location","satellizer.utils",function(e,t,n){var o=e.location.search.substring(1),i=Object.keys(t.search()).length?t.search():n.parseQueryString(o);try{e.opener&&e.opener.location.origin===e.location.origin&&(i.oauth_token&&i.oauth_verifier?e.opener.postMessage({oauth_token:i.oauth_token,oauth_verifier:i.oauth_verifier},e.location.origin):i.code?e.opener.postMessage({code:i.code},e.location.origin):i.error&&e.opener.postMessage({error:i.error},e.location.origin))}catch(r){}}])}(window,window.angular); \ No newline at end of file