diff --git a/UnitTests/MPIdentityCachingTests.m b/UnitTests/MPIdentityCachingTests.m index 36035c09..8ab70dd7 100644 --- a/UnitTests/MPIdentityCachingTests.m +++ b/UnitTests/MPIdentityCachingTests.m @@ -23,7 +23,7 @@ + (void)cacheIdentityResponse:(nonnull MPIdentityCachedResponse *)cachedResponse + (nullable MPIdentityCachedResponse *)getCachedIdentityResponseForEndpoint:(MPEndpoint)endpoint identities:(nonnull NSDictionary *)identities; + (nullable NSDictionary *)getCache; + (void)setCache:(nullable NSDictionary *)cache; -+ (nonnull NSString *)keyWithEndpoint:(MPEndpoint)endpoint identities:(nonnull NSDictionary *)identities; ++ (nullable NSString *)keyWithEndpoint:(MPEndpoint)endpoint identities:(nonnull NSDictionary *)identities; + (nullable NSDictionary *)identitiesFromIdentityRequest:(nonnull MPIdentityHTTPBaseRequest *)identityRequest; + (nullable NSString *)hashIdentities:(NSDictionary *)identities; + (nullable NSString *)serializeIdentities:(NSDictionary *)identities; diff --git a/mParticle-Apple-SDK/Network/MPNetworkCommunication.m b/mParticle-Apple-SDK/Network/MPNetworkCommunication.m index 46448b3c..816cbcc5 100644 --- a/mParticle-Apple-SDK/Network/MPNetworkCommunication.m +++ b/mParticle-Apple-SDK/Network/MPNetworkCommunication.m @@ -809,6 +809,9 @@ - (void)identityApiRequestWithURL:(NSURL*)url identityRequest:(MPIdentityHTTPBas responseDictionary = nil; success = NO; MPILogError(@"Identity response serialization error: %@", [serializationError localizedDescription]); + } else { + responseCode = cachedResponse.statusCode; + success = YES; } } @catch (NSException *exception) { responseDictionary = nil; diff --git a/mParticle-Apple-SDK/Persistence/MPIdentityCaching.m b/mParticle-Apple-SDK/Persistence/MPIdentityCaching.m index d091cb9c..beca0c52 100644 --- a/mParticle-Apple-SDK/Persistence/MPIdentityCaching.m +++ b/mParticle-Apple-SDK/Persistence/MPIdentityCaching.m @@ -158,8 +158,13 @@ + (void)setCache:(nullable NSDictionary *)cache { [[MPIUserDefaults standardUserDefaults] setMPObject:cache forKey:kMPIdentityCachingCachedIdentityCallsKey userId:@0]; } -+ (nonnull NSString *)keyWithEndpoint:(MPEndpoint)endpoint identities:(nonnull NSDictionary *)identities { - return [NSString stringWithFormat:@"%ld::%@", (long)endpoint, [self hashIdentities:identities]]; ++ (nullable NSString *)keyWithEndpoint:(MPEndpoint)endpoint identities:(nonnull NSDictionary *)identities { + NSString *hash = [self hashIdentities:identities]; + if (!hash) { + return nil; + } + NSString *key = [NSString stringWithFormat:@"%ld::%@", (long)endpoint, hash]; + return key; } + (nullable NSDictionary *)identitiesFromIdentityRequest:(nonnull MPIdentityHTTPBaseRequest *)identityRequest { @@ -187,7 +192,8 @@ + (nullable NSDictionary *)identitiesFromIdentityRequest:(nonnull MPIdentityHTTP if (![identityType isKindOfClass:[NSString class]]) { return nil; } - identities[identityType] = change; + // Hash the dictionary to get a reproducible string value + identities[identityType] = [self hashIdentities:change]; } return identities; } @@ -197,6 +203,10 @@ + (nullable NSDictionary *)identitiesFromIdentityRequest:(nonnull MPIdentityHTTP + (nullable NSString *)hashIdentities:(NSDictionary *)identities { NSString *serializedIdentities = [self serializeIdentities:identities]; + if (!serializedIdentities) { + return nil; + } + NSString *hashedString = [self sha256Hash:serializedIdentities]; return hashedString; }