forked from sahat/satellizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
satellizer.min.js
1 lines (1 loc) · 10.3 KB
/
satellizer.min.js
1
!function(t,e){"use strict";e.module("satellizer",[]).constant("satellizer.config",{httpInterceptor:!0,loginOnSignup:!0,loginRedirect:"/",logoutRedirect:"/",signupRedirect:"/login",loginUrl:"/auth/login",signupUrl:"/auth/signup",loginRoute:"/login",signupRoute:"/signup",tokenName:"token",tokenPrefix:"satellizer",unlinkUrl:"/auth/unlink/",authHeader:"Authorization",providers:{google:{url:"/auth/google",authorizationEndpoint:"https://accounts.google.com/o/oauth2/auth",redirectUri:t.location.origin||t.location.protocol+"//"+t.location.host,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:t.location.origin+"/"||t.location.protocol+"//"+t.location.host+"/",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:t.location.origin||t.location.protocol+"//"+t.location.host,requiredUrlParams:["state"],scope:["r_emailaddress"],scopeDelimiter:" ",state:"STATE",type:"2.0",popupOptions:{width:527,height:582}},github:{url:"/auth/github",authorizationEndpoint:"https://github.com/login/oauth/authorize",redirectUri:t.location.origin||t.location.protocol+"//"+t.location.host,scope:[],scopeDelimiter:" ",type:"2.0",popupOptions:{width:1020,height:618}},yahoo:{url:"/auth/yahoo",authorizationEndpoint:"https://api.login.yahoo.com/oauth2/request_auth",redirectUri:t.location.origin||t.location.protocol+"//"+t.location.host,scope:[],scopeDelimiter:",",type:"2.0",popupOptions:{width:559,height:519}},twitter:{url:"/auth/twitter",type:"1.0",popupOptions:{width:495,height:645}},live:{url:"/auth/live",authorizationEndpoint:"https://login.live.com/oauth20_authorize.srf",redirectUri:t.location.origin||t.location.protocol+"//"+t.location.host,scope:["wl.basic"],scopeDelimiter:" ",requiredUrlParams:["display","scope"],display:"popup",type:"2.0",popupOptions:{width:500,height:560}}}}).provider("$auth",["satellizer.config",function(t){Object.defineProperties(this,{logoutRedirect:{get:function(){return t.logoutRedirect},set:function(e){t.logoutRedirect=e}},loginRedirect:{set:function(e){t.loginRedirect=e},get:function(){return t.loginRedirect}},signupRedirect:{get:function(){return t.signupRedirect},set:function(e){t.signupRedirect=e}},loginOnSignup:{get:function(){return t.loginOnSignup},set:function(e){t.loginOnSignup=e}},loginUrl:{get:function(){return t.loginUrl},set:function(e){t.loginUrl=e}},signupUrl:{get:function(){return t.signupUrl},set:function(e){t.signupUrl=e}},loginRoute:{get:function(){return t.loginRoute},set:function(e){t.loginRoute=e}},signupRoute:{get:function(){return t.signupRoute},set:function(e){t.signupRoute=e}},tokenName:{get:function(){return t.tokenName},set:function(e){t.tokenName=e}},tokenPrefix:{get:function(){return t.tokenPrefix},set:function(e){t.tokenPrefix=e}},unlinkUrl:{get:function(){return t.unlinkUrl},set:function(e){t.unlinkUrl=e}},authHeader:{get:function(){return t.authHeader},set:function(e){t.authHeader=e}}}),e.forEach(Object.keys(t.providers),function(n){this[n]=function(o){return e.extend(t.providers[n],o)}},this);var n=function(n){t.providers[n.name]=t.providers[n.name]||{},e.extend(t.providers[n.name],n)};this.oauth1=function(e){n(e),t.providers[e.name].type="1.0"},this.oauth2=function(e){n(e),t.providers[e.name].type="2.0"},this.$get=["$q","satellizer.shared","satellizer.local","satellizer.oauth",function(t,e,n,o){var r={};return r.authenticate=function(t,e){return o.authenticate(t,!1,e)},r.login=function(t){return n.login(t)},r.signup=function(t){return n.signup(t)},r.logout=function(){return e.logout()},r.isAuthenticated=function(){return e.isAuthenticated()},r.link=function(t,e){return o.authenticate(t,!0,e)},r.unlink=function(t){return o.unlink(t)},r.getToken=function(){return e.getToken()},r.setToken=function(t,n){e.setToken({access_token:t},n)},r.getPayload=function(){return e.getPayload()},r}]}]).factory("satellizer.shared",["$q","$window","$location","satellizer.config",function(t,e,n,o){var r={};return r.getToken=function(){var t=o.tokenPrefix?o.tokenPrefix+"_"+o.tokenName:o.tokenName;return e.localStorage[t]},r.getPayload=function(){var t=o.tokenPrefix?o.tokenPrefix+"_"+o.tokenName:o.tokenName,n=e.localStorage[t];if(n&&3===n.split(".").length){var r=n.split(".")[1],i=r.replace("-","+").replace("_","/");return JSON.parse(e.atob(i))}},r.setToken=function(t,r){var i=t.access_token||t.data[o.tokenName],u=o.tokenPrefix?o.tokenPrefix+"_"+o.tokenName:o.tokenName;if(!i)throw new Error('Expecting a token named "'+o.tokenName+'" but instead got: '+JSON.stringify(t.data));e.localStorage[u]=i,o.loginRedirect&&!r&&n.path(o.loginRedirect)},r.isAuthenticated=function(){var t=o.tokenPrefix?o.tokenPrefix+"_"+o.tokenName:o.tokenName,n=e.localStorage[t];if(n){if(3===n.split(".").length){var r=n.split(".")[1],i=r.replace("-","+").replace("_","/"),u=JSON.parse(e.atob(i)).exp;return Math.round((new Date).getTime()/1e3)<=u}return!0}return!1},r.logout=function(){var r=o.tokenPrefix?o.tokenPrefix+"_"+o.tokenName:o.tokenName;return delete e.localStorage[r],o.logoutRedirect&&n.path(o.logoutRedirect),t.when()},r}]).factory("satellizer.oauth",["$q","$http","satellizer.config","satellizer.shared","satellizer.Oauth1","satellizer.Oauth2",function(t,e,n,o,r,i){var u={};return u.authenticate=function(t,e,u){var a="1.0"===n.providers[t].type?new r:new i;return a.open(n.providers[t],u||{}).then(function(t){return o.setToken(t,e),t})},u.unlink=function(t){return e.get(n.unlinkUrl+t)},u}]).factory("satellizer.local",["$q","$http","$location","satellizer.utils","satellizer.shared","satellizer.config",function(t,e,n,o,r,i){var u={};return u.login=function(t){return e.post(i.loginUrl,t).then(function(t){return r.setToken(t),t})},u.signup=function(t){return e.post(i.signupUrl,t).then(function(t){return i.loginOnSignup?r.setToken(t):n.path(i.signupRedirect),t})},u}]).factory("satellizer.Oauth2",["$q","$http","satellizer.popup","satellizer.utils","satellizer.config",function(t,n,o,r){return function(){var t={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"},i={};return i.open=function(n,r){e.extend(t,n);var u=i.buildUrl();return o.open(u,t.popupOptions).then(function(e){return"token"===t.responseType?e:i.exchangeForToken(e,r)})},i.exchangeForToken=function(o,r){var i=e.extend({},r,{code:o.code,clientId:t.clientId,redirectUri:t.redirectUri});return n.post(t.url,i)},i.buildUrl=function(){var e=t.authorizationEndpoint,n=i.buildQueryString();return[e,n].join("?")},i.buildQueryString=function(){var n=[],o=["defaultUrlParams","requiredUrlParams","optionalUrlParams"];return e.forEach(o,function(o){e.forEach(t[o],function(e){var o=r.camelCase(e),i=t[o];"scope"===e&&Array.isArray(i)&&(i=i.join(t.scopeDelimiter),t.scopePrefix&&(i=[t.scopePrefix,i].join(t.scopeDelimiter))),n.push([e,i])})}),n.map(function(t){return t.join("=")}).join("&")},i}}]).factory("satellizer.Oauth1",["$q","$http","satellizer.popup",function(t,n,o){return function(){var t={url:null,name:null,popupOptions:null},r={};return r.open=function(n,i){return e.extend(t,n),o.open(t.url,t.popupOptions).then(function(t){return r.exchangeForToken(t,i)})},r.exchangeForToken=function(o,i){var u=e.extend({},i,o),a=r.buildQueryString(u);return n.get(t.url+"?"+a)},r.buildQueryString=function(t){var n=[];return e.forEach(t,function(t,e){n.push(encodeURIComponent(e)+"="+encodeURIComponent(t))}),n.join("&")},r}}]).factory("satellizer.popup",["$q","$interval","$window","$location","satellizer.utils",function(n,o,r,i,u){var a=null,l=null,c={};return c.popupWindow=a,c.open=function(e,n){var o=c.stringifyOptions(c.prepareOptions(n||{}));return a=t.open(e,"_blank",o),a&&a.focus&&a.focus(),c.pollPopup()},c.pollPopup=function(){var t=n.defer();return l=o(function(){try{if(a.document.domain===document.domain&&(a.location.search||a.location.hash)){var n=a.location.search.substring(1).replace(/\/$/,""),r=a.location.hash.substring(1).replace(/\/$/,""),i=u.parseQueryString(r),c=u.parseQueryString(n);e.extend(c,i),c.error?t.reject({error:c.error}):t.resolve(c),a.close(),o.cancel(l)}}catch(p){}a.closed&&(o.cancel(l),t.reject({data:"Authorization Failed"}))},35),t.promise},c.prepareOptions=function(t){var n=t.width||500,o=t.height||500;return e.extend({width:n,height:o,left:r.screenX+(r.outerWidth-n)/2,top:r.screenY+(r.outerHeight-o)/2.5},t)},c.stringifyOptions=function(t){var n=[];return e.forEach(t,function(t,e){n.push(e+"="+t)}),n.join(",")},c}]).service("satellizer.utils",function(){this.camelCase=function(t){return t.replace(/([\:\-\_]+(.))/g,function(t,e,n,o){return o?n.toUpperCase():n})},this.parseQueryString=function(t){var n,o,r={};return e.forEach((t||"").split("&"),function(t){t&&(o=t.split("="),n=decodeURIComponent(o[0]),r[n]=e.isDefined(o[1])?decodeURIComponent(o[1]):!0)}),r}}).config(["$httpProvider","satellizer.config",function(t,e){e.httpInterceptor&&t.interceptors.push(["$q",function(t){var n=e.tokenPrefix?e.tokenPrefix+"_"+e.tokenName:e.tokenName;return{request:function(t){var o=localStorage.getItem(n);return o&&(o="Authorization"===e.authHeader?"Bearer "+o:o,t.headers[e.authHeader]=o),t},responseError:function(e){return t.reject(e)}}}])}])}(window,window.angular),function(){function t(t){this.message=t}var e="undefined"!=typeof exports?exports:this,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";t.prototype=new Error,t.prototype.name="InvalidCharacterError",e.btoa||(e.btoa=function(e){for(var o,r,i=String(e),u=0,a=n,l="";i.charAt(0|u)||(a="=",u%1);l+=a.charAt(63&o>>8-u%1*8)){if(r=i.charCodeAt(u+=.75),r>255)throw new t("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");o=o<<8|r}return l}),e.atob||(e.atob=function(e){var o=String(e).replace(/=+$/,"");if(o.length%4==1)throw new t("'atob' failed: The string to be decoded is not correctly encoded.");for(var r,i,u=0,a=0,l="";i=o.charAt(a++);~i&&(r=u%4?64*r+i:i,u++%4)?l+=String.fromCharCode(255&r>>(-2*u&6)):0)i=n.indexOf(i);return l})}();