forked from sahat/satellizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
satellizer.min.js
6 lines (6 loc) · 13.7 KB
/
satellizer.min.js
1
2
3
4
5
6
/**
* Satellizer 0.12.5
* (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(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){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;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,"/"),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=o.baseUrl?n.joinUrl(o.baseUrl,o.unlinkUrl):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=n.baseUrl?t.joinUrl(n.baseUrl,n.loginUrl):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=n.baseUrl?t.joinUrl(n.baseUrl,n.signupUrl):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);