vue-adal
Version:
Vue plugin for using Azure Active Directory
1 lines • 35.5 kB
JavaScript
"use strict";function createCommonjsModule(e,t){return e(t={exports:{}},t.exports),t.exports}Object.defineProperty(exports,"__esModule",{value:!0});var adal=createCommonjsModule(function(e){var t=(t=function(e){if(this.REQUEST_TYPE={LOGIN:"LOGIN",RENEW_TOKEN:"RENEW_TOKEN",UNKNOWN:"UNKNOWN"},this.RESPONSE_TYPE={ID_TOKEN_TOKEN:"id_token token",TOKEN:"token"},this.CONSTANTS={ACCESS_TOKEN:"access_token",EXPIRES_IN:"expires_in",ID_TOKEN:"id_token",ERROR_DESCRIPTION:"error_description",SESSION_STATE:"session_state",ERROR:"error",STORAGE:{TOKEN_KEYS:"adal.token.keys",ACCESS_TOKEN_KEY:"adal.access.token.key",EXPIRATION_KEY:"adal.expiration.key",STATE_LOGIN:"adal.state.login",STATE_RENEW:"adal.state.renew",NONCE_IDTOKEN:"adal.nonce.idtoken",SESSION_STATE:"adal.session.state",USERNAME:"adal.username",IDTOKEN:"adal.idtoken",ERROR:"adal.error",ERROR_DESCRIPTION:"adal.error.description",LOGIN_REQUEST:"adal.login.request",LOGIN_ERROR:"adal.login.error",RENEW_STATUS:"adal.token.renew.status",ANGULAR_LOGIN_REQUEST:"adal.angular.login.request"},RESOURCE_DELIMETER:"|",CACHE_DELIMETER:"||",LOADFRAME_TIMEOUT:6e3,TOKEN_RENEW_STATUS_CANCELED:"Canceled",TOKEN_RENEW_STATUS_COMPLETED:"Completed",TOKEN_RENEW_STATUS_IN_PROGRESS:"In Progress",LOGGING_LEVEL:{ERROR:0,WARN:1,INFO:2,VERBOSE:3},LEVEL_STRING_MAP:{0:"ERROR:",1:"WARNING:",2:"INFO:",3:"VERBOSE:"},POPUP_WIDTH:483,POPUP_HEIGHT:600},t.prototype._singletonInstance)return t.prototype._singletonInstance;if((t.prototype._singletonInstance=this).instance="https://login.microsoftonline.com/",this.config={},this.callback=null,this.popUp=!1,this.isAngular=!1,this._user=null,this._activeRenewals={},this._loginInProgress=!1,this._acquireTokenInProgress=!1,this._renewStates=[],this._callBackMappedToRenewStates={},this._callBacksMappedToRenewStates={},this._openedWindows=[],this._requestType=this.REQUEST_TYPE.LOGIN,window._adalInstance=this,e.displayCall&&"function"!=typeof e.displayCall)throw new Error("displayCall is not a function");if(!e.clientId)throw new Error("clientId is required");this.config=this._cloneConfig(e),void 0===this.config.navigateToLoginRequestUrl&&(this.config.navigateToLoginRequestUrl=!0),this.config.popUp&&(this.popUp=!0),this.config.callback&&"function"==typeof this.config.callback&&(this.callback=this.config.callback),this.config.instance&&(this.instance=this.config.instance),this.config.loginResource||(this.config.loginResource=this.config.clientId),this.config.redirectUri||(this.config.redirectUri=window.location.href.split("?")[0].split("#")[0]),this.config.postLogoutRedirectUri||(this.config.postLogoutRedirectUri=window.location.href.split("?")[0].split("#")[0]),this.config.anonymousEndpoints||(this.config.anonymousEndpoints=[]),this.config.isAngular&&(this.isAngular=this.config.isAngular),this.config.loadFrameTimeout&&(this.CONSTANTS.LOADFRAME_TIMEOUT=this.config.loadFrameTimeout)},"undefined"!=typeof window&&(window.Logging={level:0,log:function(e){}}),t.prototype.login=function(){if(this._loginInProgress)this.info("Login in progress");else{this._loginInProgress=!0;var e=this._guid();this.config.state=e,this._idTokenNonce=this._guid();var t=this._getItem(this.CONSTANTS.STORAGE.ANGULAR_LOGIN_REQUEST);t&&""!==t?this._saveItem(this.CONSTANTS.STORAGE.ANGULAR_LOGIN_REQUEST,""):t=window.location.href,this.verbose("Expected state: "+e+" startPage:"+t),this._saveItem(this.CONSTANTS.STORAGE.LOGIN_REQUEST,t),this._saveItem(this.CONSTANTS.STORAGE.LOGIN_ERROR,""),this._saveItem(this.CONSTANTS.STORAGE.STATE_LOGIN,e,!0),this._saveItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN,this._idTokenNonce,!0),this._saveItem(this.CONSTANTS.STORAGE.ERROR,""),this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION,"");var i=this._getNavigateUrl("id_token",null)+"&nonce="+encodeURIComponent(this._idTokenNonce);this.config.displayCall?this.config.displayCall(i):this.popUp?(this._saveItem(this.CONSTANTS.STORAGE.STATE_LOGIN,""),this._renewStates.push(e),this.registerCallback(e,this.config.clientId,this.callback),this._loginPopup(i)):this.promptUser(i)}},t.prototype._openPopup=function(e,t,i,n){try{var o=window.screenLeft?window.screenLeft:window.screenX,s=window.screenTop?window.screenTop:window.screenY,r=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,a=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight,h=r/2-i/2+o,c=a/2-n/2+s,l=window.open(e,t,"width="+i+", height="+n+", top="+c+", left="+h);return l.focus&&l.focus(),l}catch(e){return this.warn("Error opening popup, "+e.message),this._loginInProgress=!1,this._acquireTokenInProgress=!1,null}},t.prototype._handlePopupError=function(e,t,i,n,o){this.warn(n),this._saveItem(this.CONSTANTS.STORAGE.ERROR,i),this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION,n),this._saveItem(this.CONSTANTS.STORAGE.LOGIN_ERROR,o),t&&this._activeRenewals[t]&&(this._activeRenewals[t]=null),this._loginInProgress=!1,this._acquireTokenInProgress=!1,e&&e(n,null,i)},t.prototype._loginPopup=function(e,n,t){var o=this._openPopup(e,"login",this.CONSTANTS.POPUP_WIDTH,this.CONSTANTS.POPUP_HEIGHT),s=t||this.callback;if(null!=o){if(this._openedWindows.push(o),-1!=this.config.redirectUri.indexOf("#"))var r=this.config.redirectUri.split("#")[0];else r=this.config.redirectUri;var a=this,h=window.setInterval(function(){if(!o||o.closed||void 0===o.closed){var e="Popup Window closed",t="Popup Window closed by UI action/ Popup Window handle destroyed due to cross zone navigation in IE/Edge";return a.isAngular&&a._broadcast("adal:popUpClosed",t+a.CONSTANTS.RESOURCE_DELIMETER+e),a._handlePopupError(s,n,e,t,t),void window.clearInterval(h)}try{var i=o.location;if(-1!=encodeURI(i.href).indexOf(encodeURI(r)))return a.isAngular?a._broadcast("adal:popUpHashChanged",i.hash):a.handleWindowCallback(i.hash),window.clearInterval(h),a._loginInProgress=!1,a._acquireTokenInProgress=!1,a.info("Closing popup window"),a._openedWindows=[],void o.close()}catch(e){}},1)}else{var i="Popup Window is null. This can happen if you are using IE";this._handlePopupError(s,n,"Error opening popup",i,i)}},t.prototype._broadcast=function(e,t){!function(){function e(e,t){t=t||{bubbles:!1,cancelable:!1,detail:void 0};var i=document.createEvent("CustomEvent");return i.initCustomEvent(e,t.bubbles,t.cancelable,t.detail),i}"function"!=typeof window.CustomEvent&&(e.prototype=window.Event.prototype,window.CustomEvent=e)}();var i=new CustomEvent(e,{detail:t});window.dispatchEvent(i)},t.prototype.loginInProgress=function(){return this._loginInProgress},t.prototype._hasResource=function(e){var t=this._getItem(this.CONSTANTS.STORAGE.TOKEN_KEYS);return t&&!this._isEmpty(t)&&-1<t.indexOf(e+this.CONSTANTS.RESOURCE_DELIMETER)},t.prototype.getCachedToken=function(e){if(!this._hasResource(e))return null;var t=this._getItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY+e),i=this._getItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY+e),n=this.config.expireOffsetSeconds||300;return i&&i>this._now()+n?t:(this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY+e,""),this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY+e,0),null)},t.prototype.getCachedUser=function(){if(this._user)return this._user;var e=this._getItem(this.CONSTANTS.STORAGE.IDTOKEN);return this._user=this._createUser(e),this._user},t.prototype.registerCallback=function(s,r,e){this._activeRenewals[r]=s,this._callBacksMappedToRenewStates[s]||(this._callBacksMappedToRenewStates[s]=[]);var a=this;this._callBacksMappedToRenewStates[s].push(e),this._callBackMappedToRenewStates[s]||(this._callBackMappedToRenewStates[s]=function(e,t,i,n){a._activeRenewals[r]=null;for(var o=0;o<a._callBacksMappedToRenewStates[s].length;++o)try{a._callBacksMappedToRenewStates[s][o](e,t,i,n)}catch(i){a.warn(i)}a._callBacksMappedToRenewStates[s]=null,a._callBackMappedToRenewStates[s]=null})},t.prototype._renewToken=function(e,t,i){this.info("renewToken is called for resource:"+e);var n=this._addAdalFrame("adalRenewFrame"+e),o=this._guid()+"|"+e;this.config.state=o,this._renewStates.push(o),this.verbose("Renew token Expected state: "+o),i=i||"token";var s=this._urlRemoveQueryStringParameter(this._getNavigateUrl(i,e),"prompt");i===this.RESPONSE_TYPE.ID_TOKEN_TOKEN&&(this._idTokenNonce=this._guid(),this._saveItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN,this._idTokenNonce,!0),s+="&nonce="+encodeURIComponent(this._idTokenNonce)),s+="&prompt=none",s=this._addHintParameters(s),this.registerCallback(o,e,t),this.verbose("Navigate to:"+s),n.src="about:blank",this._loadFrameTimeout(s,"adalRenewFrame"+e,e)},t.prototype._renewIdToken=function(e,t){this.info("renewIdToken is called");var i=this._addAdalFrame("adalIdTokenFrame"),n=this._guid()+"|"+this.config.clientId;this._idTokenNonce=this._guid(),this._saveItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN,this._idTokenNonce,!0),this.config.state=n,this._renewStates.push(n),this.verbose("Renew Idtoken Expected state: "+n);var o=null==t?null:this.config.clientId,s=(t=t||"id_token",this._urlRemoveQueryStringParameter(this._getNavigateUrl(t,o),"prompt"));s+="&prompt=none",s=this._addHintParameters(s),s+="&nonce="+encodeURIComponent(this._idTokenNonce),this.registerCallback(n,this.config.clientId,e),this.verbose("Navigate to:"+s),i.src="about:blank",this._loadFrameTimeout(s,"adalIdTokenFrame",this.config.clientId)},t.prototype._urlContainsQueryStringParameter=function(e,t){return new RegExp("[\\?&]"+e+"=").test(t)},t.prototype._urlRemoveQueryStringParameter=function(e,t){var i=new RegExp("(\\&"+t+"=)[^&]+");return e=e.replace(i,""),i=new RegExp("("+t+"=)[^&]+&"),e=e.replace(i,""),i=new RegExp("("+t+"=)[^&]+"),e=e.replace(i,"")},t.prototype._loadFrameTimeout=function(e,t,i){this.verbose("Set loading state to pending for: "+i),this._saveItem(this.CONSTANTS.STORAGE.RENEW_STATUS+i,this.CONSTANTS.TOKEN_RENEW_STATUS_IN_PROGRESS),this._loadFrame(e,t);var n=this;setTimeout(function(){if(n._getItem(n.CONSTANTS.STORAGE.RENEW_STATUS+i)===n.CONSTANTS.TOKEN_RENEW_STATUS_IN_PROGRESS){n.verbose("Loading frame has timed out after: "+n.CONSTANTS.LOADFRAME_TIMEOUT/1e3+" seconds for resource "+i);var e=n._activeRenewals[i];e&&n._callBackMappedToRenewStates[e]&&n._callBackMappedToRenewStates[e]("Token renewal operation failed due to timeout",null,"Token Renewal Failed"),n._saveItem(n.CONSTANTS.STORAGE.RENEW_STATUS+i,n.CONSTANTS.TOKEN_RENEW_STATUS_CANCELED)}},n.CONSTANTS.LOADFRAME_TIMEOUT)},t.prototype._loadFrame=function(t,e){var i=this;i.info("LoadFrame: "+e);var n=e;setTimeout(function(){var e=i._addAdalFrame(n);""!==e.src&&"about:blank"!==e.src||(e.src=t,i._loadFrame(t,n))},500)},t.prototype.acquireToken=function(e,t){if(this._isEmpty(e))return this.warn("resource is required"),void t("resource is required",null,"resource is required");var i=this.getCachedToken(e);return i?(this.info("Token is already in cache for resource:"+e),void t(null,i,null)):this._user||this.config.extraQueryParameter&&-1!==this.config.extraQueryParameter.indexOf("login_hint")?void(this._activeRenewals[e]?this.registerCallback(this._activeRenewals[e],e,t):(this._requestType=this.REQUEST_TYPE.RENEW_TOKEN,e===this.config.clientId?this._user?(this.verbose("renewing idtoken"),this._renewIdToken(t)):(this.verbose("renewing idtoken and access_token"),this._renewIdToken(t,this.RESPONSE_TYPE.ID_TOKEN_TOKEN)):this._user?(this.verbose("renewing access_token"),this._renewToken(e,t)):(this.verbose("renewing idtoken and access_token"),this._renewToken(e,t,this.RESPONSE_TYPE.ID_TOKEN_TOKEN)))):(this.warn("User login is required"),void t("User login is required",null,"login required"))},t.prototype.acquireTokenPopup=function(e,t,i,n){if(this._isEmpty(e))return this.warn("resource is required"),void n("resource is required",null,"resource is required");if(!this._user)return this.warn("User login is required"),void n("User login is required",null,"login required");if(this._acquireTokenInProgress)return this.warn("Acquire token interactive is already in progress"),void n("Acquire token interactive is already in progress",null,"Acquire token interactive is already in progress");var o=this._guid()+"|"+e;this.config.state=o,this._renewStates.push(o),this._requestType=this.REQUEST_TYPE.RENEW_TOKEN,this.verbose("Renew token Expected state: "+o);var s=this._urlRemoveQueryStringParameter(this._getNavigateUrl("token",e),"prompt");if(s+="&prompt=select_account",t&&(s+=encodeURIComponent(t)),i&&-1===s.indexOf("&claims"))s+="&claims="+encodeURIComponent(i);else if(i&&-1!==s.indexOf("&claims"))throw new Error("Claims cannot be passed as an extraQueryParameter");s=this._addHintParameters(s),this._acquireTokenInProgress=!0,this.info("acquireToken interactive is called for the resource "+e),this.registerCallback(o,e,n),this._loginPopup(s,e,n)},t.prototype.acquireTokenRedirect=function(e,t,i){if(this._isEmpty(e))return this.warn("resource is required"),void n("resource is required",null,"resource is required");var n=this.callback;if(!this._user)return this.warn("User login is required"),void n("User login is required",null,"login required");if(this._acquireTokenInProgress)return this.warn("Acquire token interactive is already in progress"),void n("Acquire token interactive is already in progress",null,"Acquire token interactive is already in progress");var o=this._guid()+"|"+e;this.config.state=o,this.verbose("Renew token Expected state: "+o);var s=this._urlRemoveQueryStringParameter(this._getNavigateUrl("token",e),"prompt");if(s+="&prompt=select_account",t&&(s+=encodeURIComponent(t)),i&&-1===s.indexOf("&claims"))s+="&claims="+encodeURIComponent(i);else if(i&&-1!==s.indexOf("&claims"))throw new Error("Claims cannot be passed as an extraQueryParameter");s=this._addHintParameters(s),this._acquireTokenInProgress=!0,this.info("acquireToken interactive is called for the resource "+e),this._saveItem(this.CONSTANTS.STORAGE.LOGIN_REQUEST,window.location.href),this._saveItem(this.CONSTANTS.STORAGE.STATE_RENEW,o,!0),this.promptUser(s)},t.prototype.promptUser=function(e){e?(this.info("Navigate to:"+e),window.location.replace(e)):this.info("Navigate url is empty")},t.prototype.clearCache=function(){this._saveItem(this.CONSTANTS.STORAGE.LOGIN_REQUEST,""),this._saveItem(this.CONSTANTS.STORAGE.ANGULAR_LOGIN_REQUEST,""),this._saveItem(this.CONSTANTS.STORAGE.SESSION_STATE,""),this._saveItem(this.CONSTANTS.STORAGE.STATE_LOGIN,""),this._saveItem(this.CONSTANTS.STORAGE.STATE_RENEW,""),this._renewStates=[],this._saveItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN,""),this._saveItem(this.CONSTANTS.STORAGE.IDTOKEN,""),this._saveItem(this.CONSTANTS.STORAGE.ERROR,""),this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION,""),this._saveItem(this.CONSTANTS.STORAGE.LOGIN_ERROR,""),this._saveItem(this.CONSTANTS.STORAGE.LOGIN_ERROR,"");var e=this._getItem(this.CONSTANTS.STORAGE.TOKEN_KEYS);if(!this._isEmpty(e)){e=e.split(this.CONSTANTS.RESOURCE_DELIMETER);for(var t=0;t<e.length&&""!==e[t];t++)this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY+e[t],""),this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY+e[t],0)}this._saveItem(this.CONSTANTS.STORAGE.TOKEN_KEYS,"")},t.prototype.clearCacheForResource=function(e){this._saveItem(this.CONSTANTS.STORAGE.STATE_RENEW,""),this._saveItem(this.CONSTANTS.STORAGE.ERROR,""),this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION,""),this._hasResource(e)&&(this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY+e,""),this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY+e,0))},t.prototype.logOut=function(){var e;if(this.clearCache(),this._user=null,this.config.logOutUri)e=this.config.logOutUri;else{var t="common",i="";this.config.tenant&&(t=this.config.tenant),this.config.postLogoutRedirectUri&&(i="post_logout_redirect_uri="+encodeURIComponent(this.config.postLogoutRedirectUri)),e=this.instance+t+"/oauth2/logout?"+i}this.info("Logout navigate to: "+e),this.promptUser(e)},t.prototype._isEmpty=function(e){return void 0===e||!e||0===e.length},t.prototype.getUser=function(e){if("function"!=typeof e)throw new Error("callback is not a function");if(this._user)e(null,this._user);else{var t=this._getItem(this.CONSTANTS.STORAGE.IDTOKEN);this._isEmpty(t)?(this.warn("User information is not available"),e("User information is not available",null)):(this.info("User exists in cache: "),this._user=this._createUser(t),e(null,this._user))}},t.prototype._addHintParameters=function(e){if(this._user&&this._user.profile&&this._user.profile.hasOwnProperty("upn")&&(this._urlContainsQueryStringParameter("login_hint",e)||(e+="&login_hint="+encodeURIComponent(this._user.profile.upn)),!this._urlContainsQueryStringParameter("domain_hint",e)&&-1<this._user.profile.upn.indexOf("@"))){var t=this._user.profile.upn.split("@");e+="&domain_hint="+encodeURIComponent(t[t.length-1])}return e},t.prototype._createUser=function(e){var t=null,i=this._extractIdToken(e);return i&&i.hasOwnProperty("aud")&&(i.aud.toLowerCase()===this.config.clientId.toLowerCase()?(t={userName:"",profile:i},i.hasOwnProperty("upn")?t.userName=i.upn:i.hasOwnProperty("email")&&(t.userName=i.email)):this.warn("IdToken has invalid aud field")),t},t.prototype._getHash=function(e){return-1<e.indexOf("#/")?e=e.substring(e.indexOf("#/")+2):-1<e.indexOf("#")&&(e=e.substring(1)),e},t.prototype.isCallback=function(e){e=this._getHash(e);var t=this._deserialize(e);return t.hasOwnProperty(this.CONSTANTS.ERROR_DESCRIPTION)||t.hasOwnProperty(this.CONSTANTS.ACCESS_TOKEN)||t.hasOwnProperty(this.CONSTANTS.ID_TOKEN)},t.prototype.getLoginError=function(){return this._getItem(this.CONSTANTS.STORAGE.LOGIN_ERROR)},t.prototype.getRequestInfo=function(e){e=this._getHash(e);var t=this._deserialize(e),i={valid:!1,parameters:{},stateMatch:!1,stateResponse:"",requestType:this.REQUEST_TYPE.UNKNOWN};if(t&&((i.parameters=t).hasOwnProperty(this.CONSTANTS.ERROR_DESCRIPTION)||t.hasOwnProperty(this.CONSTANTS.ACCESS_TOKEN)||t.hasOwnProperty(this.CONSTANTS.ID_TOKEN))){i.valid=!0;var n="";if(!t.hasOwnProperty("state"))return this.warn("No state returned"),i;if(this.verbose("State: "+t.state),n=t.state,i.stateResponse=n,this._matchState(i))return i;if(!i.stateMatch&&window.parent){i.requestType=this._requestType;for(var o=this._renewStates,s=0;s<o.length;s++)if(o[s]===i.stateResponse){i.stateMatch=!0;break}}}return i},t.prototype._matchNonce=function(e){var t=this._getItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN);if(t){t=t.split(this.CONSTANTS.CACHE_DELIMETER);for(var i=0;i<t.length;i++)if(t[i]===e.profile.nonce)return!0}return!1},t.prototype._matchState=function(e){var t=this._getItem(this.CONSTANTS.STORAGE.STATE_LOGIN);if(t){t=t.split(this.CONSTANTS.CACHE_DELIMETER);for(var i=0;i<t.length;i++)if(t[i]===e.stateResponse)return e.requestType=this.REQUEST_TYPE.LOGIN,e.stateMatch=!0}var n=this._getItem(this.CONSTANTS.STORAGE.STATE_RENEW);if(n)for(n=n.split(this.CONSTANTS.CACHE_DELIMETER),i=0;i<n.length;i++)if(n[i]===e.stateResponse)return e.requestType=this.REQUEST_TYPE.RENEW_TOKEN,e.stateMatch=!0;return!1},t.prototype._getResourceFromState=function(e){if(e){var t=e.indexOf("|");if(-1<t&&t+1<e.length)return e.substring(t+1)}return""},t.prototype.saveTokenFromHash=function(e){this.info("State status:"+e.stateMatch+"; Request type:"+e.requestType),this._saveItem(this.CONSTANTS.STORAGE.ERROR,""),this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION,"");var t,i=this._getResourceFromState(e.stateResponse);e.parameters.hasOwnProperty(this.CONSTANTS.ERROR_DESCRIPTION)?(this.info("Error :"+e.parameters.error+"; Error description:"+e.parameters[this.CONSTANTS.ERROR_DESCRIPTION]),this._saveItem(this.CONSTANTS.STORAGE.ERROR,e.parameters.error),this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION,e.parameters[this.CONSTANTS.ERROR_DESCRIPTION]),e.requestType===this.REQUEST_TYPE.LOGIN&&(this._loginInProgress=!1,this._saveItem(this.CONSTANTS.STORAGE.LOGIN_ERROR,e.parameters.error_description))):e.stateMatch?(this.info("State is right"),e.parameters.hasOwnProperty(this.CONSTANTS.SESSION_STATE)&&this._saveItem(this.CONSTANTS.STORAGE.SESSION_STATE,e.parameters[this.CONSTANTS.SESSION_STATE]),e.parameters.hasOwnProperty(this.CONSTANTS.ACCESS_TOKEN)&&(this.info("Fragment has access token"),this._hasResource(i)||(t=this._getItem(this.CONSTANTS.STORAGE.TOKEN_KEYS)||"",this._saveItem(this.CONSTANTS.STORAGE.TOKEN_KEYS,t+i+this.CONSTANTS.RESOURCE_DELIMETER)),this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY+i,e.parameters[this.CONSTANTS.ACCESS_TOKEN]),this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY+i,this._expiresIn(e.parameters[this.CONSTANTS.EXPIRES_IN]))),e.parameters.hasOwnProperty(this.CONSTANTS.ID_TOKEN)&&(this.info("Fragment has id token"),this._loginInProgress=!1,this._user=this._createUser(e.parameters[this.CONSTANTS.ID_TOKEN]),this._user&&this._user.profile?this._matchNonce(this._user)?(this._saveItem(this.CONSTANTS.STORAGE.IDTOKEN,e.parameters[this.CONSTANTS.ID_TOKEN]),i=this.config.loginResource?this.config.loginResource:this.config.clientId,this._hasResource(i)||(t=this._getItem(this.CONSTANTS.STORAGE.TOKEN_KEYS)||"",this._saveItem(this.CONSTANTS.STORAGE.TOKEN_KEYS,t+i+this.CONSTANTS.RESOURCE_DELIMETER)),this._saveItem(this.CONSTANTS.STORAGE.ACCESS_TOKEN_KEY+i,e.parameters[this.CONSTANTS.ID_TOKEN]),this._saveItem(this.CONSTANTS.STORAGE.EXPIRATION_KEY+i,this._user.profile.exp)):(this._saveItem(this.CONSTANTS.STORAGE.LOGIN_ERROR,"Nonce received: "+this._user.profile.nonce+" is not same as requested: "+this._getItem(this.CONSTANTS.STORAGE.NONCE_IDTOKEN)),this._user=null):(e.parameters.error="invalid id_token",e.parameters.error_description="Invalid id_token. id_token: "+e.parameters[this.CONSTANTS.ID_TOKEN],this._saveItem(this.CONSTANTS.STORAGE.ERROR,"invalid id_token"),this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION,"Invalid id_token. id_token: "+e.parameters[this.CONSTANTS.ID_TOKEN])))):(e.parameters.error="Invalid_state",e.parameters.error_description="Invalid_state. state: "+e.stateResponse,this._saveItem(this.CONSTANTS.STORAGE.ERROR,"Invalid_state"),this._saveItem(this.CONSTANTS.STORAGE.ERROR_DESCRIPTION,"Invalid_state. state: "+e.stateResponse)),this._saveItem(this.CONSTANTS.STORAGE.RENEW_STATUS+i,this.CONSTANTS.TOKEN_RENEW_STATUS_COMPLETED)},t.prototype.getResourceForEndpoint=function(e){if(this.config&&this.config.anonymousEndpoints)for(var t=0;t<this.config.anonymousEndpoints.length;t++)if(-1<e.indexOf(this.config.anonymousEndpoints[t]))return null;if(this.config&&this.config.endpoints)for(var i in this.config.endpoints)if(-1<e.indexOf(i))return this.config.endpoints[i];return-1<e.indexOf("http://")||-1<e.indexOf("https://")?this._getHostFromUri(e)===this._getHostFromUri(this.config.redirectUri)?this.config.loginResource:null:this.config.loginResource},t.prototype._getHostFromUri=function(e){var t=String(e).replace(/^(https?:)\/\//,"");return t=t.split("/")[0]},t.prototype.handleWindowCallback=function(e){if(null==e&&(e=window.location.hash),this.isCallback(e)){var t=null,i=!1;0<this._openedWindows.length&&this._openedWindows[this._openedWindows.length-1].opener&&this._openedWindows[this._openedWindows.length-1].opener._adalInstance?(t=this._openedWindows[this._openedWindows.length-1].opener._adalInstance,i=!0):window.parent&&window.parent._adalInstance&&(t=window.parent._adalInstance);var n,o,s=t.getRequestInfo(e),r=null;o=i||window.parent!==window?t._callBackMappedToRenewStates[s.stateResponse]:t.callback,t.info("Returned from redirect url"),t.saveTokenFromHash(s),s.requestType===this.REQUEST_TYPE.RENEW_TOKEN&&window.parent?(window.parent!==window?t.verbose("Window is in iframe, acquiring token silently"):t.verbose("acquiring token interactive in progress"),n=s.parameters[t.CONSTANTS.ACCESS_TOKEN]||s.parameters[t.CONSTANTS.ID_TOKEN],r=t.CONSTANTS.ACCESS_TOKEN):s.requestType===this.REQUEST_TYPE.LOGIN&&(n=s.parameters[t.CONSTANTS.ID_TOKEN],r=t.CONSTANTS.ID_TOKEN);var a=s.parameters[t.CONSTANTS.ERROR_DESCRIPTION],h=s.parameters[t.CONSTANTS.ERROR];try{o&&o(a,n,h,r)}catch(e){t.error("Error occurred in user defined callback function: "+e)}window.parent!==window||i||(window.location.hash="",t.config.navigateToLoginRequestUrl&&(window.location.href=t._getItem(t.CONSTANTS.STORAGE.LOGIN_REQUEST)))}},t.prototype._getNavigateUrl=function(e,t){var i="common";this.config.tenant&&(i=this.config.tenant);var n=this.instance+i+"/oauth2/authorize"+this._serialize(e,this.config,t)+this._addLibMetadata();return this.info("Navigate url:"+n),n},t.prototype._extractIdToken=function(e){var t=this._decodeJwt(e);if(!t)return null;try{var i=t.JWSPayload,n=this._base64DecodeStringUrlSafe(i);return n?JSON.parse(n):(this.info("The returned id_token could not be base64 url safe decoded."),null)}catch(e){this.error("The returned id_token could not be decoded",e)}return null},t.prototype._base64DecodeStringUrlSafe=function(e){return e=e.replace(/-/g,"+").replace(/_/g,"/"),window.atob?decodeURIComponent(escape(window.atob(e))):decodeURIComponent(escape(this._decode(e)))},t.prototype._decode=function(e){var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",i=(e=String(e).replace(/=+$/,"")).length;if(i%4==1)throw new Error("The token to be decoded is not correctly encoded.");for(var n,o,s,r,a,h,c,l,u="",T=0;T<i;T+=4){if(n=t.indexOf(e.charAt(T)),o=t.indexOf(e.charAt(T+1)),s=t.indexOf(e.charAt(T+2)),r=t.indexOf(e.charAt(T+3)),T+2===i-1){h=(a=n<<18|o<<12|s<<6)>>16&255,c=a>>8&255,u+=String.fromCharCode(h,c);break}if(T+1===i-1){h=(a=n<<18|o<<12)>>16&255,u+=String.fromCharCode(h);break}h=(a=n<<18|o<<12|s<<6|r)>>16&255,c=a>>8&255,l=255&a,u+=String.fromCharCode(h,c,l)}return u},t.prototype._decodeJwt=function(e){if(this._isEmpty(e))return null;var t=/^([^\.\s]*)\.([^\.\s]+)\.([^\.\s]*)$/.exec(e);return!t||t.length<4?(this.warn("The returned id_token is not parseable."),null):{header:t[1],JWSPayload:t[2],JWSSig:t[3]}},t.prototype._convertUrlSafeToRegularBase64EncodedString=function(e){return e.replace("-","+").replace("_","/")},t.prototype._serialize=function(e,t,i){var n=[];if(null!==t){n.push("?response_type="+e),n.push("client_id="+encodeURIComponent(t.clientId)),i&&n.push("resource="+encodeURIComponent(i)),n.push("redirect_uri="+encodeURIComponent(t.redirectUri)),n.push("state="+encodeURIComponent(t.state)),t.hasOwnProperty("slice")&&n.push("slice="+encodeURIComponent(t.slice)),t.hasOwnProperty("extraQueryParameter")&&n.push(t.extraQueryParameter);var o=t.correlationId?t.correlationId:this._guid();n.push("client-request-id="+encodeURIComponent(o))}return n.join("&")},t.prototype._deserialize=function(e){var t,i=/\+/g,n=/([^&=]+)=([^&]*)/g,o=function(e){return decodeURIComponent(e.replace(i," "))},s={};for(t=n.exec(e);t;)s[o(t[1])]=o(t[2]),t=n.exec(e);return s},t.prototype._decimalToHex=function(e){for(var t=e.toString(16);t.length<2;)t="0"+t;return t},t.prototype._guid=function(){var e=window.crypto||window.msCrypto;if(e&&e.getRandomValues){var t=new Uint8Array(16);return e.getRandomValues(t),t[6]|=64,t[6]&=79,t[8]|=128,t[8]&=191,this._decimalToHex(t[0])+this._decimalToHex(t[1])+this._decimalToHex(t[2])+this._decimalToHex(t[3])+"-"+this._decimalToHex(t[4])+this._decimalToHex(t[5])+"-"+this._decimalToHex(t[6])+this._decimalToHex(t[7])+"-"+this._decimalToHex(t[8])+this._decimalToHex(t[9])+"-"+this._decimalToHex(t[10])+this._decimalToHex(t[11])+this._decimalToHex(t[12])+this._decimalToHex(t[13])+this._decimalToHex(t[14])+this._decimalToHex(t[15])}for(var i="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx",n="0123456789abcdef",o=0,s="",r=0;r<36;r++)"-"!==i[r]&&"4"!==i[r]&&(o=16*Math.random()|0),"x"===i[r]?s+=n[o]:"y"===i[r]?(o&=3,s+=n[o|=8]):s+=i[r];return s},t.prototype._expiresIn=function(e){return e||(e=3599),this._now()+parseInt(e,10)},t.prototype._now=function(){return Math.round((new Date).getTime()/1e3)},t.prototype._addAdalFrame=function(e){if(void 0!==e){this.info("Add adal frame to document:"+e);var t=document.getElementById(e);if(!t){if(document.createElement&&document.documentElement&&(window.opera||-1===window.navigator.userAgent.indexOf("MSIE 5.0"))){var i=document.createElement("iframe");i.setAttribute("id",e),i.setAttribute("aria-hidden","true"),i.style.visibility="hidden",i.style.position="absolute",i.style.width=i.style.height=i.borderWidth="0px",t=document.getElementsByTagName("body")[0].appendChild(i)}else document.body&&document.body.insertAdjacentHTML&&document.body.insertAdjacentHTML("beforeEnd",'<iframe name="'+e+'" id="'+e+'" style="display:none"></iframe>');window.frames&&window.frames[e]&&(t=window.frames[e])}return t}},t.prototype._saveItem=function(e,t,i){if(this.config&&this.config.cacheLocation&&"localStorage"===this.config.cacheLocation){if(!this._supportsLocalStorage())return this.info("Local storage is not supported"),!1;if(i){var n=this._getItem(e)||"";localStorage.setItem(e,n+t+this.CONSTANTS.CACHE_DELIMETER)}else localStorage.setItem(e,t);return!0}return this._supportsSessionStorage()?(sessionStorage.setItem(e,t),!0):(this.info("Session storage is not supported"),!1)},t.prototype._getItem=function(e){return this.config&&this.config.cacheLocation&&"localStorage"===this.config.cacheLocation?this._supportsLocalStorage()?localStorage.getItem(e):(this.info("Local storage is not supported"),null):this._supportsSessionStorage()?sessionStorage.getItem(e):(this.info("Session storage is not supported"),null)},t.prototype._supportsLocalStorage=function(){try{return!!window.localStorage&&(window.localStorage.setItem("storageTest","A"),"A"==window.localStorage.getItem("storageTest")&&(window.localStorage.removeItem("storageTest"),!window.localStorage.getItem("storageTest")))}catch(e){return!1}},t.prototype._supportsSessionStorage=function(){try{return!!window.sessionStorage&&(window.sessionStorage.setItem("storageTest","A"),"A"==window.sessionStorage.getItem("storageTest")&&(window.sessionStorage.removeItem("storageTest"),!window.sessionStorage.getItem("storageTest")))}catch(e){return!1}},t.prototype._cloneConfig=function(e){if(null===e||"object"!=typeof e)return e;var t={};for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i]);return t},t.prototype._addLibMetadata=function(){return"&x-client-SKU=Js&x-client-Ver="+this._libVersion()},t.prototype.log=function(e,t,i){if(e<=Logging.level){var n=(new Date).toUTCString(),o="";o=this.config.correlationId?n+":"+this.config.correlationId+"-"+this._libVersion()+"-"+this.CONSTANTS.LEVEL_STRING_MAP[e]+" "+t:n+":"+this._libVersion()+"-"+this.CONSTANTS.LEVEL_STRING_MAP[e]+" "+t,i&&(o+="\nstack:\n"+i.stack),Logging.log(o)}},t.prototype.error=function(e,t){this.log(this.CONSTANTS.LOGGING_LEVEL.ERROR,e,t)},t.prototype.warn=function(e){this.log(this.CONSTANTS.LOGGING_LEVEL.WARN,e,null)},t.prototype.info=function(e){this.log(this.CONSTANTS.LOGGING_LEVEL.INFO,e,null)},t.prototype.verbose=function(e){this.log(this.CONSTANTS.LOGGING_LEVEL.VERBOSE,e,null)},t.prototype._libVersion=function(){return"1.0.16"},e.exports&&(e.exports=t,e.exports.inject=function(e){return new t(e)}),t)}),adal_1=adal.inject,classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function n(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(e,t,i){return t&&n(e.prototype,t),i&&n(e,i),e}}(),AuthenticationContext=function(){function t(o){var s=this;if(classCallCheck(this,t),this.config=o.config||{tenant:"your aad tenant",clientId:"your aad application client id",redirectUri:"base uri for this application",cacheLocation:"localStorage",endpoints:{}},this.requireAuthOnInitialize=o.requireAuthOnInitialize,this.adalContext=o.adalContext||new adal(this.config),this.adalContext.isCallback(window.location.hash)||window!==window.parent)this.adalContext.handleWindowCallback();else{var e=this.adalContext.getCachedUser();e&&window.parent===window&&!window.opener?this.user=e:this.requireAuthOnInitialize&&this.login()}this.requireAuthOnInitialize&&(this.acquireToken(o.config.clientId,function(e,t){e&&console.log("Could not get token")}),this.config.endpoints&&Object.keys(this.config.endpoints).forEach(function(e,t){var i=this.config.endpoints[e];this.acquireToken(i,function(e,t){e&&console.log("Could not get token")})})),o.router&&o.router.beforeEach(function(e,t,i){if(o.globalAuth||e.matched.some(function(e){return e.meta.requireAuth}))if(s.isAuthenticated()){var n=[];e.matched.some(function(e){return!!e.meta.requireRoles&&(n=n.concat(e.meta.requireRoles),!0)})?s.checkRoles(n)?i():(console.log("Not Authorized"),i(t.fullPath)):i()}else s.login();else i()})}return createClass(t,[{key:"login",value:function(){this.adalContext.login()}},{key:"logout",value:function(){this.adalContext.logOut()}},{key:"acquireToken",value:function(e,i){this.adalContext.acquireToken(e,function(e,t){e?i(e):i(null,t)})}},{key:"acquireTokenRedirect",value:function(e,t){this.adalContext.acquireTokenRedirect(e,t)}},{key:"getResourceForEndpoint",value:function(e){return this.adalContext.getResourceForEndpoint(e)}},{key:"isAuthenticated",value:function(){var e=!!this.adalContext.getCachedToken(this.config.clientId);return e&&!this.user&&(this.user=this.adalContext.getCachedUser()),e&&this.user}},{key:"checkRoles",value:function(e){if(!this.isAuthenticated())return!1;if(!this.user.profile.roles)return!1;"string"==typeof e&&(e=[e]);for(var t=0;t<e.length;t++)if(-1<this.user.profile.roles.indexOf(e[t]))return!0;return!1}}]),t}(),getToken=function(i,n,o){exports.AuthenticationContext.acquireToken(i,function(e,t){if(e){switch(e.split(":")[0]){case"AADSTS50058":exports.AuthenticationContext.login();break;case"AADSTS65001":exports.AuthenticationContext.acquireTokenRedirect(i);break;case"AADSTS16000":default:exports.AuthenticationContext.login()}o(new Error("Failed to acquire token"))}else n.defaults.headers.Authorization="BEARER "+t,o(null,t)})},AxiosAuthHttp$$1=function(){function e(){classCallCheck(this,e)}return createClass(e,null,[{key:"createNewClient",value:function(o){if(null==o)throw new Error("Provided options for auth-http are null!");if(null==o.axios)throw new Error("options.axios is required to generate a new http client");if(null==o.resourceId)throw new Error("options.resourceId is required to acquire an auth token");var s=o.axios.create({baseURL:o.baseUrl,headers:{Authorization:null}});return s.interceptors.response.use(function(e){return e},function(n){return 401===n.response.status?(exports.AuthenticationContext.adalContext.clearCacheForResource(o.resourceId),new Promise(function(t,i){return getToken(o.resourceId,s,function(){var e=n.response.config;e.headers.Authorization=s.defaults.headers.Authorization,s({method:e.method,url:e.url,data:e.data,headers:{Accept:e.headers.Accept,Authorization:e.headers.Authorization,"Content-Type":e.headers["Content-Type"]}}).then(function(e){return t(e)},function(e){return i(e)})})})):Promise.reject(n)}),null==o.router||o.router.beforeEach(function(e,t,i){getToken(o.resourceId,s,function(e,t){if(e)return o.onTokenFailure instanceof Function&&o.onTokenFailure(e),void i();o.onTokenSuccess instanceof Function&&o.onTokenSuccess(s,exports.AuthenticationContext,t),i()})}),s}}]),e}();exports.AuthenticationContext=null;var AdalPlugin={install:function(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};exports.AuthenticationContext=new AuthenticationContext(t),e.prototype.$adal=exports.AuthenticationContext,e.mixin({data:function(){return{authenticated:!1}},computed:{isAuthenticated:function(){return this.authenticated=this.$adal.isAuthenticated(),this.authenticated}}})}};exports.default=AdalPlugin,exports.AxiosAuthHttp=AxiosAuthHttp$$1;