Skip to content

Commit

Permalink
RSA10k (#495)
Browse files Browse the repository at this point in the history
* Implemented timeOffset logic.

* RSA10k

Added offset test.

Test improvement, use of fake time offset.

* Fix RSA10k

* RSA10k: discard the time offset from system clock notifications

* Test suite: mock server date

* Travis: fixes scan stable version

* Fix testRecoverFails: connect with an invalid connection Key
  • Loading branch information
EvgenyKarkan authored and ricardopereira committed Oct 11, 2016
1 parent ab283ec commit 5900411
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 29 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ before_install:
install:
# Install Scan
# Automatically switches to the travis formatter when running on Travis
- gem install scan
- gem install scan -v 0.11.3
- gem uninstall xcpretty -aIx
- gem install xcpretty -v 0.2.2
# Remove old versions of CocoaPods
- gem uninstall cocoapods -aIx
# Install CocoaPods
Expand Down
7 changes: 7 additions & 0 deletions Source/ARTAuth+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ART_ASSUME_NONNULL_BEGIN

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

@end

Expand All @@ -34,9 +35,15 @@ ART_ASSUME_NONNULL_BEGIN
// CONNECTED ProtocolMessage may contain a clientId
- (void)setProtocolClientId:(NSString *)clientId;

// Discard the cached local clock offset
- (void)discardTimeOffset;

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

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

@end

ART_ASSUME_NONNULL_END
83 changes: 69 additions & 14 deletions Source/ARTAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#import "ARTAuth+Private.h"

#ifdef TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif

#import "ARTRest.h"
#import "ARTRest+Private.h"
#import "ARTHttp.h"
Expand Down Expand Up @@ -36,11 +40,40 @@ - (instancetype)init:(ARTRest *)rest withOptions:(ARTClientOptions *)options {
_protocolClientId = nil;
_tokenParams = options.defaultTokenParams ? : [[ARTTokenParams alloc] initWithOptions:self.options];
[self validate:options];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveCurrentLocaleDidChangeNotification:)
name:NSCurrentLocaleDidChangeNotification
object:nil];

#ifdef TARGET_OS_IPHONE
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveApplicationSignificantTimeChangeNotification:)
name:UIApplicationSignificantTimeChangeNotification
object:nil];
#endif
}

return self;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSCurrentLocaleDidChangeNotification object:nil];
#ifdef TARGET_OS_IPHONE
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationSignificantTimeChangeNotification object:nil];
#endif
}

- (void)didReceiveCurrentLocaleDidChangeNotification:(NSNotification *)notification {
[self.logger debug:__FILE__ line:__LINE__ message:@"RS:%p NSCurrentLocaleDidChangeNotification received", _rest];
[self discardTimeOffset];
}

- (void)didReceiveApplicationSignificantTimeChangeNotification:(NSNotification *)notification {
[self.logger debug:__FILE__ line:__LINE__ message:@"RS:%p UIApplicationSignificantTimeChangeNotification received", _rest];
[self discardTimeOffset];
}

- (void)validate:(ARTClientOptions *)options {
[self.logger debug:__FILE__ line:__LINE__ message:@"RS:%p validating %@", _rest, options];
if ([options isBasicAuth]) {
Expand Down Expand Up @@ -95,6 +128,8 @@ - (void)storeOptions:(ARTAuthOptions *)customOptions {
self.options.authMethod = customOptions.authMethod;
self.options.authParams = [customOptions.authParams copy];
self.options.useTokenAuth = customOptions.useTokenAuth;
self.options.queryTime = false;
self.options.force = false;
}

- (ARTTokenParams *)mergeParams:(ARTTokenParams *)customParams {
Expand Down Expand Up @@ -154,7 +189,7 @@ - (void)requestToken:(ARTTokenParams *)tokenParams withOptions:(ARTAuthOptions *
// The values replace all corresponding.
ARTAuthOptions *replacedOptions = authOptions ? authOptions : self.options;
ARTTokenParams *currentTokenParams = tokenParams ? tokenParams : _tokenParams;
tokenParams.timestamp = [NSDate date];
tokenParams.timestamp = [self currentDate];

if (replacedOptions.key == nil && replacedOptions.authCallback == nil && replacedOptions.authUrl == nil) {
callback(nil, [ARTErrorInfo createWithCode:ARTStateRequestTokenFailed message:@"no means to renew the token is provided (either an API key, authCallback or authUrl)"]);
Expand Down Expand Up @@ -279,11 +314,11 @@ - (void)authorise:(ARTTokenParams *)tokenParams options:(ARTAuthOptions *)authOp

ARTAuthOptions *replacedOptions;
if ([authOptions isOnlyForceTrue]) {
replacedOptions = self.options;
replacedOptions = [self.options copy];
replacedOptions.force = YES;
}
else {
replacedOptions = authOptions ? : self.options;
replacedOptions = [authOptions copy] ? : [self.options copy];
}
[self storeOptions:replacedOptions];

Expand All @@ -296,7 +331,7 @@ - (void)authorise:(ARTTokenParams *)tokenParams options:(ARTAuthOptions *)authOp
[self.logger verbose:@"RS:%p ARTAuth: reuse current token.", _rest];
requestNewToken = NO;
}
else if ([self.tokenDetails.expires timeIntervalSinceNow] > 0) {
else if ([self.tokenDetails.expires timeIntervalSinceDate:[self currentDate]] > 0) {
[self.logger verbose:@"RS:%p ARTAuth: current token has not expired yet. Reusing token details.", _rest];
requestNewToken = NO;
}
Expand Down Expand Up @@ -355,18 +390,26 @@ - (void)createTokenRequest:(ARTTokenParams *)tokenParams options:(ARTAuthOptions
return;
}

if (replacedOptions.queryTime) {
[_rest time:^(NSDate *time, NSError *error) {
if (error) {
callback(nil, error);
} else {
currentTokenParams.timestamp = [self handleServerTime:time];
callback([currentTokenParams sign:replacedOptions.key], nil);
}
}];
} else {
if (_timeOffset && !replacedOptions.queryTime) {
currentTokenParams.timestamp = [self currentDate];
callback([currentTokenParams sign:replacedOptions.key], nil);
}
else {
if (replacedOptions.queryTime) {
[_rest time:^(NSDate *time, NSError *error) {
if (error) {
callback(nil, error);
} else {
NSDate *serverTime = [self handleServerTime:time];
_timeOffset = [serverTime timeIntervalSinceNow];
currentTokenParams.timestamp = serverTime;
callback([currentTokenParams sign:replacedOptions.key], nil);
}
}];
} else {
callback([currentTokenParams sign:replacedOptions.key], nil);
}
}
}

- (NSDate *)handleServerTime:(NSDate *)time {
Expand All @@ -392,10 +435,22 @@ - (NSString *)getClientId {
}
}

- (NSDate*)currentDate {
return [[NSDate date] dateByAddingTimeInterval:_timeOffset];
}

- (void)discardTimeOffset {
_timeOffset = 0;
}

- (void)setTokenDetails:(ARTTokenDetails *)tokenDetails {
_tokenDetails = tokenDetails;
}

- (void)setTimeOffset:(NSTimeInterval)offset {
_timeOffset = offset;
}

@end

@implementation NSString (ARTTokenDetailsCompatible)
Expand Down
Loading

0 comments on commit 5900411

Please sign in to comment.