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

RSA4b1 #836

Merged
merged 5 commits into from
Apr 24, 2019
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
5 changes: 3 additions & 2 deletions Source/ARTAuth+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, weak) ARTLog *logger;
@property (nullable, nonatomic, readonly, strong) ARTTokenDetails *tokenDetails;
@property (nonatomic, readonly, assign) NSTimeInterval timeOffset;
@property (nullable, nonatomic, readonly, strong) NSNumber *timeOffset;

@property (nullable, weak) id<ARTAuthDelegate> delegate;
@property (readonly, assign) BOOL authorizing;
Expand Down Expand Up @@ -73,10 +73,11 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)tokenRemainsValid;

// Private TokenDetails setter for testing only
- (void)setTokenDetails:(ARTTokenDetails *)tokenDetails;
- (void)setTokenDetails:(nullable ARTTokenDetails *)tokenDetails;

// Private TimeOffset setter for testing only
- (void)setTimeOffset:(NSTimeInterval)offset;
- (void)clearTimeOffset;

- (NSString *_Nullable)clientId;

Expand Down
28 changes: 21 additions & 7 deletions Source/ARTAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ - (BOOL)tokenRemainsValid {
if (self.tokenDetails.expires == nil) {
return YES;
}
else if ([self.tokenDetails.expires timeIntervalSinceDate:[self currentDate]] > 0) {
else if (![self hasTimeOffset] || [self.tokenDetails.expires timeIntervalSinceDate:[self currentDate]] > 0) {
return YES;
}
}
Expand Down Expand Up @@ -588,7 +588,7 @@ - (void)_createTokenRequest:(ARTTokenParams *)tokenParams options:(ARTAuthOption
return;
}

if (_timeOffset && !replacedOptions.queryTime) {
if ([self hasTimeOffsetWithValue] && !replacedOptions.queryTime) {
currentTokenParams.timestamp = [self currentDate];
callback([currentTokenParams sign:replacedOptions.key], nil);
}
Expand All @@ -599,7 +599,7 @@ - (void)_createTokenRequest:(ARTTokenParams *)tokenParams options:(ARTAuthOption
callback(nil, error);
} else {
NSDate *serverTime = [self handleServerTime:time];
self->_timeOffset = [serverTime timeIntervalSinceNow];
self->_timeOffset = @([serverTime timeIntervalSinceNow]);
currentTokenParams.timestamp = serverTime;
callback([currentTokenParams sign:replacedOptions.key], nil);
}
Expand Down Expand Up @@ -646,12 +646,20 @@ - (NSString *)clientId_nosync {
}
}

- (NSDate*)currentDate {
- (NSDate *)currentDate {
ART_TRY_OR_REPORT_CRASH_START(_rest) {
return [[NSDate date] dateByAddingTimeInterval:_timeOffset];
return [[NSDate date] dateByAddingTimeInterval:_timeOffset.doubleValue];
} ART_TRY_OR_REPORT_CRASH_END
}

- (BOOL)hasTimeOffset {
return _timeOffset != nil;
}

- (BOOL)hasTimeOffsetWithValue {
return _timeOffset != nil && _timeOffset.doubleValue > 0;
}

- (void)discardTimeOffset {
// This may run after dealloc has been called in _rest. I've seen this
// happen when rest.auth is put in a variable, even if (apparently) that
Expand All @@ -667,7 +675,7 @@ - (void)discardTimeOffset {
// Called from NSNotificationCenter, so must put change in the queue.
dispatch_sync(_queue, ^{
ART_TRY_OR_REPORT_CRASH_START(self->_rest) {
self->_timeOffset = 0;
[self clearTimeOffset];
} ART_TRY_OR_REPORT_CRASH_END
});
}
Expand All @@ -680,7 +688,13 @@ - (void)setTokenDetails:(ARTTokenDetails *)tokenDetails {

- (void)setTimeOffset:(NSTimeInterval)offset {
ART_TRY_OR_REPORT_CRASH_START(_rest) {
_timeOffset = offset;
_timeOffset = @(offset);
} ART_TRY_OR_REPORT_CRASH_END
}

- (void)clearTimeOffset; {
ART_TRY_OR_REPORT_CRASH_START(_rest) {
_timeOffset = nil;
} ART_TRY_OR_REPORT_CRASH_END
}

Expand Down
1 change: 1 addition & 0 deletions Source/ARTAuthOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ - (id)copyWithZone:(NSZone *)zone {

options.key = self.key;
options.token = self.token;
options.tokenDetails = self.tokenDetails;
options.authCallback = self.authCallback;
options.authUrl = self.authUrl;
options.authMethod = self.authMethod;
Expand Down
1 change: 1 addition & 0 deletions Source/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ - (void)transportConnectForcingNewToken:(BOOL)forceNewToken keepConnection:(BOOL
}
else {
// New Token
[self.auth setTokenDetails:nil];
// Transport instance couldn't exist anymore when `authorize` completes or reaches time out.
__weak __typeof(self) weakSelf = self;

Expand Down
1 change: 1 addition & 0 deletions Source/ARTRest.m
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ - (void)executeRequestWithAuthentication:(NSMutableURLRequest *)request withMeth
}
else {
// New Token
[self.auth setTokenDetails:nil];
[self.auth _authorize:nil options:self.options callback:^(ARTTokenDetails *tokenDetails, NSError *error) {
if (error) {
[self.logger debug:__FILE__ line:__LINE__ message:@"RS:%p ARTRest reissuing token failed %@", self, error];
Expand Down
Loading