in shared/AppInsightsCore/src/JavaScriptSDK/CookieMgr.ts [112:242]
export function createCookieMgr(rootConfig?: IConfiguration, logger?: IDiagnosticLogger): ICookieMgr {
let cookieMgrConfig = _createCookieMgrConfig(rootConfig || _globalCookieConfig);
let _path = cookieMgrConfig.path || "/";
let _domain = cookieMgrConfig.domain;
// Explicitly checking against false, so that setting to undefined will === true
let _enabled = cookieMgrConfig[strEnabled] !== false;
let cookieMgr: ICookieMgr = {
isEnabled: () => {
let enabled = _enabled && areCookiesSupported(logger);
// Using an indirect lookup for any global cookie manager to support tree shaking for SDK's
// that don't use the "applicationinsights-core" version of the default cookie function
let gblManager = _globalCookieConfig[strConfigCookieMgr];
if (enabled && gblManager && cookieMgr !== gblManager) {
// Make sure the GlobalCookie Manager instance (if not this instance) is also enabled.
// As the global (deprecated) functions may have been called (for backward compatibility)
enabled = _isMgrEnabled(gblManager);
}
return enabled;
},
setEnabled: (value: boolean) => {
// Explicitly checking against false, so that setting to undefined will === true
_enabled = value !== false;
},
set: (name: string, value: string, maxAgeSec?: number, domain?: string, path?: string) => {
let result = false;
if (_isMgrEnabled(cookieMgr)) {
let values: any = {};
let theValue = strTrim(value || strEmpty);
let idx = theValue.indexOf(";");
if (idx !== -1) {
theValue = strTrim(value.substring(0, idx));
values = _extractParts(value.substring(idx + 1));
}
// Only update domain if not already present (isUndefined) and the value is truthy (not null, undefined or empty string)
setValue(values, "domain", domain || _domain, isTruthy, isUndefined);
if (!isNullOrUndefined(maxAgeSec)) {
const _isIE = isIE();
if (isUndefined(values[strExpires])) {
const nowMs = dateNow();
// Only add expires if not already present
let expireMs = nowMs + (maxAgeSec * 1000);
// Sanity check, if zero or -ve then ignore
if (expireMs > 0) {
let expiry = new Date();
expiry.setTime(expireMs);
setValue(values, strExpires,
_formatDate(expiry, !_isIE ? strToUTCString : strToGMTString) || _formatDate(expiry, _isIE ? strToGMTString : strToUTCString) || strEmpty,
isTruthy);
}
}
if (!_isIE) {
// Only replace if not already present
setValue(values, "max-age", strEmpty + maxAgeSec, null, isUndefined);
}
}
let location = getLocation();
if (location && location.protocol === "https:") {
setValue(values, "secure", null, null, isUndefined);
// Only set same site if not also secure
if (_allowUaSameSite === null) {
_allowUaSameSite = !uaDisallowsSameSiteNone((getNavigator() || {} as Navigator).userAgent);
}
if (_allowUaSameSite) {
setValue(values, "SameSite", "None", null, isUndefined);
}
}
setValue(values, "path", path || _path, null, isUndefined);
let setCookieFn = cookieMgrConfig.setCookie || _setCookieValue;
setCookieFn(name, _formatCookieValue(theValue, values));
result = true;
}
return result;
},
get: (name: string): string => {
let value = strEmpty
if (_isMgrEnabled(cookieMgr)) {
value = (cookieMgrConfig.getCookie || _getCookieValue)(name);
}
return value;
},
del: (name: string, path?: string) => {
let result = false;
if (_isMgrEnabled(cookieMgr)) {
// Only remove the cookie if the manager and cookie support has not been disabled
result = cookieMgr.purge(name, path);
}
return result;
},
purge: (name: string, path?: string) => {
let result = false;
if (areCookiesSupported(logger)) {
// Setting the expiration date in the past immediately removes the cookie
let values = {
["path"]: path ? path : "/",
[strExpires]: "Thu, 01 Jan 1970 00:00:01 GMT"
}
if (!isIE()) {
// Set max age to expire now
values["max-age"] = "0"
}
let delCookie = cookieMgrConfig.delCookie || _setCookieValue;
delCookie(name, _formatCookieValue(strEmpty, values));
result = true;
}
return result;
}
};
// Associated this cookie manager with the config
cookieMgr[strConfigCookieMgr] = cookieMgr;
return cookieMgr
}