Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add trackVerifiedToken() #270

Merged
merged 7 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 9PGY6B643R;
DEVELOPMENT_TEAM = 96GHH65B9D;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
Expand Down Expand Up @@ -368,7 +368,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 9PGY6B643R;
DEVELOPMENT_TEAM = 96GHH65B9D;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)",
Expand Down
2 changes: 1 addition & 1 deletion RadarSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'RadarSDK'
s.version = '3.8.4'
s.version = '3.8.5'
s.summary = 'iOS SDK for Radar, the leading geofencing and location tracking platform'
s.homepage = 'https://radar.com'
s.author = { 'Radar Labs, Inc.' => '[email protected]' }
Expand Down
4 changes: 2 additions & 2 deletions RadarSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MARKETING_VERSION = 3.8.4;
MARKETING_VERSION = 3.8.5;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -1024,7 +1024,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MARKETING_VERSION = 3.8.4;
MARKETING_VERSION = 3.8.5;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
OTHER_CFLAGS = "-fembed-bitcode";
Expand Down
22 changes: 21 additions & 1 deletion RadarSDK/Include/Radar.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ typedef NS_ENUM(NSInteger, RadarAddressVerificationStatus) {
};


#pragma mark - Callback typedefs
#pragma mark - Callbacks

/**
Called when a location request succeeds, fails, or times out.
Expand Down Expand Up @@ -185,6 +185,15 @@ typedef void (^_Nullable RadarBeaconCompletionHandler)(RadarStatus status, NSArr
*/
typedef void (^_Nullable RadarTrackCompletionHandler)(RadarStatus status, CLLocation *_Nullable location, NSArray<RadarEvent *> *_Nullable events, RadarUser *_Nullable user);

/**
Called when an track request with token callback succeeds, fails, or times out.

Receives the request status and, if successful, a JSON Web Token (JWT) containing an array of the events generated and the user. Verify the JWT server-side using your secret key.

@see https://radar.com/documentation/sdk/fraud
*/
typedef void (^_Nullable RadarTrackTokenCompletionHandler)(RadarStatus status, NSString *_Nullable token);

/**
Called when a trip update succeeds, fails, or times out.

Expand Down Expand Up @@ -441,6 +450,17 @@ typedef void (^_Nonnull RadarLogConversionCompletionHandler)(RadarStatus status,
*/
+ (void)trackVerifiedWithCompletionHandler:(RadarTrackCompletionHandler _Nullable)completionHandler NS_SWIFT_NAME(trackVerified(completionHandler:));

/**
Tracks the user's location with device integrity information for location verification use cases. Returns a JSON Web Token (JWT). Verify the JWT server-side using your secret key.

@warning Note that you must configure SSL pinning before calling this method.

@param completionHandler An optional completion handler.

@see https://radar.com/documentation/fraud
*/
+ (void)trackVerifiedTokenWithCompletionHandler:(RadarTrackTokenCompletionHandler _Nullable)completionHandler NS_SWIFT_NAME(trackVerifiedToken(completionHandler:));

/**
Starts tracking the user's location in the background with configurable tracking options.

Expand Down
55 changes: 51 additions & 4 deletions RadarSDK/Radar.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ + (void)trackOnceWithDesiredAccuracy:(RadarTrackingOptionsDesiredAccuracy)desire
replayed:NO
beacons:beacons
completionHandler:^(RadarStatus status, NSDictionary *_Nullable res, NSArray<RadarEvent *> *_Nullable events, RadarUser *_Nullable user,
NSArray<RadarGeofence *> *_Nullable nearbyGeofences, RadarConfig *_Nullable config) {
NSArray<RadarGeofence *> *_Nullable nearbyGeofences, RadarConfig *_Nullable config, NSString *_Nullable token) {
if (status == RadarStatusSuccess) {
[[RadarLocationManager sharedInstance] replaceSyncedGeofences:nearbyGeofences];
}
Expand Down Expand Up @@ -219,7 +219,7 @@ + (void)trackOnceWithLocation:(CLLocation *)location completionHandler:(RadarTra
replayed:NO
beacons:nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a use case where a customer would want the payload encrypted through the vanilla trackOnce call and not necessarily need SSL pinning? wondering if we should add an encrypted option here too

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is, I think if they want the encrypted payload they have to do the full trackVerified experience.

completionHandler:^(RadarStatus status, NSDictionary *_Nullable res, NSArray<RadarEvent *> *_Nullable events, RadarUser *_Nullable user,
NSArray<RadarGeofence *> *_Nullable nearbyGeofences, RadarConfig *_Nullable config) {
NSArray<RadarGeofence *> *_Nullable nearbyGeofences, RadarConfig *_Nullable config, NSString *_Nullable token) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we call out anywhere that the rest of the objects in this callback will be nil if token is present?

Copy link
Contributor Author

@nickpatrick nickpatrick Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This callback is only ever used internally, not worried about it


if (completionHandler) {
[RadarUtils runOnMainThread:^{
Expand Down Expand Up @@ -260,9 +260,10 @@ + (void)trackVerifiedWithCompletionHandler:(RadarTrackCompletionHandler)completi
attestationString:attestationString
keyId:keyId
attestationError:attestationError
encrypted:YES
completionHandler:^(RadarStatus status, NSDictionary *_Nullable res, NSArray<RadarEvent *> *_Nullable events,
RadarUser *_Nullable user, NSArray<RadarGeofence *> *_Nullable nearbyGeofences,
RadarConfig *_Nullable config) {
RadarConfig *_Nullable config, NSString *_Nullable token) {
if (completionHandler) {
[RadarUtils runOnMainThread:^{
completionHandler(status, location, events, user);
Expand All @@ -274,6 +275,52 @@ + (void)trackVerifiedWithCompletionHandler:(RadarTrackCompletionHandler)completi
}];
}

+ (void)trackVerifiedTokenWithCompletionHandler:(RadarTrackTokenCompletionHandler)completionHandler {
[[RadarAPIClient sharedInstance]
getConfigForUsage:@"verify" verified:YES
completionHandler:^(RadarStatus status, RadarConfig *_Nullable config) {
[[RadarLocationManager sharedInstance]
getLocationWithDesiredAccuracy:RadarTrackingOptionsDesiredAccuracyHigh
completionHandler:^(RadarStatus status, CLLocation *_Nullable location, BOOL stopped) {
if (status != RadarStatusSuccess) {
if (completionHandler) {
[RadarUtils runOnMainThread:^{
completionHandler(status, nil);
}];
}

return;
}

[[RadarVerificationManager sharedInstance]
getAttestationWithNonce:config.nonce
completionHandler:^(NSString *_Nullable attestationString, NSString *_Nullable keyId, NSString *_Nullable attestationError) {
[[RadarAPIClient sharedInstance]
trackWithLocation:location
stopped:RadarState.stopped
foreground:[RadarUtils foreground]
source:RadarLocationSourceForegroundLocation
replayed:NO
beacons:nil
verified:YES
attestationString:attestationString
keyId:keyId
attestationError:attestationError
encrypted:YES
completionHandler:^(RadarStatus status, NSDictionary *_Nullable res, NSArray<RadarEvent *> *_Nullable events,
RadarUser *_Nullable user, NSArray<RadarGeofence *> *_Nullable nearbyGeofences,
RadarConfig *_Nullable config, NSString *_Nullable token) {
if (completionHandler) {
[RadarUtils runOnMainThread:^{
completionHandler(status, token);
}];
}
}];
}];
}];
}];
}

+ (void)startTrackingWithOptions:(RadarTrackingOptions *)options {
[[RadarLogger sharedInstance] logWithLevel:RadarLogLevelInfo type:RadarLogTypeSDKCall message:@"startTracking()"];
[[RadarLocationManager sharedInstance] startTrackingWithOptions:options];
Expand Down Expand Up @@ -345,7 +392,7 @@ + (void)mockTrackingWithOrigin:(CLLocation *)origin
replayed:NO
beacons:nil
completionHandler:^(RadarStatus status, NSDictionary *_Nullable res, NSArray<RadarEvent *> *_Nullable events, RadarUser *_Nullable user,
NSArray<RadarGeofence *> *_Nullable nearbyGeofences, RadarConfig *_Nullable config) {
NSArray<RadarGeofence *> *_Nullable nearbyGeofences, RadarConfig *_Nullable config, NSString *_Nullable token) {
if (completionHandler) {
[RadarUtils runOnMainThread:^{
completionHandler(status, location, events, user);
Expand Down
4 changes: 3 additions & 1 deletion RadarSDK/RadarAPIClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ typedef void (^_Nonnull RadarTrackAPICompletionHandler)(RadarStatus status,
NSArray<RadarEvent *> *_Nullable events,
RadarUser *_Nullable user,
NSArray<RadarGeofence *> *_Nullable nearbyGeofences,
RadarConfig *_Nullable config);
RadarConfig *_Nullable config,
NSString *_Nullable token);

typedef void (^_Nonnull RadarTripAPICompletionHandler)(RadarStatus status, RadarTrip *_Nullable trip, NSArray<RadarEvent *> *_Nullable events);

Expand Down Expand Up @@ -86,6 +87,7 @@ typedef void (^_Nonnull RadarSyncLogsAPICompletionHandler)(RadarStatus status);
attestationString:(NSString *_Nullable)attestationString
keyId:(NSString *_Nullable)keyId
attestationError:(NSString *_Nullable)attestationError
encrypted:(BOOL)encrypted
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was going to suggest we think about changing this to verified given discussion in slack & recent changes, but of course that's already used.. 🤔

Copy link
Contributor

@lmeier lmeier Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the JWT docs, I think encoded instead of encrypted would perhaps be most accurate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking we can change param or endpoint name or switch to a project setting later on

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token param or POST /track/token endpoint are both options, too

completionHandler:(RadarTrackAPICompletionHandler _Nonnull)completionHandler;

- (void)verifyEventId:(NSString *_Nonnull)eventId verification:(RadarEventVerification)verification verifiedPlaceId:(NSString *_Nullable)verifiedPlaceId;
Expand Down
22 changes: 18 additions & 4 deletions RadarSDK/RadarAPIClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
attestationString:nil
keyId:nil
attestationError:nil
encrypted:NO
completionHandler:completionHandler];
}

Expand All @@ -143,10 +144,11 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
attestationString:(NSString *_Nullable)attestationString
keyId:(NSString *_Nullable)keyId
attestationError:(NSString *_Nullable)attestationError
encrypted:(BOOL)encrypted
completionHandler:(RadarTrackAPICompletionHandler _Nonnull)completionHandler {
NSString *publishableKey = [RadarSettings publishableKey];
if (!publishableKey) {
return completionHandler(RadarStatusErrorPublishableKey, nil, nil, nil, nil, nil);
return completionHandler(RadarStatusErrorPublishableKey, nil, nil, nil, nil, nil, nil);
}
NSMutableDictionary *params = [NSMutableDictionary new];
BOOL anonymous = [RadarSettings anonymousTrackingEnabled];
Expand Down Expand Up @@ -257,6 +259,7 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
params[@"attestationString"] = attestationString;
params[@"keyId"] = keyId;
params[@"attestationError"] = attestationError;
params[@"encrypted"] = @(encrypted);
}
params[@"appId"] = [[NSBundle mainBundle] bundleIdentifier];

Expand Down Expand Up @@ -314,7 +317,7 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location

[[RadarDelegateHolder sharedInstance] didFailWithStatus:status];

return completionHandler(status, nil, nil, nil, nil, nil);
return completionHandler(status, nil, nil, nil, nil, nil, nil);
}

[[RadarReplayBuffer sharedInstance] clearBuffer];
Expand All @@ -327,10 +330,21 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
id eventsObj = res[@"events"];
id userObj = res[@"user"];
id nearbyGeofencesObj = res[@"nearbyGeofences"];
id tokenObj = res[@"token"];
NSArray<RadarEvent *> *events = [RadarEvent eventsFromObject:eventsObj];
RadarUser *user = [[RadarUser alloc] initWithObject:userObj];
NSArray<RadarGeofence *> *nearbyGeofences = [RadarGeofence geofencesFromObject:nearbyGeofencesObj];

if (encrypted) {
if (!tokenObj) {
return completionHandler(status, nil, nil, nil, nil, nil, nil);
}

NSString *token = (NSString *)tokenObj;

return completionHandler(status, nil, nil, nil, nil, nil, token);
}
Comment on lines +338 to +346
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This codepath is completely forgoing updating the event & location update handlers, trips, & place updating.

As such, trackVerified and trackVerifiedEncrypted behave significantly differently -- wonder how we should make it clear to users the difference in behavior and effects (could see myself getting confused by this).

Copy link
Contributor Author

@nickpatrick nickpatrick Aug 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will mention on /documentation. Most people using this aren't going to be using startTracking(), so shouldn't cause unexpected behavior in practice


if (user) {
BOOL inGeofences = user.geofences && user.geofences.count;
BOOL atPlace = user.place != nil;
Expand Down Expand Up @@ -394,12 +408,12 @@ - (void)trackWithLocation:(CLLocation *_Nonnull)location
[[RadarDelegateHolder sharedInstance] didReceiveEvents:events user:user];
}

return completionHandler(RadarStatusSuccess, res, events, user, nearbyGeofences, config);
return completionHandler(RadarStatusSuccess, res, events, user, nearbyGeofences, config, nil);
}

[[RadarDelegateHolder sharedInstance] didFailWithStatus:status];

completionHandler(RadarStatusErrorServer, nil, nil, nil, nil, nil);
completionHandler(RadarStatusErrorServer, nil, nil, nil, nil, nil, nil);
}];
}

Expand Down
2 changes: 1 addition & 1 deletion RadarSDK/RadarLocationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ - (void)sendLocation:(CLLocation *)location stopped:(BOOL)stopped source:(RadarL
replayed:replayed
beacons:beacons
completionHandler:^(RadarStatus status, NSDictionary *_Nullable res, NSArray<RadarEvent *> *_Nullable events, RadarUser *_Nullable user,
NSArray<RadarGeofence *> *_Nullable nearbyGeofences, RadarConfig *_Nullable config) {
NSArray<RadarGeofence *> *_Nullable nearbyGeofences, RadarConfig *_Nullable config, NSString *_Nullable token) {
self.sending = NO;

[self updateTrackingFromMeta:config.meta];
Expand Down
2 changes: 1 addition & 1 deletion RadarSDK/RadarUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ + (NSNumber *)timeZoneOffset {
}

+ (NSString *)sdkVersion {
return @"3.8.4";
return @"3.8.5";
}

+ (NSString *)deviceId {
Expand Down
Loading