forked from sahat/satellizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
satellizer.min.js
6 lines (6 loc) · 11.6 KB
/
satellizer.min.js
1
2
3
4
5
6
/**
* Satellizer 0.9.2
* (c) 2015 Sahat Yalkabov
* License: MIT
*/
!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",tokenRoot:!1,tokenName:"token",tokenPrefix:"satellizer",unlinkUrl:"/auth/unlink/",unlinkMethod:"get",authHeader:"Authorization",withCredentials:!0,providers:{google:{name:"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:{name:"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:580,height:400}},linkedin:{name:"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:{name:"github",url:"/auth/github",authorizationEndpoint:"https://github.com/login/oauth/authorize",redirectUri:t.location.origin||t.location.protocol+"//"+t.location.host,optionalUrlParams:["scope"],scope:["user:email"],scopeDelimiter:" ",type:"2.0",popupOptions:{width:1020,height:618}},yahoo:{name:"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:{name:"twitter",url:"/auth/twitter",type:"1.0",popupOptions:{width:495,height:645}},live:{name:"live",url:"/auth/live",authorizationEndpoint:"https://login.live.com/oauth20_authorize.srf",redirectUri:t.location.origin||t.location.protocol+"//"+t.location.host,scope:["wl.emails"],scopeDelimiter:" ",requiredUrlParams:["display","scope"],display:"popup",type:"2.0",popupOptions:{width:500,height:560}}}}).provider("$auth",["satellizer.config",function(t){Object.defineProperties(this,{loginOnSignup:{get:function(){return t.loginOnSignup},set:function(e){t.loginOnSignup=e}},httpInterceptor:{get:function(){return t.httpInterceptor},set:function(e){t.httpInterceptor=e}},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}},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}},tokenRoot:{get:function(){return t.tokenRoot},set:function(e){t.tokenRoot=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}},withCredentials:{get:function(){return t.withCredentials},set:function(e){t.withCredentials=e}},unlinkMethod:{get:function(){return t.unlinkMethod},set:function(e){t.unlinkMethod=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.removeToken=function(){return e.removeToken()},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;i=t.access_token||(o.tokenRoot&&t.data[o.tokenRoot]?t.data[o.tokenRoot][o.tokenName]:t.data[o.tokenName]);var a=o.tokenPrefix?o.tokenPrefix+"_"+o.tokenName:o.tokenName;if(!i)throw a=o.tokenRoot?o.tokenRoot+"."+o.tokenName:o.tokenName,new Error('Expecting a token named "'+a+'" but instead got: '+JSON.stringify(t.data));e.localStorage[a]=i,o.loginRedirect&&!r&&n.path(o.loginRedirect)},r.removeToken=function(){var t=o.tokenPrefix?o.tokenPrefix+"_"+o.tokenName:o.tokenName;delete e.localStorage[t]},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("_","/"),a=JSON.parse(e.atob(i)).exp;if(a)return Math.round((new Date).getTime()/1e3)<=a}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.url(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 a={};return a.authenticate=function(e,a,u){var l="1.0"===n.providers[e].type?new r:new i,c=t.defer();return l.open(n.providers[e],u||{}).then(function(t){o.setToken(t,a),c.resolve(t)}).catch(function(t){c.reject(t)}),c.promise},a.unlink=function(t){return"get"===n.unlinkMethod?e.get(n.unlinkUrl+t):"post"===n.unlinkMethod?e.post(n.unlinkUrl,t):void 0},a}]).factory("satellizer.local",["$q","$http","$location","satellizer.utils","satellizer.shared","satellizer.config",function(t,e,n,o,r,i){var a={};return a.login=function(t){return e.post(i.loginUrl,t).then(function(t){return r.setToken(t),t})},a.signup=function(t){return e.post(i.signupUrl,t).then(function(t){return i.loginOnSignup?r.setToken(t):i.signupRedirect&&n.path(i.signupRedirect),t})},a}]).factory("satellizer.Oauth2",["$q","$http","$window","satellizer.popup","satellizer.utils","satellizer.config",function(t,n,o,r,i,a){return function(){var u={url:null,name:null,state:null,scope:null,scopeDelimiter:null,clientId:null,redirectUri:null,popupOptions:null,authorizationEndpoint:null,responseParams:null,requiredUrlParams:null,optionalUrlParams:null,defaultUrlParams:["response_type","client_id","redirect_uri"],responseType:"code"},l={};return l.open=function(n,i){e.extend(u,n);var a=u.name+"_state";e.isFunction(u.state)?o.localStorage[a]=u.state():e.isString(u.state)&&(o.localStorage[a]=u.state);var c=u.authorizationEndpoint+"?"+l.buildQueryString();return r.open(c,u.popupOptions).then(function(e){return"token"===u.responseType?e:e.state&&e.state!==o.localStorage[a]?t.reject("OAuth 2.0 state parameter mismatch."):l.exchangeForToken(e,i)})},l.exchangeForToken=function(t,o){var r=e.extend({},o,{code:t.code,clientId:u.clientId,redirectUri:u.redirectUri});return t.state&&(r.state=t.state),e.forEach(u.responseParams,function(e){r[e]=t[e]}),n.post(u.url,r,{withCredentials:a.withCredentials})},l.buildQueryString=function(){var t=[],n=["defaultUrlParams","requiredUrlParams","optionalUrlParams"];return e.forEach(n,function(n){e.forEach(u[n],function(e){var n=i.camelCase(e),r=u[n];if("state"===e){var a=u.name+"_state";r=o.localStorage[a]}"scope"===e&&Array.isArray(r)&&(r=r.join(u.scopeDelimiter),u.scopePrefix&&(r=[u.scopePrefix,r].join(u.scopeDelimiter))),t.push([e,r])})}),t.map(function(t){return t.join("=")}).join("&")},l}}]).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 a=e.extend({},i,o),u=r.buildQueryString(a);return n.get(t.url+"?"+u)},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,a){var u=null,l=null,c={};return c.popupWindow=u,c.open=function(e,n){var o=c.stringifyOptions(c.prepareOptions(n||{}));return u=t.open(e,"_blank",o),u&&u.focus&&u.focus(),c.pollPopup()},c.pollPopup=function(){var t=n.defer();return l=o(function(){try{if(u.document.domain===document.domain&&(u.location.search||u.location.hash)){var n=u.location.search.substring(1).replace(/\/$/,""),r=u.location.hash.substring(1).replace(/\/$/,""),i=a.parseQueryString(r),c=a.parseQueryString(n);e.extend(c,i),c.error?t.reject({error:c.error}):t.resolve(c),u.close(),o.cancel(l)}}catch(s){}u.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){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&&e.httpInterceptor&&(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),a=0,u=n,l="";i.charAt(0|a)||(u="=",a%1);l+=u.charAt(63&o>>8-a%1*8)){if(r=i.charCodeAt(a+=.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,a=0,u=0,l="";i=o.charAt(u++);~i&&(r=a%4?64*r+i:i,a++%4)?l+=String.fromCharCode(255&r>>(-2*a&6)):0)i=n.indexOf(i);return l})}();