forked from sahat/satellizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsatellizer.min.js
1 lines (1 loc) · 6.5 KB
/
satellizer.min.js
1
!function(e,t){"use strict";var n={logoutRedirect:"/",loginRedirect:"/",loginUrl:"/auth/login",signupUrl:"/auth/signup",signupRedirect:"/login",loginRoute:"/login",signupRoute:"/signup",user:"currentUser"},o={google:{url:"/auth/google",authorizationEndpoint:"https://accounts.google.com/o/oauth2/auth",redirectUri:e.location.origin,scope:"openid profile email",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"}};t.module("Satellizer",[]).provider("$auth",function(){this.config=n,this.providers=o,this.facebook=function(e){t.extend(o.facebook,e)},this.google=function(e){t.extend(o.google,e)},this.linkedin=function(e){t.extend(o.linkedin,e)},this.github=function(e){t.extend(o.github,e)},this.twitter=function(e){t.extend(o.twitter,e)},this.oauthBase=function(e){o[e.name]=o[e.name]||{},t.extend(o[e.name],e)},this.oauth1=function(e){this.oauthBase(e),o[e.name].type="1.0"},this.oauth2=function(e){this.oauthBase(e),o[e.name].type="2.0"},this.$get=["$q","Oauth1","Oauth2","Local",function(e,t,n,r){var i={};return i.authenticate=function(i){var u=e.defer(),a="1.0"===o[i].type?t:n;return a.open(o[i]).then(function(e){r.parseUser(e.token,u)}).catch(function(e){u.reject(e)}),u.promise},i.login=function(e){return r.login(e)},i.signup=function(e){return r.signup(e)},i.logout=function(){return r.logout()},i.isAuthenticated=function(){return r.isAuthenticated()},i}]}).factory("Local",["$q","$http","$rootScope","$location",function(t,o,r,i){var u={};return u.parseUser=function(t,o){var u=JSON.parse(e.atob(t.split(".")[1]));localStorage.setItem("jwtToken",t),r[n.user]=u.user,i.path(n.loginRedirect),o.resolve(u.user)},u.login=function(e){var r=t.defer();return o.post(n.loginUrl,e).then(function(e){u.parseUser(e.data.token,r)}).catch(function(e){r.reject(e)}),r.promise},u.signup=function(e){var r=t.defer();return o.post(n.signupUrl,e).then(function(){i.path(n.signupRedirect),r.resolve()}).catch(function(e){r.reject(e)}),r.promise},u.logout=function(){var e=t.defer();return delete r[n.user],localStorage.removeItem("jwtToken"),i.path(n.logoutRedirect),e.resolve(),e.promise},u.isAuthenticated=function(){return Boolean(r.currentUser)},u}]).factory("Oauth2",["$q","$http","Utils","Popup",function(e,n,o,r){var i={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(i,n);var o=e.defer(),a=u.buildUrl();return r.open(a,i.popupOptions).then(function(e){u.exchangeForToken(e).then(function(e){o.resolve(e.data)}).catch(function(e){o.reject(e)})}),o.promise},u.exchangeForToken=function(e){return n.post(i.url,{code:e.code,clientId:i.clientId,redirectUri:i.redirectUri})},u.buildUrl=function(){var e=i.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(i[n],function(t){var n=o.camelCase(t),r=i[n];e.push([t,encodeURIComponent(r)])})}),e.map(function(e){return e.join("=")}).join("&")},u}]).factory("Oauth1",["$q","$http","Popup",function(e,n,o){var r={url:null,name:null,popupOptions:null},i={};return i.open=function(n){t.extend(r,n);var u=e.defer();return o.open(r.url).then(function(e){i.exchangeForToken(e).then(function(e){u.resolve(e.data)})}),u.promise},i.exchangeForToken=function(e){return e=i.buildQueryString(e),n.get(r.url+"?"+e)},i.buildQueryString=function(e){var n=[];return t.forEach(e,function(e,t){n.push(encodeURIComponent(t)+"="+encodeURIComponent(e))}),n.join("&")},i}]).factory("Popup",["$q","$interval","$window",function(e,n,o){var r=null,i=null,u={};return u.popupWindow=r,u.open=function(t,n){var i=e.defer(),a=u.stringifyOptions(u.prepareOptions(n||{}));return r=o.open(t,"Satellizer",a),r.focus(),u.postMessageHandler(i),u.pollPopup(i),i.promise},u.pollPopup=function(e){i=n(function(){r.closed&&(n.cancel(i),e.reject("Popup was closed by the user"))},35)},u.postMessageHandler=function(e){o.addEventListener("message",function(t){t.origin===o.location.origin&&(r.close(),e.resolve(t.data))},!1)},u.prepareOptions=function(e){var n=e.width||500,r=e.height||500;return t.extend({width:n,height:r,left:o.screenX+(o.outerWidth-n)/2,top:o.screenY+(o.outerHeight-r)/2.5},e)},u.stringifyOptions=function(e){var n=[];return t.forEach(e,function(e,t){n.push(t+"="+e)}),n.join(",")},u}]).factory("RunBlock",["$rootScope","$window","$location","Utils",function(e,t,n,o){return{run:function(){var r=t.localStorage.jwtToken;if(r){var i=JSON.parse(t.atob(r.split(".")[1]));e.currentUser=i.user}var u=t.location.search.substring(1),a=Object.keys(n.search()).length?n.search():o.parseQueryString(u);t.opener&&t.opener.location.origin===t.location.origin&&(a.oauth_token&&a.oauth_verifier?t.opener.postMessage({oauth_token:a.oauth_token,oauth_verifier:a.oauth_verifier},"*"):a.code&&t.opener.postMessage({code:a.code},"*"))}}}]).service("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,r={};return t.forEach((e||"").split("&"),function(e){e&&(o=e.split("="),n=decodeURIComponent(o[0]),r[n]=t.isDefined(o[1])?decodeURIComponent(o[1]):!0)}),r}}).config(["$httpProvider",function(e){e.interceptors.push(["$q","$window","$location",function(e,t,o){return{request:function(e){return t.localStorage.jwtToken&&(e.headers.Authorization="Bearer "+t.localStorage.jwtToken),e},responseError:function(r){return 401===r.status&&(delete t.localStorage.jwtToken,o.path(n.loginRoute)),e.reject(r)}}}])}]).run(["RunBlock",function(e){e.run()}])}(window,window.angular);