UNPKG

msal

Version:
170 lines 7 kB
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. import { Constants, CacheKeys } from "./utils/Constants"; import { AccessTokenCacheItem } from "./AccessTokenCacheItem"; import { ClientConfigurationError } from "./error/ClientConfigurationError"; /** * @hidden */ var Storage = /** @class */ (function () { function Storage(cacheLocation) { if (Storage.instance) { return Storage.instance; } this.cacheLocation = cacheLocation; this.localStorageSupported = typeof window[this.cacheLocation] !== "undefined" && window[this.cacheLocation] != null; this.sessionStorageSupported = typeof window[cacheLocation] !== "undefined" && window[cacheLocation] != null; Storage.instance = this; if (!this.localStorageSupported && !this.sessionStorageSupported) { throw ClientConfigurationError.createNoStorageSupportedError(); } return Storage.instance; } // add value to storage Storage.prototype.setItem = function (key, value, enableCookieStorage) { if (window[this.cacheLocation]) { window[this.cacheLocation].setItem(key, value); } if (enableCookieStorage) { this.setItemCookie(key, value); } }; // get one item by key from storage Storage.prototype.getItem = function (key, enableCookieStorage) { if (enableCookieStorage && this.getItemCookie(key)) { return this.getItemCookie(key); } if (window[this.cacheLocation]) { return window[this.cacheLocation].getItem(key); } return null; }; // remove value from storage Storage.prototype.removeItem = function (key) { if (window[this.cacheLocation]) { return window[this.cacheLocation].removeItem(key); } }; // clear storage (remove all items from it) Storage.prototype.clear = function () { if (window[this.cacheLocation]) { return window[this.cacheLocation].clear(); } }; Storage.prototype.getAllAccessTokens = function (clientId, homeAccountIdentifier) { var results = []; var accessTokenCacheItem; var storage = window[this.cacheLocation]; if (storage) { var key = void 0; for (key in storage) { if (storage.hasOwnProperty(key)) { if (key.match(clientId) && key.match(homeAccountIdentifier)) { var value = this.getItem(key); if (value) { accessTokenCacheItem = new AccessTokenCacheItem(JSON.parse(key), JSON.parse(value)); results.push(accessTokenCacheItem); } } } } } return results; }; Storage.prototype.removeAcquireTokenEntries = function (state) { var storage = window[this.cacheLocation]; if (storage) { var key = void 0; for (key in storage) { if (storage.hasOwnProperty(key)) { if ((key.indexOf(CacheKeys.AUTHORITY) !== -1 || key.indexOf(CacheKeys.ACQUIRE_TOKEN_ACCOUNT) !== 1) && (!state || key.indexOf(state) !== -1)) { var splitKey = key.split(Constants.resourceDelimiter); var state_1 = void 0; if (splitKey.length > 1) { state_1 = splitKey[1]; } if (state_1 && !this.tokenRenewalInProgress(state_1)) { this.removeItem(key); this.removeItem(Constants.renewStatus + state_1); this.removeItem(Constants.stateLogin); this.removeItem(Constants.stateAcquireToken); this.setItemCookie(key, "", -1); } } } } } this.clearCookie(); }; Storage.prototype.tokenRenewalInProgress = function (stateValue) { var storage = window[this.cacheLocation]; var renewStatus = storage[Constants.renewStatus + stateValue]; return !(!renewStatus || renewStatus !== Constants.tokenRenewStatusInProgress); }; Storage.prototype.resetCacheItems = function () { var storage = window[this.cacheLocation]; if (storage) { var key = void 0; for (key in storage) { if (storage.hasOwnProperty(key)) { if (key.indexOf(Constants.msal) !== -1) { this.removeItem(key); } } } this.removeAcquireTokenEntries(); } }; Storage.prototype.setItemCookie = function (cName, cValue, expires) { var cookieStr = cName + "=" + cValue + ";"; if (expires) { var expireTime = this.getCookieExpirationTime(expires); cookieStr += "expires=" + expireTime + ";"; } document.cookie = cookieStr; }; Storage.prototype.getItemCookie = function (cName) { var name = cName + "="; var ca = document.cookie.split(";"); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) === " ") { c = c.substring(1); } if (c.indexOf(name) === 0) { return c.substring(name.length, c.length); } } return ""; }; Storage.prototype.getCookieExpirationTime = function (cookieLifeDays) { var today = new Date(); var expr = new Date(today.getTime() + cookieLifeDays * 24 * 60 * 60 * 1000); return expr.toUTCString(); }; Storage.prototype.clearCookie = function () { this.setItemCookie(Constants.nonceIdToken, "", -1); this.setItemCookie(Constants.stateLogin, "", -1); this.setItemCookie(Constants.loginRequest, "", -1); this.setItemCookie(Constants.stateAcquireToken, "", -1); }; /** * Create acquireTokenAccountKey to cache account object * @param accountId * @param state */ Storage.generateAcquireTokenAccountKey = function (accountId, state) { return CacheKeys.ACQUIRE_TOKEN_ACCOUNT + Constants.resourceDelimiter + ("" + accountId) + Constants.resourceDelimiter + ("" + state); }; /** * Create authorityKey to cache authority * @param state */ Storage.generateAuthorityKey = function (state) { return CacheKeys.AUTHORITY + Constants.resourceDelimiter + ("" + state); }; return Storage; }()); export { Storage }; //# sourceMappingURL=Storage.js.map