diff --git a/src/identity-utils.ts b/src/identity-utils.ts index 6cb31681..37434109 100644 --- a/src/identity-utils.ts +++ b/src/identity-utils.ts @@ -7,6 +7,7 @@ import { IdentityAPIMethod } from './sdkRuntimeModels'; import { isObject } from './utils'; const { Identify, Modify, Login, Logout } = Constants.IdentityMethods; import { ONE_DAY_IN_SECONDS, MILLIS_IN_ONE_SEC } from './constants'; +import { generateHash } from './utils'; export interface IKnownIdentities extends UserIdentities { device_application_stamp?: string; @@ -61,8 +62,9 @@ export const cacheIdentityRequest = ( ): void => { const cache: Dictionary = idCache.retrieve() || ({} as Dictionary); const cacheKey = concatenateIdentities(method, identities); + const hashedKey = generateHash(cacheKey); - cache[cacheKey] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp}; + cache[hashedKey] = { responseText: xhr.responseText, status: xhr.status, expireTimestamp}; idCache.store(cache); }; @@ -124,16 +126,17 @@ export const hasValidCachedIdentity = ( method, proposedUserIdentities ); + const hashedKey = generateHash(cacheKey); // if cache doesn't have the cacheKey, there is no valid cached identity - if (!cache.hasOwnProperty(cacheKey)) { + if (!cache.hasOwnProperty(hashedKey)) { return false; } // If there is a valid cache key, compare the expireTimestamp to the current time. // If the current time is greater than the expireTimestamp, it is not a valid // cached identity. - const expireTimestamp = cache[cacheKey].expireTimestamp; + const expireTimestamp = cache[hashedKey].expireTimestamp; if (expireTimestamp < new Date().getTime()) { return false; @@ -151,9 +154,10 @@ export const getCachedIdentity = ( method, proposedUserIdentities ); + const hashedKey = generateHash(cacheKey); const cache = idCache.retrieve(); - const cachedIdentity = cache ? cache[cacheKey] : null; + const cachedIdentity = cache ? cache[hashedKey] : null; return cachedIdentity; }; diff --git a/test/src/tests-identity-utils.ts b/test/src/tests-identity-utils.ts index c63b8ed3..b0b1814a 100644 --- a/test/src/tests-identity-utils.ts +++ b/test/src/tests-identity-utils.ts @@ -9,7 +9,7 @@ import { ICachedIdentityCall } from "../../src/identity-utils"; import { LocalStorageVault } from "../../src/vault"; -import { Dictionary } from "../../src/utils"; +import { Dictionary, generateHash } from "../../src/utils"; import { expect } from 'chai'; import { MILLISECONDS_IN_ONE_DAY, @@ -133,7 +133,7 @@ describe('identity-utils', () => { expect(Object.keys(updatedMpIdCache!).length).to.equal(1); const cachedKey = - 'identify:device_application_stamp=test-device-id;customerid=id1;'; + generateHash('identify:device_application_stamp=test-device-id;customerid=id1;'); expect(updatedMpIdCache!.hasOwnProperty(cachedKey)).to.equal(true); @@ -180,7 +180,7 @@ describe('identity-utils', () => { const updatedMpIdCache = cacheVault.retrieve(); expect(Object.keys(updatedMpIdCache!).length).to.equal(1); - const cachedKey = 'login:device_application_stamp=test-device-id;customerid=id1;'; + const cachedKey = generateHash('login:device_application_stamp=test-device-id;customerid=id1;'); expect(updatedMpIdCache!.hasOwnProperty(cachedKey)).to.equal(true); const cachedLoginCall: ICachedIdentityCall = updatedMpIdCache![ @@ -421,9 +421,9 @@ describe('identity-utils', () => { const updatedMpIdCache = cacheVault.retrieve(); const knownIdentities1CachedKey = - 'identify:device_application_stamp=test-device-id;customerid=id1;'; + generateHash('identify:device_application_stamp=test-device-id;customerid=id1;'); const knownIdentities2CachedKey = - 'identify:device_application_stamp=test-device-id;customerid=id2;'; + generateHash('identify:device_application_stamp=test-device-id;customerid=id2;'); // both known identities cache keys should exist on the cacheVault