function AuthenticationTokenManager(n){this.configuration=$.extend({},AuthenticationTokenManager.defaultConfiguration,n);this.configuration.checkingTokenValidityFrequency=Math.round(this.configuration.refreshingTokenThreshold*.5)}function AuthenticationSessionManager(n){this.configuration=$.extend({},AuthenticationSessionManager.defaultConfiguration,n)}AuthenticationTokenManager.defaultConfiguration={refreshingTokenThreshold:60,tokenProviderUrl:"",revocationUrl:"",signingOutUrl:"",storage:localStorage,tokenType:{accessToken:"access_token",refreshToken:"refresh_token"},tokenResponseKey:{accessToken:"access_token",refreshToken:"refresh_token",expirationTime:"expires_in"}};AuthenticationTokenManager.prototype.initialize=function(n,t,i,r){!this.isTokenStored()||this.getTokens().expirationDate<r?this.setTokens(n,t,i):this.setAjaxRequestHeader(this.getTokens().accessToken);this.observeExpirationTime()};AuthenticationTokenManager.prototype.setTokens=function(n,t,i){if(n==null||n.length==0){this.clearTokens();return}if(i<=this.configuration.refreshingTokenThreshold)throw"Expiration time is equal or lower than configured refreshing threshold.";this.configuration.storage.authenticationTokens=JSON.stringify({accessToken:n,refreshToken:t,expirationDate:this.calculateExpirationDate(i)});this.setAjaxRequestHeader(n)};AuthenticationTokenManager.prototype.setAjaxRequestHeader=function(n){var t=$.ajaxSetup().headers,i;t==null&&(t={});n==null||n.length==0?delete t.Authorization:t.Authorization="Bearer "+n;$.ajaxSetup({headers:t});i=this;$(document).ajaxError(function(n,t){t.status===401&&i.signOut()})};AuthenticationTokenManager.prototype.signOut=function(){var n=this;this.revokeTokens(function(){window.location.href=n.configuration.signingOutUrl})};AuthenticationTokenManager.prototype.getTokens=function(){if(!this.isTokenStored())return null;var n=JSON.parse(this.configuration.storage.authenticationTokens);return n.expirationDate=new Date(n.expirationDate),n};AuthenticationTokenManager.prototype.calculateExpirationDate=function(n){var t=(new Date).getTime();return new Date(t+AuthenticationTokenManager.convertToMilliseconds(n))};AuthenticationTokenManager.prototype.revokeTokens=function(n){if(!this.isTokenStored()){this.clearTokens();return}var i=this.getTokens(),t=this;$.post(this.configuration.revocationUrl,{token:i.refreshToken,tokenType:this.configuration.tokenType.refreshToken}).always(function(){$.post(t.configuration.revocationUrl,{token:i.accessToken,tokenType:t.configuration.tokenType.accessToken},function(){t.clearTokens()}).always(function(){$.isFunction(n)&&n()})})};AuthenticationTokenManager.prototype.clearTokens=function(){if(this.isTokenStored()){var n=this.getTokens();delete this.configuration.storage.authenticationTokens}this.setAjaxRequestHeader(null)};AuthenticationTokenManager.prototype.isTokenStored=function(){return this.configuration.storage.authenticationTokens!=null};AuthenticationTokenManager.prototype.observeExpirationTime=function(){var n;if(this.isTokenStored()){var t=this.getTokens(),i=new Date(t.expirationDate.getTime()-AuthenticationTokenManager.convertToMilliseconds(this.configuration.refreshingTokenThreshold)),r=i-new Date;if(r<=0){this.refreshToken();return}n=this;setTimeout(function(){n.observeExpirationTime()},AuthenticationTokenManager.convertToMilliseconds(n.configuration.checkingTokenValidityFrequency))}};AuthenticationTokenManager.prototype.refreshToken=function(){var t=this.getTokens(),n=this;$.post(this.configuration.tokenProviderUrl,{refreshToken:t.refreshToken},function(t){var i=null;if(t!=null&&(i=JSON.parse(t)),i==null||i.error!=null){n.clearTokens();return}var r=i[n.configuration.tokenResponseKey.accessToken],u=i[n.configuration.tokenResponseKey.refreshToken],f=i[n.configuration.tokenResponseKey.expirationTime];n.setTokens(r,u,f);n.observeExpirationTime()})};AuthenticationTokenManager.convertToMilliseconds=function(n){return n*1e3};AuthenticationSessionManager.defaultConfiguration={refreshingFrequency:900,keepingAliveActionUrl:"",authorizationLifetime:43200};AuthenticationSessionManager.prototype.keepAlive=function(){var t=this,i=$.now(),n=setInterval(function(){t.isLifetimeExceeded(i)&&clearInterval(n);$.ajax({url:t.configuration.keepingAliveActionUrl,cache:!1}).fail(function(){n!=null&&clearInterval(n)})},AuthenticationSessionManager.convertToMilliseconds(this.configuration.refreshingFrequency))};AuthenticationSessionManager.prototype.isLifetimeExceeded=function(n){var t=AuthenticationSessionManager.convertToMilliseconds(this.configuration.authorizationLifetime);return $.now()>=n+t};AuthenticationSessionManager.convertToMilliseconds=function(n){return n*1e3}