/** * angular-login * @version v0.0.12 - 2015-09-12 * @link https://github.com/aronvaughan/angular-login * @author <> * @license MIT License, http://www.opensource.org/licenses/MIT */ "use strict";var GrailsSpringSecurityRestAuthManager={name:"GrailsSpringSecurityRestAuthManager",useAuthTokenHeader:!0,logger:void 0,tokenName:"X-Auth-Token",jsonResponseTokenName:"access_token",tokenValidationUrl:"/api/validate",getName:function(){return this.name},setLog:function(a){this.logger=a.getLogger("AuthManager")},getHeader:function(a){var b={};return this.getTokenValues(a)&&(b[this.tokenName]=this.getTokenValues(a)[0]),b},setAuthOnRequest:function(a,b){var c=a.authToken;this.useAuthTokenHeader?(this.logger.debug("TOKEN_MANAGER, setting X-Auth-Token header",c),b.headers["X-Auth-Token"]=c):(this.logger.debug("use url token"),b.url=b.url+"?token="+c)},isTokenValid:function(a,b,c){var d=this;a({method:"GET",url:this.tokenValidationUrl,data:"check",headers:this.getHeader(b),ignoreAuthModule:!0,withCredentials:!0}).success(function(a,b,c,e){return d.logger.info("back from validate check, valid: TRUE!",[a,b,c,e]),!0}).error(function(a,e,f,g){return d.logger.info("back from validate check, valid: FALSE!",[a,e,f,g]),d.clear(c,b),!1})},isTokenAvailable:function(a,b){return this.load(b,a),this.logger.debug("isTokenAvailable?",[b,a.authToken]),angular.isDefined(a.authToken)},getTokenValues:function(a){return[a.authToken]},load:function(a,b){var c;a?c=a.get("authToken"):console.log("TOKEN_MANAGER: WARN $cookieStore is undefined"),void 0!==c&&(console.log("TOKEN_MANAGER, load, got valid value from cookie",c),b.authToken=c)},save:function(a,b,c){this.logger.debug("save, authentication token: "+a[this.jsonResponseTokenName],a),localStorage.authToken=a[this.jsonResponseTokenName],b.authToken=a[this.jsonResponseTokenName],c.put("authToken",a[this.jsonResponseTokenName])},clear:function(a,b){this.logger.debug("logout success, clearing tokens"),localStorage.clear(),a.remove("authToken"),b.authToken=void 0},getLocalToken:function(){var a=localStorage.authToken;return this.logger.debug("AUTH TOKEN:"+a),a},getHttpConfig:function(){return{headers:{"X-Auth-Token":this.getLocalToken()}}},getAuthenticateHttpConfig:function(){return{ignoreAuthModule:!0}},configUpdateFunction:function(a){return a.headers["X-Auth-Token"]||(this.logger.debug("X-Auth-Token not on original request; adding it"),a.headers["X-Auth-Token"]=this.getLocalToken()),a}},SpringSecurityAuthManager={name:"SpringSecurityAuthManager",useAuthTokenHeader:!0,userUrlHeader:!1,logger:void 0,tokenName:"X-authtoken",getName:function(){return this.name},setLog:function(a){this.logger=a.getLogger("AuthManager")},getHeader:function(a){var b={};return this.getTokenValues(a)&&(b[this.tokenName]=this.getTokenValues(a)[0]),b},setAuthOnRequest:function(a,b){var c=a[this.tokenName];this.useAuthTokenHeader?(this.logger.debug("TOKEN_MANAGER, setting "+this.tokenName+" header",c),b.headers[this.tokenName]=c):this.useUrlHeader&&(this.logger.debug("use url token"),b.url=b.url+"?jsessionid="+c)},isTokenValid:function(){return!0},isTokenAvailable:function(a,b,c){return this.logger.debug("isTokenAvailable?",c),this.load(b,a),this.logger.debug("isTokenAvailable?",[b,a[this.tokenName],angular.isDefined(a[this.tokenName])]),angular.isDefined(a[this.tokenName])},getTokenValues:function(a){return[a[this.tokenName]]},load:function(a,b){this.logger.debug("[AuthManager] load, cookieStore",[a,a.get(this.tokenName)]),this.logger.debug("load - BROWSER COOKIES!!!",document.cookie);var c;a?c=a.get(this.tokenName):(console.log("TOKEN_MANAGER: WARN $cookieStore is undefined, trying localstorage"),c=localStorage[this.tokenName]),void 0!==c?(console.log("TOKEN_MANAGER, load, got valid value from cookie",c),b[this.tokenName]=c):console.log("TOKEN_MANAGER, load, no valid value from cookie",c)},save:function(a,b,c,d){this.logger.debug("save, authentication token: "+d(this.tokenName)),this.logger.debug("save - BROWSER COOKIES!!!",document.cookie),localStorage[this.tokenName]=d(this.tokenName),b[this.tokenName]=d(this.tokenName),c.put(this.tokenName,d(this.tokenName))},clear:function(a,b){this.logger.debug("logout success, clearing tokens"),localStorage.clear(),a.remove(this.tokenName),b[this.tokenName]=void 0},getLocalToken:function(){var a=localStorage[this.tokenName];return this.logger.debug("AUTH TOKEN:"+a),a},getHttpConfig:function(){var a={};return a[this.tokenName]=this.getLocalToken(),a},getAuthenticateHttpConfig:function(){return{ignoreAuthModule:!0}},configUpdateFunction:function(a){return a.headers[this.tokenName]||(this.logger.debug(this.tokenName+" not on original request; adding it"),a.headers[this.tokenName]=this.getLocalToken()),a}},AVaughanLoginConfig={restCallsWillContain:"api",loginUrlForRemote:"/api/login",loginUserLabel:"username",loginPassLabel:"password",logoutUrlForRemote:"/api/logout",redirectIfTokenNotFound:!1,redirectIfTokenNotFoundUrl:"/login",redirectAfterLogin:!1,defaultUrlAfterLogin:"/",postType:"JSON",authManager:void 0,setAuthManager:function(a){this.authManager=_.extend(a,{})}},AVaughanLoginConfigFactory={create:function(a,b){var c=_.extend(AVaughanLoginConfig,a);c.setAuthManager(b)}},AVaughanLogin=AVaughanLogin||{loginConfig:_.extend(AVaughanLoginConfig,{}),getAuthManager:function(){return this.loginConfig.authManager},authService:void 0,logger:void 0,$rootScope:void 0,construct:function(a){a&&(this.loginConfig=a)},initialize:function(a,b,c){this.logger=b.getLogger("AVaughanLogin"),this.logger.debug("avaughan.login get called",a),this.setAuthService(a),this.getAuthManager().setLog(b),this.$rootScope=c},interceptHttpRequests:function(a){a.interceptors.push(this.getRequestInterceptor())},setAuthService:function(a){a&&(this.logger.debug("setting authService ",a),this.authService=a)},toLogin:function(a){a.path(this.loginConfig.redirectIfTokenNotFoundUrl)},parseUri:function(a){for(var b=this.parseUriOptions,c=b.parser[b.strictMode?"strict":"loose"].exec(a),d={},e=14;e--;)d[b.key[e]]=c[e]||"";return d[b.q.name]={},d[b.key[12]].replace(b.q.parser,function(a,c,e){c&&(d[b.q.name][c]=e)}),d},parseUriOptions:{strictMode:!1,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}},login:function(a,b,c,d,e,f){this.logger.debug("service login called ",["location",f,"http",c,"$rootScope",d,"$cookieStore",e]);var g=this,h={"Content-Type":"application/json"},i={};i[this.loginConfig.loginUserLabel]=a,i[this.loginConfig.loginPassLabel]=b,"FORM"===this.loginConfig.postType&&(h={"Content-Type":"application/x-www-form-urlencoded"},i=this.loginConfig.loginUserLabel+"="+a+"&"+this.loginConfig.loginPassLabel+"="+b),this.logger.debug("Post type: "+this.loginConfig.postType+"value: ",i),c({method:"POST",url:this.loginConfig.loginUrlForRemote,data:i,headers:h,ignoreAuthModule:!0,withCredentials:!0}).success(function(b,c,h,i){if(g.logger.info("Login successful for user: ",[a,b,c,h,i,g.loginConfig]),g.getAuthManager().save(b,d,e,h),g.authService.loginConfirmed(b,g.configUpdateFunction),g.loginConfig.redirectAfterLogin){var j=f.$$absUrl;if(g.logger.info("should redirect after login",[f,j]),j.indexOf("originalUrl=")>-1){var k=g.parseUri(j),l=k.queryKey.originalUrl,m=decodeURIComponent(l);g.logger.info("original url found",m),f.search("originalUrl",null),f.url(m)}else g.logger.info("using default url to redirect after login",f.path().originalUrl),f.path(g.loginConfig.defaultUrlAfterLogin)}}).error(function(a,b,c,d){g.logger.error("login error: ",a,b,c,d),g.loginFailed(a)})},loginConfirmed:function(a){this.logger.info("loginConfirmed",a),this.authService.loginConfirmed(a,this.loginConfig.configUpdateFunction)},loginFailed:function(a){this.logger.warn("broadcasting login failed: "),this.$rootScope.$broadcast("event:auth-loginFailed",a)},logout:function(a,b,c){this.logger.debug("logout called");var d=this;a.post(this.loginConfig.logoutUrlForRemote,{},this.loginConfig.getHttpConfig).success(function(){d.logger.info("Logout successful"),d.getAuthManager().clear(b,c),c.$broadcast("event:auth-logoutConfirmed","")}).error(function(a){d.logger.error("logout error: "+a)})},getRequestInterceptor:function(){this.logger?this.logger.debug("get request interceptor called"):console.log("get request interceptor called");var a=this;return["$q","$rootScope","$cookieStore","$location",function(b,c,d,e){return{request:function(e){console.log("requestInterceptor - BROWSER COOKIES!!!",document.cookie),a.logger&&a.logger.debug("avaughan.login request interceptor - request!!!!",[a.getAuthManager().getTokenValues(c),e]);var f=e.url.indexOf(a.loginConfig.restCallsWillContain)>=0;return a.logger&&a.logger.debug("avaughan.login request is rest call?",[f,e.url]),f&&a.getAuthManager().isTokenAvailable(c,d)?a.getAuthManager().setAuthOnRequest(c,e):a.logger&&a.logger.debug("avaughan.login token is not available, or not rest call",e.url),e||b.when(e)},responseError:function(c){return a.logger.info("avaughan.login request interceptor - responseError",c),a.loginFailed(c),403===c.status&&a.loginConfig.redirectIfTokenNotFound&&(a.logger.info("got 403 and configured to redirect",a.loginConfig.redirectIfTokenNotFoundUrl),e.search("originalUrl",e.path()),e.path(a.loginConfig.redirectIfTokenNotFoundUrl)),b.reject(c)},response:function(b){return a.logger&&a.logger.debug("avaughan.login request interceptor - response",b),console.log("BROWSER COOKIES!!!",document.cookie),b}}}]},isTokenAvailable:function(a,b){return this.getAuthManager().isTokenAvailable(a,b)},checkRequest:function(a,b,c,d,e){var f=a.path();this.getAuthManager().load(b,d),this.getAuthManager().isTokenAvailable(d,b,c)&&this.getAuthManager().isTokenValid(e,d,b)?(this.logger?this.logger.debug("app.js routing to path",f):console.log("[avLogin] checkRequest - app.js routing to path",f),a.path(f)):this.loginConfig.redirectIfTokenNotFound&&(this.logger?this.logger.debug("not authorized, routing",this.loginConfig.redirectIfTokenNotFoundUrl):console.log("not authorized, routing",this.loginConfig.redirectIfTokenNotFoundUrl),console.log("[avLogin] checkRequest - location.path ",a.path(),this.loginConfig.redirectIfTokenNotFoundUrl),""!==a.path()&&a.path()!==this.loginConfig.redirectIfTokenNotFoundUrl&&a.search("originalUrl",a.path()),a.path(this.loginConfig.redirectIfTokenNotFoundUrl)),this.logger?this.logger.debug("app.js routing to path complete",f):console.log("app.js routing to path complete",f)}};angular.module("avaughan.login",["avaughan.logging","ngResource","ngCookies","http-auth-interceptor"]),angular.module("avaughan.login").provider("avLogin",["$httpProvider",function(a){var b=_.extend(AVaughanLogin,{});this.initialize=function(a){console.log("avaughan.login initialize called",a),b.construct(a)},this.$get=["authService","avLog","$rootScope",function(c,d,e){return console.log("avaughan.login get called",c),b.initialize(c,d,e),b.interceptHttpRequests(a),b}]}]);