diff --git a/ably-ios/ARTAuth+Private.h b/ably-ios/ARTAuth+Private.h index ab2869711..026c8c5d6 100644 --- a/ably-ios/ARTAuth+Private.h +++ b/ably-ios/ARTAuth+Private.h @@ -20,6 +20,7 @@ ART_ASSUME_NONNULL_BEGIN // CONNECTED ProtocolMessage may contain a clientId - (void)setProtocolClientId:(NSString *)clientId; +- (void)setTokenDetails:(ARTAuthTokenDetails *)tokenDetails; @end diff --git a/ably-ios/ARTAuth.m b/ably-ios/ARTAuth.m index 463789ce6..5d2280eb1 100644 --- a/ably-ios/ARTAuth.m +++ b/ably-ios/ARTAuth.m @@ -224,9 +224,9 @@ - (void)authorise:(ARTAuthTokenParams *)tokenParams options:(ARTAuthOptions *)op callback(nil, error); } } else { - _tokenDetails = tokenDetails; + [self setTokenDetails:tokenDetails]; if (callback) { - callback(tokenDetails, nil); + callback(self.tokenDetails, nil); } } }]; @@ -262,6 +262,10 @@ - (void)setProtocolClientId:(NSString *)clientId { _protocolClientId = clientId; } +- (void)setTokenDetails:(ARTAuthTokenDetails *)tokenDetails { + _tokenDetails = tokenDetails; +} + - (NSString *)getClientId { if (_protocolClientId) { // Check wildcard diff --git a/ablySpec/RestClient.swift b/ablySpec/RestClient.swift index cf81ad309..b374c0908 100644 --- a/ablySpec/RestClient.swift +++ b/ablySpec/RestClient.swift @@ -222,6 +222,72 @@ class RestClient: QuickSpec { } } + // RSC9 + it("should use Auth to manage authentication") { + let options = AblyTests.commonAppSetup() + let client = ARTRest(options: options) + let auth = client.auth + + expect(auth.method.rawValue).to(equal(ARTAuthMethod.Basic.rawValue)) + + waitUntil(timeout: 25.0) { done in + auth.requestToken(nil, withOptions: options, callback: { tokenDetailsA, errorA in + if let e = errorA { + XCTFail(e.description) + done() + } + else if let currentTokenDetails = tokenDetailsA { + auth.setTokenDetails(currentTokenDetails) + } + + auth.authorise(nil, options: options, force: false, callback: { tokenDetailsB, errorB in + if let e = errorB { + XCTFail(e.description) + done() + } + // Use the same token because it is valid + expect(auth.tokenDetails?.token).to(equal(tokenDetailsB?.token)) + done() + }) + }) + } + } + + // RSC10 + it("should request another token after current one is no longer valid") { + let options = AblyTests.commonAppSetup() + let client = ARTRest(options: options) + let auth = client.auth + + let tokenParams = ARTAuthTokenParams() + tokenParams.ttl = 3.0 //Seconds + + waitUntil(timeout: 25.0) { done in + auth.requestToken(tokenParams, withOptions: options) { tokenDetailsA, errorA in + if let e = errorA { + XCTFail(e.description) + done() + } + else if let currentTokenDetails = tokenDetailsA { + auth.setTokenDetails(currentTokenDetails) + } + + // Delay for token expiration + delay(tokenParams.ttl) { + auth.authorise(tokenParams, options: options, force: false) { tokenDetailsB, errorB in + if let e = errorB { + XCTFail(e.description) + done() + } + // Different token + expect(tokenDetailsA?.token).toNot(equal(tokenDetailsB?.token)) + done() + } + } + } + } + } + } //RestClient } } \ No newline at end of file diff --git a/ablySpec/TestUtilities.swift b/ablySpec/TestUtilities.swift index 4296144a1..5e43bf74b 100644 --- a/ablySpec/TestUtilities.swift +++ b/ablySpec/TestUtilities.swift @@ -172,6 +172,15 @@ func getTestToken() -> String { return token ?? "" } +public func delay(seconds: NSTimeInterval, closure: ()->()) { + dispatch_after( + dispatch_time( + DISPATCH_TIME_NOW, + Int64(seconds * Double(NSEC_PER_SEC)) + ), + dispatch_get_main_queue(), closure) +} + // TODO: after merge use robrix/Box class Box { let unbox: T