diff --git a/satellizer.js b/satellizer.js index 8a9efca9..18ed8f1c 100644 --- a/satellizer.js +++ b/satellizer.js @@ -295,6 +295,10 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.ex }; Shared.setToken = function(response) { + if (!response) { + return console.warn('Can\'t set token without passing a value'); + } + var accessToken = response && response.access_token; var token; @@ -313,7 +317,7 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.ex if (!token) { var tokenPath = config.tokenRoot ? config.tokenRoot + '.' + config.tokenName : config.tokenName; - throw new Error('Expecting a token named "' + tokenPath + '" but instead got: ' + JSON.stringify(response.data)); + return console.warn('Expecting a token named "' + tokenPath); } storage.set(tokenName, token); diff --git a/satellizer.min.js b/satellizer.min.js index 67604671..98fcd834 100644 --- a/satellizer.min.js +++ b/satellizer.min.js @@ -3,4 +3,4 @@ * (c) 2014-2015 Sahat Yalkabov * License: MIT */ -"undefined"!=typeof module&&"undefined"!=typeof exports&&module.exports===exports&&(module.exports="satellizer"),function(e,t,o){"use strict";t.module("satellizer",[]).constant("SatellizerConfig",{httpInterceptor:!0,withCredentials:!0,tokenRoot:null,cordova:!1,baseUrl:"/",loginUrl:"/auth/login",signupUrl:"/auth/signup",unlinkUrl:"/auth/unlink/",tokenName:"token",tokenPrefix:"satellizer",authHeader:"Authorization",authToken:"Bearer",storageType:"localStorage",providers:{facebook:{name:"facebook",url:"/auth/facebook",authorizationEndpoint:"https://www.facebook.com/v2.3/dialog/oauth",redirectUri:(e.location.origin||e.location.protocol+"//"+e.location.host)+"/",requiredUrlParams:["display","scope"],scope:["email"],scopeDelimiter:",",display:"popup",type:"2.0",popupOptions:{width:580,height:400}},google:{name:"google",url:"/auth/google",authorizationEndpoint:"https://accounts.google.com/o/oauth2/auth",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["scope"],optionalUrlParams:["display"],scope:["profile","email"],scopePrefix:"openid",scopeDelimiter:" ",display:"popup",type:"2.0",popupOptions:{width:452,height:633}},github:{name:"github",url:"/auth/github",authorizationEndpoint:"https://github.com/login/oauth/authorize",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,optionalUrlParams:["scope"],scope:["user:email"],scopeDelimiter:" ",type:"2.0",popupOptions:{width:1020,height:618}},instagram:{name:"instagram",url:"/auth/instagram",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["scope"],scope:["basic"],scopeDelimiter:"+",authorizationEndpoint:"https://api.instagram.com/oauth/authorize"},linkedin:{name:"linkedin",url:"/auth/linkedin",authorizationEndpoint:"https://www.linkedin.com/uas/oauth2/authorization",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["state"],scope:["r_emailaddress"],scopeDelimiter:" ",state:"STATE",type:"2.0",popupOptions:{width:527,height:582}},twitter:{name:"twitter",url:"/auth/twitter",authorizationEndpoint:"https://api.twitter.com/oauth/authenticate",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,type:"1.0",popupOptions:{width:495,height:645}},twitch:{name:"twitch",url:"/auth/twitch",authorizationEndpoint:"https://api.twitch.tv/kraken/oauth2/authorize",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["scope"],scope:["user_read"],scopeDelimiter:" ",display:"popup",type:"2.0",popupOptions:{width:500,height:560}},live:{name:"live",url:"/auth/live",authorizationEndpoint:"https://login.live.com/oauth20_authorize.srf",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["display","scope"],scope:["wl.emails"],scopeDelimiter:" ",display:"popup",type:"2.0",popupOptions:{width:500,height:560}},yahoo:{name:"yahoo",url:"/auth/yahoo",authorizationEndpoint:"https://api.login.yahoo.com/oauth2/request_auth",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,scope:[],scopeDelimiter:",",type:"2.0",popupOptions:{width:559,height:519}}}}).provider("$auth",["SatellizerConfig",function(e){Object.defineProperties(this,{httpInterceptor:{get:function(){return e.httpInterceptor},set:function(t){e.httpInterceptor=t}},baseUrl:{get:function(){return e.baseUrl},set:function(t){e.baseUrl=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}},tokenRoot:{get:function(){return e.tokenRoot},set:function(t){e.tokenRoot=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}},authHeader:{get:function(){return e.authHeader},set:function(t){e.authHeader=t}},authToken:{get:function(){return e.authToken},set:function(t){e.authToken=t}},withCredentials:{get:function(){return e.withCredentials},set:function(t){e.withCredentials=t}},cordova:{get:function(){return e.cordova},set:function(t){e.cordova=t}},storageType:{get:function(){return e.storageType},set:function(t){e.storageType=t}}}),t.forEach(Object.keys(e.providers),function(o){this[o]=function(r){return t.extend(e.providers[o],r)}},this);var o=function(o){e.providers[o.name]=e.providers[o.name]||{},t.extend(e.providers[o.name],o)};this.oauth1=function(t){o(t),e.providers[t.name].type="1.0"},this.oauth2=function(t){o(t),e.providers[t.name].type="2.0"},this.$get=["$q","SatellizerShared","SatellizerLocal","SatellizerOauth",function(e,t,o,r){var n={};return n.login=function(e,t){return o.login(e,t)},n.signup=function(e,t){return o.signup(e,t)},n.logout=function(){return t.logout()},n.authenticate=function(e,t){return r.authenticate(e,t)},n.link=function(e,t){return r.authenticate(e,t)},n.unlink=function(e,t){return r.unlink(e,t)},n.isAuthenticated=function(){return t.isAuthenticated()},n.getToken=function(){return t.getToken()},n.setToken=function(e){t.setToken({access_token:e})},n.removeToken=function(){return t.removeToken()},n.getPayload=function(){return t.getPayload()},n.setStorageType=function(e){return t.setStorageType(e)},n}]}]).factory("SatellizerShared",["$q","$window","SatellizerConfig","SatellizerStorage",function(r,n,i,a){var u={},l=i.tokenPrefix?[i.tokenPrefix,i.tokenName].join("_"):i.tokenName;return u.getToken=function(){return a.get(l)},u.getPayload=function(){var t=a.get(l);if(t&&3===t.split(".").length)try{var r=t.split(".")[1],n=r.replace(/-/g,"+").replace(/_/g,"/");return JSON.parse(decodeURIComponent(escape(e.atob(n))))}catch(i){return o}},u.setToken=function(e){var o,r=e&&e.access_token;if(r&&(t.isObject(r)&&t.isObject(r.data)?e=r:t.isString(r)&&(o=r)),!o&&e){var n=i.tokenRoot&&i.tokenRoot.split(".").reduce(function(e,t){return e[t]},e.data);o=n?n[i.tokenName]:e.data[i.tokenName]}if(!o){var u=i.tokenRoot?i.tokenRoot+"."+i.tokenName:i.tokenName;throw new Error('Expecting a token named "'+u+'" but instead got: '+JSON.stringify(e.data))}a.set(l,o)},u.removeToken=function(){a.remove(l)},u.isAuthenticated=function(){var e=a.get(l);if(e){if(3===e.split(".").length)try{var t=e.split(".")[1],o=t.replace(/-/g,"+").replace(/_/g,"/"),r=JSON.parse(n.atob(o)).exp;if(r){var i=Math.round((new Date).getTime()/1e3)>=r;return i?(a.remove(l),!1):!0}}catch(u){return!0}return!0}return!1},u.logout=function(){return a.remove(l),r.when()},u.setStorageType=function(e){i.storageType=e},u}]).factory("SatellizerOauth",["$q","$http","SatellizerConfig","SatellizerUtils","SatellizerShared","SatellizerOauth1","SatellizerOauth2",function(e,t,o,r,n,i,a){var u={};return u.authenticate=function(t,r){var u="1.0"===o.providers[t].type?new i:new a,l=e.defer();return u.open(o.providers[t],r||{}).then(function(e){n.setToken(e,!1),l.resolve(e)})["catch"](function(e){l.reject(e)}),l.promise},u.unlink=function(e,n){return n=n||{},n.url=n.url?n.url:r.joinUrl(o.baseUrl,o.unlinkUrl),n.data={provider:e}||n.data,n.method=n.method||"POST",t(n)},u}]).factory("SatellizerLocal",["$http","SatellizerUtils","SatellizerShared","SatellizerConfig",function(e,t,o,r){var n={};return n.login=function(n,i){return i=i||{},i.url=i.url?i.url:t.joinUrl(r.baseUrl,r.loginUrl),i.data=n||i.data,i.method=i.method||"POST",e(i).then(function(e){return o.setToken(e),e})},n.signup=function(o,n){return n=n||{},n.url=n.url?n.url:t.joinUrl(r.baseUrl,r.signupUrl),n.data=o||n.data,n.method=n.method||"POST",e(n)},n}]).factory("SatellizerOauth2",["$q","$http","$window","SatellizerPopup","SatellizerUtils","SatellizerConfig","SatellizerStorage",function(e,o,r,n,i,a,u){return function(){var r={},l={defaultUrlParams:["response_type","client_id","redirect_uri"],responseType:"code",responseParams:{code:"code",clientId:"clientId",redirectUri:"redirectUri"}};return r.open=function(o,p){l=i.merge(o,l);var c,s,h=l.name+"_state";return t.isFunction(l.state)?u.set(h,l.state()):t.isString(l.state)&&u.set(h,l.state),c=[l.authorizationEndpoint,r.buildQueryString()].join("?"),s=a.cordova?n.open(c,l.name,l.popupOptions,l.redirectUri).eventListener(l.redirectUri):n.open(c,l.name,l.popupOptions,l.redirectUri).pollPopup(),s.then(function(t){return"token"===l.responseType?t:t.state&&t.state!==u.get(h)?e.reject('OAuth "state" mismatch'):r.exchangeForToken(t,p)})},r.exchangeForToken=function(e,r){var n=t.extend({},r);t.forEach(l.responseParams,function(t,o){switch(o){case"code":n[t]=e.code;break;case"clientId":n[t]=l.clientId;break;case"redirectUri":n[t]=l.redirectUri;break;default:n[t]=e[o]}}),e.state&&(n.state=e.state);var u=a.baseUrl?i.joinUrl(a.baseUrl,l.url):l.url;return o.post(u,n,{withCredentials:a.withCredentials})},r.buildQueryString=function(){var e=[],o=["defaultUrlParams","requiredUrlParams","optionalUrlParams"];return t.forEach(o,function(o){t.forEach(l[o],function(o){var r=i.camelCase(o),n=t.isFunction(l[o])?l[o]():l[r];if("state"===o){var a=l.name+"_state";n=encodeURIComponent(u.get(a))}"scope"===o&&Array.isArray(n)&&(n=n.join(l.scopeDelimiter),l.scopePrefix&&(n=[l.scopePrefix,n].join(l.scopeDelimiter))),e.push([o,n])})}),e.map(function(e){return e.join("=")}).join("&")},r}}]).factory("SatellizerOauth1",["$q","$http","SatellizerPopup","SatellizerConfig","SatellizerUtils",function(e,o,r,n,i){return function(){var e={},a={url:null,name:null,popupOptions:null,redirectUri:null,authorizationEndpoint:null};return e.open=function(u,l){t.extend(a,u);var p,c=n.baseUrl?i.joinUrl(n.baseUrl,a.url):a.url;return n.cordova||(p=r.open("",a.name,a.popupOptions,a.redirectUri)),o.post(c,a).then(function(t){n.cordova?p=r.open([a.authorizationEndpoint,e.buildQueryString(t.data)].join("?"),a.name,a.popupOptions,a.redirectUri):p.popupWindow.location=[a.authorizationEndpoint,e.buildQueryString(t.data)].join("?");var o=n.cordova?p.eventListener(a.redirectUri):p.pollPopup();return o.then(function(t){return e.exchangeForToken(t,l)})})},e.exchangeForToken=function(e,r){var u=t.extend({},r,e),l=n.baseUrl?i.joinUrl(n.baseUrl,a.url):a.url;return o.post(l,u,{withCredentials:n.withCredentials})},e.buildQueryString=function(e){var o=[];return t.forEach(e,function(e,t){o.push(encodeURIComponent(t)+"="+encodeURIComponent(e))}),o.join("&")},e}}]).factory("SatellizerPopup",["$q","$interval","$window","SatellizerConfig","SatellizerUtils",function(r,n,i,a,u){var l={};return l.url="",l.popupWindow=null,l.open=function(t,o,r){l.url=t;var n=l.stringifyOptions(l.prepareOptions(r)),i=a.cordova?"_blank":o;return l.popupWindow=e.open(t,i,n),e.popup=l.popupWindow,l.popupWindow&&l.popupWindow.focus&&l.popupWindow.focus(),l},l.eventListener=function(e){var o=r.defer();return l.popupWindow.addEventListener("loadstart",function(r){if(0===r.url.indexOf(e)){var n=document.createElement("a");if(n.href=r.url,n.search||n.hash){var i=n.search.substring(1).replace(/\/$/,""),a=n.hash.substring(1).replace(/\/$/,""),p=u.parseQueryString(a),c=u.parseQueryString(i);t.extend(c,p),c.error||o.resolve(c),l.popupWindow.close()}}}),l.popupWindow.addEventListener("loaderror",function(){o.reject("Authorization Failed")}),o.promise},l.pollPopup=function(){var e=r.defer(),i=n(function(){try{var r=document.location.host,a=l.popupWindow.location.host;if(a===r&&(l.popupWindow.location.search||l.popupWindow.location.hash)){var p=l.popupWindow.location.search.substring(1).replace(/\/$/,""),c=l.popupWindow.location.hash.substring(1).replace(/[\/$]/,""),s=u.parseQueryString(c),h=u.parseQueryString(p);t.extend(h,s),h.error||e.resolve(h),l.popupWindow.close(),n.cancel(i)}}catch(d){}(!l.popupWindow||l.popupWindow.closed||l.popupWindow.closed===o)&&n.cancel(i)},50);return e.promise},l.prepareOptions=function(e){e=e||{};var o=e.width||500,r=e.height||500;return t.extend({width:o,height:r,left:i.screenX+(i.outerWidth-o)/2,top:i.screenY+(i.outerHeight-r)/2.5},e)},l.stringifyOptions=function(e){var o=[];return t.forEach(e,function(e,t){o.push(t+"="+e)}),o.join(",")},l}]).service("SatellizerUtils",function(){this.camelCase=function(e){return e.replace(/([\:\-\_]+(.))/g,function(e,t,o,r){return r?o.toUpperCase():o})},this.parseQueryString=function(e){var o,r,n={};return t.forEach((e||"").split("&"),function(e){e&&(r=e.split("="),o=decodeURIComponent(r[0]),n[o]=t.isDefined(r[1])?decodeURIComponent(r[1]):!0)}),n},this.joinUrl=function(e,t){if(/^(?:[a-z]+:)?\/\//i.test(t))return t;var o=[e,t].join("/"),r=function(e){return e.replace(/[\/]+/g,"/").replace(/\/\?/g,"?").replace(/\/\#/g,"#").replace(/\:\//g,"://")};return r(o)},this.merge=function(e,t){var o={};for(var r in e)e.hasOwnProperty(r)&&(r in t&&"object"==typeof e[r]&&null!==r?o[r]=this.merge(e[r],t[r]):o[r]=e[r]);for(r in t)if(t.hasOwnProperty(r)){if(r in o)continue;o[r]=t[r]}return o}}).factory("SatellizerStorage",["$window","SatellizerConfig",function(e,t){var r=function(){try{var o=t.storageType in e&&null!==e[t.storageType];if(o){var r=Math.random().toString(36).substring(7);e[t.storageType].setItem(r,""),e[t.storageType].removeItem(r)}return o}catch(n){return!1}}();return r||console.warn("Satellizer Warning: "+t.storageType+" is not available."),{get:function(n){return r?e[t.storageType].getItem(n):o},set:function(n,i){return r?e[t.storageType].setItem(n,i):o},remove:function(n){return r?e[t.storageType].removeItem(n):o}}}]).factory("SatellizerInterceptor",["$q","SatellizerConfig","SatellizerStorage","SatellizerShared",function(e,t,o,r){return{request:function(e){if(e.skipAuthorization)return e;if(r.isAuthenticated()&&t.httpInterceptor){var n=t.tokenPrefix?t.tokenPrefix+"_"+t.tokenName:t.tokenName,i=o.get(n);t.authHeader&&t.authToken&&(i=t.authToken+" "+i),e.headers[t.authHeader]=i}return e},responseError:function(t){return e.reject(t)}}}]).config(["$httpProvider",function(e){e.interceptors.push("SatellizerInterceptor")}])}(window,window.angular); \ No newline at end of file +"undefined"!=typeof module&&"undefined"!=typeof exports&&module.exports===exports&&(module.exports="satellizer"),function(e,t,o){"use strict";t.module("satellizer",[]).constant("SatellizerConfig",{httpInterceptor:!0,withCredentials:!0,tokenRoot:null,cordova:!1,baseUrl:"/",loginUrl:"/auth/login",signupUrl:"/auth/signup",unlinkUrl:"/auth/unlink/",tokenName:"token",tokenPrefix:"satellizer",authHeader:"Authorization",authToken:"Bearer",storageType:"localStorage",providers:{facebook:{name:"facebook",url:"/auth/facebook",authorizationEndpoint:"https://www.facebook.com/v2.3/dialog/oauth",redirectUri:(e.location.origin||e.location.protocol+"//"+e.location.host)+"/",requiredUrlParams:["display","scope"],scope:["email"],scopeDelimiter:",",display:"popup",type:"2.0",popupOptions:{width:580,height:400}},google:{name:"google",url:"/auth/google",authorizationEndpoint:"https://accounts.google.com/o/oauth2/auth",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["scope"],optionalUrlParams:["display"],scope:["profile","email"],scopePrefix:"openid",scopeDelimiter:" ",display:"popup",type:"2.0",popupOptions:{width:452,height:633}},github:{name:"github",url:"/auth/github",authorizationEndpoint:"https://github.com/login/oauth/authorize",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,optionalUrlParams:["scope"],scope:["user:email"],scopeDelimiter:" ",type:"2.0",popupOptions:{width:1020,height:618}},instagram:{name:"instagram",url:"/auth/instagram",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["scope"],scope:["basic"],scopeDelimiter:"+",authorizationEndpoint:"https://api.instagram.com/oauth/authorize"},linkedin:{name:"linkedin",url:"/auth/linkedin",authorizationEndpoint:"https://www.linkedin.com/uas/oauth2/authorization",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["state"],scope:["r_emailaddress"],scopeDelimiter:" ",state:"STATE",type:"2.0",popupOptions:{width:527,height:582}},twitter:{name:"twitter",url:"/auth/twitter",authorizationEndpoint:"https://api.twitter.com/oauth/authenticate",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,type:"1.0",popupOptions:{width:495,height:645}},twitch:{name:"twitch",url:"/auth/twitch",authorizationEndpoint:"https://api.twitch.tv/kraken/oauth2/authorize",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["scope"],scope:["user_read"],scopeDelimiter:" ",display:"popup",type:"2.0",popupOptions:{width:500,height:560}},live:{name:"live",url:"/auth/live",authorizationEndpoint:"https://login.live.com/oauth20_authorize.srf",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,requiredUrlParams:["display","scope"],scope:["wl.emails"],scopeDelimiter:" ",display:"popup",type:"2.0",popupOptions:{width:500,height:560}},yahoo:{name:"yahoo",url:"/auth/yahoo",authorizationEndpoint:"https://api.login.yahoo.com/oauth2/request_auth",redirectUri:e.location.origin||e.location.protocol+"//"+e.location.host,scope:[],scopeDelimiter:",",type:"2.0",popupOptions:{width:559,height:519}}}}).provider("$auth",["SatellizerConfig",function(e){Object.defineProperties(this,{httpInterceptor:{get:function(){return e.httpInterceptor},set:function(t){e.httpInterceptor=t}},baseUrl:{get:function(){return e.baseUrl},set:function(t){e.baseUrl=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}},tokenRoot:{get:function(){return e.tokenRoot},set:function(t){e.tokenRoot=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}},authHeader:{get:function(){return e.authHeader},set:function(t){e.authHeader=t}},authToken:{get:function(){return e.authToken},set:function(t){e.authToken=t}},withCredentials:{get:function(){return e.withCredentials},set:function(t){e.withCredentials=t}},cordova:{get:function(){return e.cordova},set:function(t){e.cordova=t}},storageType:{get:function(){return e.storageType},set:function(t){e.storageType=t}}}),t.forEach(Object.keys(e.providers),function(o){this[o]=function(n){return t.extend(e.providers[o],n)}},this);var o=function(o){e.providers[o.name]=e.providers[o.name]||{},t.extend(e.providers[o.name],o)};this.oauth1=function(t){o(t),e.providers[t.name].type="1.0"},this.oauth2=function(t){o(t),e.providers[t.name].type="2.0"},this.$get=["$q","SatellizerShared","SatellizerLocal","SatellizerOauth",function(e,t,o,n){var r={};return r.login=function(e,t){return o.login(e,t)},r.signup=function(e,t){return o.signup(e,t)},r.logout=function(){return t.logout()},r.authenticate=function(e,t){return n.authenticate(e,t)},r.link=function(e,t){return n.authenticate(e,t)},r.unlink=function(e,t){return n.unlink(e,t)},r.isAuthenticated=function(){return t.isAuthenticated()},r.getToken=function(){return t.getToken()},r.setToken=function(e){t.setToken({access_token:e})},r.removeToken=function(){return t.removeToken()},r.getPayload=function(){return t.getPayload()},r.setStorageType=function(e){return t.setStorageType(e)},r}]}]).factory("SatellizerShared",["$q","$window","SatellizerConfig","SatellizerStorage",function(n,r,i,a){var u={},l=i.tokenPrefix?[i.tokenPrefix,i.tokenName].join("_"):i.tokenName;return u.getToken=function(){return a.get(l)},u.getPayload=function(){var t=a.get(l);if(t&&3===t.split(".").length)try{var n=t.split(".")[1],r=n.replace(/-/g,"+").replace(/_/g,"/");return JSON.parse(decodeURIComponent(escape(e.atob(r))))}catch(i){return o}},u.setToken=function(e){if(!e)return console.warn("Can't set token without passing a value");var o,n=e&&e.access_token;if(n&&(t.isObject(n)&&t.isObject(n.data)?e=n:t.isString(n)&&(o=n)),!o&&e){var r=i.tokenRoot&&i.tokenRoot.split(".").reduce(function(e,t){return e[t]},e.data);o=r?r[i.tokenName]:e.data[i.tokenName]}if(!o){var u=i.tokenRoot?i.tokenRoot+"."+i.tokenName:i.tokenName;return console.warn('Expecting a token named "'+u)}a.set(l,o)},u.removeToken=function(){a.remove(l)},u.isAuthenticated=function(){var e=a.get(l);if(e){if(3===e.split(".").length)try{var t=e.split(".")[1],o=t.replace(/-/g,"+").replace(/_/g,"/"),n=JSON.parse(r.atob(o)).exp;if(n){var i=Math.round((new Date).getTime()/1e3)>=n;return i?(a.remove(l),!1):!0}}catch(u){return!0}return!0}return!1},u.logout=function(){return a.remove(l),n.when()},u.setStorageType=function(e){i.storageType=e},u}]).factory("SatellizerOauth",["$q","$http","SatellizerConfig","SatellizerUtils","SatellizerShared","SatellizerOauth1","SatellizerOauth2",function(e,t,o,n,r,i,a){var u={};return u.authenticate=function(t,n){var u="1.0"===o.providers[t].type?new i:new a,l=e.defer();return u.open(o.providers[t],n||{}).then(function(e){r.setToken(e,!1),l.resolve(e)})["catch"](function(e){l.reject(e)}),l.promise},u.unlink=function(e,r){return r=r||{},r.url=r.url?r.url:n.joinUrl(o.baseUrl,o.unlinkUrl),r.data={provider:e}||r.data,r.method=r.method||"POST",t(r)},u}]).factory("SatellizerLocal",["$http","SatellizerUtils","SatellizerShared","SatellizerConfig",function(e,t,o,n){var r={};return r.login=function(r,i){return i=i||{},i.url=i.url?i.url:t.joinUrl(n.baseUrl,n.loginUrl),i.data=r||i.data,i.method=i.method||"POST",e(i).then(function(e){return o.setToken(e),e})},r.signup=function(o,r){return r=r||{},r.url=r.url?r.url:t.joinUrl(n.baseUrl,n.signupUrl),r.data=o||r.data,r.method=r.method||"POST",e(r)},r}]).factory("SatellizerOauth2",["$q","$http","$window","SatellizerPopup","SatellizerUtils","SatellizerConfig","SatellizerStorage",function(e,o,n,r,i,a,u){return function(){var n={},l={defaultUrlParams:["response_type","client_id","redirect_uri"],responseType:"code",responseParams:{code:"code",clientId:"clientId",redirectUri:"redirectUri"}};return n.open=function(o,p){l=i.merge(o,l);var c,s,h=l.name+"_state";return t.isFunction(l.state)?u.set(h,l.state()):t.isString(l.state)&&u.set(h,l.state),c=[l.authorizationEndpoint,n.buildQueryString()].join("?"),s=a.cordova?r.open(c,l.name,l.popupOptions,l.redirectUri).eventListener(l.redirectUri):r.open(c,l.name,l.popupOptions,l.redirectUri).pollPopup(),s.then(function(t){return"token"===l.responseType?t:t.state&&t.state!==u.get(h)?e.reject('OAuth "state" mismatch'):n.exchangeForToken(t,p)})},n.exchangeForToken=function(e,n){var r=t.extend({},n);t.forEach(l.responseParams,function(t,o){switch(o){case"code":r[t]=e.code;break;case"clientId":r[t]=l.clientId;break;case"redirectUri":r[t]=l.redirectUri;break;default:r[t]=e[o]}}),e.state&&(r.state=e.state);var u=a.baseUrl?i.joinUrl(a.baseUrl,l.url):l.url;return o.post(u,r,{withCredentials:a.withCredentials})},n.buildQueryString=function(){var e=[],o=["defaultUrlParams","requiredUrlParams","optionalUrlParams"];return t.forEach(o,function(o){t.forEach(l[o],function(o){var n=i.camelCase(o),r=t.isFunction(l[o])?l[o]():l[n];if("state"===o){var a=l.name+"_state";r=encodeURIComponent(u.get(a))}"scope"===o&&Array.isArray(r)&&(r=r.join(l.scopeDelimiter),l.scopePrefix&&(r=[l.scopePrefix,r].join(l.scopeDelimiter))),e.push([o,r])})}),e.map(function(e){return e.join("=")}).join("&")},n}}]).factory("SatellizerOauth1",["$q","$http","SatellizerPopup","SatellizerConfig","SatellizerUtils",function(e,o,n,r,i){return function(){var e={},a={url:null,name:null,popupOptions:null,redirectUri:null,authorizationEndpoint:null};return e.open=function(u,l){t.extend(a,u);var p,c=r.baseUrl?i.joinUrl(r.baseUrl,a.url):a.url;return r.cordova||(p=n.open("",a.name,a.popupOptions,a.redirectUri)),o.post(c,a).then(function(t){r.cordova?p=n.open([a.authorizationEndpoint,e.buildQueryString(t.data)].join("?"),a.name,a.popupOptions,a.redirectUri):p.popupWindow.location=[a.authorizationEndpoint,e.buildQueryString(t.data)].join("?");var o=r.cordova?p.eventListener(a.redirectUri):p.pollPopup();return o.then(function(t){return e.exchangeForToken(t,l)})})},e.exchangeForToken=function(e,n){var u=t.extend({},n,e),l=r.baseUrl?i.joinUrl(r.baseUrl,a.url):a.url;return o.post(l,u,{withCredentials:r.withCredentials})},e.buildQueryString=function(e){var o=[];return t.forEach(e,function(e,t){o.push(encodeURIComponent(t)+"="+encodeURIComponent(e))}),o.join("&")},e}}]).factory("SatellizerPopup",["$q","$interval","$window","SatellizerConfig","SatellizerUtils",function(n,r,i,a,u){var l={};return l.url="",l.popupWindow=null,l.open=function(t,o,n){l.url=t;var r=l.stringifyOptions(l.prepareOptions(n)),i=a.cordova?"_blank":o;return l.popupWindow=e.open(t,i,r),e.popup=l.popupWindow,l.popupWindow&&l.popupWindow.focus&&l.popupWindow.focus(),l},l.eventListener=function(e){var o=n.defer();return l.popupWindow.addEventListener("loadstart",function(n){if(0===n.url.indexOf(e)){var r=document.createElement("a");if(r.href=n.url,r.search||r.hash){var i=r.search.substring(1).replace(/\/$/,""),a=r.hash.substring(1).replace(/\/$/,""),p=u.parseQueryString(a),c=u.parseQueryString(i);t.extend(c,p),c.error||o.resolve(c),l.popupWindow.close()}}}),l.popupWindow.addEventListener("loaderror",function(){o.reject("Authorization Failed")}),o.promise},l.pollPopup=function(){var e=n.defer(),i=r(function(){try{var n=document.location.host,a=l.popupWindow.location.host;if(a===n&&(l.popupWindow.location.search||l.popupWindow.location.hash)){var p=l.popupWindow.location.search.substring(1).replace(/\/$/,""),c=l.popupWindow.location.hash.substring(1).replace(/[\/$]/,""),s=u.parseQueryString(c),h=u.parseQueryString(p);t.extend(h,s),h.error||e.resolve(h),l.popupWindow.close(),r.cancel(i)}}catch(d){}(!l.popupWindow||l.popupWindow.closed||l.popupWindow.closed===o)&&r.cancel(i)},50);return e.promise},l.prepareOptions=function(e){e=e||{};var o=e.width||500,n=e.height||500;return t.extend({width:o,height:n,left:i.screenX+(i.outerWidth-o)/2,top:i.screenY+(i.outerHeight-n)/2.5},e)},l.stringifyOptions=function(e){var o=[];return t.forEach(e,function(e,t){o.push(t+"="+e)}),o.join(",")},l}]).service("SatellizerUtils",function(){this.camelCase=function(e){return e.replace(/([\:\-\_]+(.))/g,function(e,t,o,n){return n?o.toUpperCase():o})},this.parseQueryString=function(e){var o,n,r={};return t.forEach((e||"").split("&"),function(e){e&&(n=e.split("="),o=decodeURIComponent(n[0]),r[o]=t.isDefined(n[1])?decodeURIComponent(n[1]):!0)}),r},this.joinUrl=function(e,t){if(/^(?:[a-z]+:)?\/\//i.test(t))return t;var o=[e,t].join("/"),n=function(e){return e.replace(/[\/]+/g,"/").replace(/\/\?/g,"?").replace(/\/\#/g,"#").replace(/\:\//g,"://")};return n(o)},this.merge=function(e,t){var o={};for(var n in e)e.hasOwnProperty(n)&&(n in t&&"object"==typeof e[n]&&null!==n?o[n]=this.merge(e[n],t[n]):o[n]=e[n]);for(n in t)if(t.hasOwnProperty(n)){if(n in o)continue;o[n]=t[n]}return o}}).factory("SatellizerStorage",["$window","SatellizerConfig",function(e,t){var n=function(){try{var o=t.storageType in e&&null!==e[t.storageType];if(o){var n=Math.random().toString(36).substring(7);e[t.storageType].setItem(n,""),e[t.storageType].removeItem(n)}return o}catch(r){return!1}}();return n||console.warn("Satellizer Warning: "+t.storageType+" is not available."),{get:function(r){return n?e[t.storageType].getItem(r):o},set:function(r,i){return n?e[t.storageType].setItem(r,i):o},remove:function(r){return n?e[t.storageType].removeItem(r):o}}}]).factory("SatellizerInterceptor",["$q","SatellizerConfig","SatellizerStorage","SatellizerShared",function(e,t,o,n){return{request:function(e){if(e.skipAuthorization)return e;if(n.isAuthenticated()&&t.httpInterceptor){var r=t.tokenPrefix?t.tokenPrefix+"_"+t.tokenName:t.tokenName,i=o.get(r);t.authHeader&&t.authToken&&(i=t.authToken+" "+i),e.headers[t.authHeader]=i}return e},responseError:function(t){return e.reject(t)}}}]).config(["$httpProvider",function(e){e.interceptors.push("SatellizerInterceptor")}])}(window,window.angular); \ No newline at end of file