Skip to content

Commit

Permalink
Fixed some issues found in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
einsteinx2 committed Dec 22, 2023
1 parent 66bf944 commit 6141eeb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion UnitTests/MPIdentityCachingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ + (void)cacheIdentityResponse:(nonnull MPIdentityCachedResponse *)cachedResponse
+ (nullable MPIdentityCachedResponse *)getCachedIdentityResponseForEndpoint:(MPEndpoint)endpoint identities:(nonnull NSDictionary *)identities;
+ (nullable NSDictionary<NSString*, NSDictionary*> *)getCache;
+ (void)setCache:(nullable NSDictionary<NSString*, 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;
Expand Down
3 changes: 3 additions & 0 deletions mParticle-Apple-SDK/Network/MPNetworkCommunication.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions mParticle-Apple-SDK/Persistence/MPIdentityCaching.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ + (void)setCache:(nullable NSDictionary<NSString*, 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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 6141eeb

Please sign in to comment.