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

RSA15a #89

Merged
merged 6 commits into from
Dec 29, 2015
Merged
Show file tree
Hide file tree
Changes from 4 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
20 changes: 20 additions & 0 deletions ably-ios/ARTConnectionDetails.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ARTConnectionDetails.h
// ably
//
// Created by Ricardo Pereira on 26/11/15.
// Copyright © 2015 Ably. All rights reserved.
//

#import <Foundation/Foundation.h>

@class ARTProtocolMessage;

@interface ARTConnectionDetails : NSObject

@property (readonly, getter=getClientId) NSString *clientId;
@property (readonly, getter=getConnectionKey) NSString *connectionKey;

- (instancetype)initWithProtocolMessage:(ARTProtocolMessage *)protocolMessage;

@end
37 changes: 37 additions & 0 deletions ably-ios/ARTConnectionDetails.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ARTConnectionDetails.m
// ably
//
// Created by Ricardo Pereira on 26/11/15.
// Copyright © 2015 Ably. All rights reserved.
//

#import "ARTConnectionDetails.h"

#import "ARTProtocolMessage.h"

@interface ARTConnectionDetails () {
// FIXME: temporary
__weak ARTProtocolMessage* _protocolMessage;
}

@end

@implementation ARTConnectionDetails

- (instancetype)initWithProtocolMessage:(ARTProtocolMessage *)protocolMessage {
if (self == [super init]) {
_protocolMessage = protocolMessage;
}
return self;
}

- (NSString *)getClientId {
return _protocolMessage.clientId;
}

- (NSString *)getConnectionKey {
return _protocolMessage.connectionKey;
}

@end
2 changes: 2 additions & 0 deletions ably-ios/ARTProtocolMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>
#import "CompatibilityMacros.h"

@class ARTConnectionDetails;
@class ARTErrorInfo;

typedef NS_ENUM(NSUInteger, ARTProtocolMessageAction) {
Expand Down Expand Up @@ -57,6 +58,7 @@ ART_ASSUME_NONNULL_BEGIN
@property (art_nullable, readwrite, strong, nonatomic) NSArray *presence;
@property (readonly, assign, nonatomic) BOOL ackRequired;
@property (readwrite, assign, nonatomic) int64_t flags;
@property (readonly, getter=connectionDetails) ARTConnectionDetails *connectionDetails;

- (BOOL)isSyncEnabled;

Expand Down
13 changes: 13 additions & 0 deletions ably-ios/ARTProtocolMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

#import "ARTProtocolMessage.h"
#import "ARTStatus.h"
#import "ARTConnectionDetails.h"

@interface ARTProtocolMessage () {
// FIXME: temporary
ARTConnectionDetails *_connectionDetails;
}

@end

@implementation ARTProtocolMessage

Expand All @@ -29,6 +37,7 @@ - (id)init {
_presence = nil;
_flags = 0;
_error = nil;
_connectionDetails = [[ARTConnectionDetails alloc] initWithProtocolMessage:self];
}
return self;
}
Expand Down Expand Up @@ -63,4 +72,8 @@ - (BOOL)isSyncEnabled {
return self.flags & 0x1;
}

- (ARTConnectionDetails *)getConnectionDetails {
return _connectionDetails;
}

@end
1 change: 1 addition & 0 deletions ably-ios/ARTRealtimeTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ART_ASSUME_NONNULL_BEGIN

@property (readwrite, weak, nonatomic) id<ARTRealtimeTransportDelegate> delegate;
- (void)send:(ARTProtocolMessage *)msg;
- (void)receive:(ARTProtocolMessage *)msg;
- (void)connect;
- (void)sendClose;
- (void)sendPing;
Expand Down
6 changes: 5 additions & 1 deletion ably-ios/ARTWebSocketTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ - (void)send:(ARTProtocolMessage *)msg {
[self.websocket send:data];
}

- (void)receive:(ARTProtocolMessage *)msg {
[self.delegate realtimeTransport:self didReceiveMessage:msg];
}

- (void)connect {
[self.logger debug:@"ARTWebSocketTransport: websocket connect"];
if ([self.options isBasicAuth]) {
Expand Down Expand Up @@ -209,7 +213,7 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
ARTWebSocketTransport *s = weakSelf;
if (s) {
ARTProtocolMessage *pm = [s.encoder decodeProtocolMessage:data];
[s.delegate realtimeTransport:s didReceiveMessage:pm];
[s receive:pm];
}
});
CFRunLoopWakeUp(self.rl);
Expand Down
1 change: 1 addition & 0 deletions ably-ios/ably.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ FOUNDATION_EXPORT const unsigned char ablyVersionString[];

#import <Ably/ARTAuth.h>
#import <Ably/ARTConnection.h>
#import <Ably/ARTConnectionDetails.h>
#import <Ably/ARTHttp.h>
#import <Ably/ARTBaseMessage.h>
#import <Ably/ARTChannelCollection.h>
Expand Down
8 changes: 8 additions & 0 deletions ably.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@
D746AE541BBD85C5003ECEF8 /* ARTChannelCollection.m in Sources */ = {isa = PBXBuildFile; fileRef = D746AE521BBD85C5003ECEF8 /* ARTChannelCollection.m */; };
D7588AF31BFF91B800BB8279 /* ARTURLSessionServerTrust.h in Headers */ = {isa = PBXBuildFile; fileRef = D7588AF11BFF91B800BB8279 /* ARTURLSessionServerTrust.h */; settings = {ASSET_TAGS = (); }; };
D7588AF41BFF91B800BB8279 /* ARTURLSessionServerTrust.m in Sources */ = {isa = PBXBuildFile; fileRef = D7588AF21BFF91B800BB8279 /* ARTURLSessionServerTrust.m */; settings = {ASSET_TAGS = (); }; };
D7B17EE31C07208B00A6958E /* ARTConnectionDetails.h in Headers */ = {isa = PBXBuildFile; fileRef = D7B17EE11C07208B00A6958E /* ARTConnectionDetails.h */; settings = {ATTRIBUTES = (Public, ); }; };
D7B17EE41C07208B00A6958E /* ARTConnectionDetails.m in Sources */ = {isa = PBXBuildFile; fileRef = D7B17EE21C07208B00A6958E /* ARTConnectionDetails.m */; settings = {ASSET_TAGS = (); }; };
D7C1B8771BBEA81A0087B55F /* Auth.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C1B8761BBEA81A0087B55F /* Auth.swift */; };
D7C1B8791BBF5F810087B55F /* ARTAuth+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = D7C1B8781BBF5F460087B55F /* ARTAuth+Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
D7D29B421BE3DEB300374295 /* ARTConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = D7D29B411BE3DEB300374295 /* ARTConnection.m */; settings = {ASSET_TAGS = (); }; };
Expand Down Expand Up @@ -316,6 +318,8 @@
D746AE551BBD8622003ECEF8 /* ARTChannelCollection+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ARTChannelCollection+Private.h"; sourceTree = "<group>"; };
D7588AF11BFF91B800BB8279 /* ARTURLSessionServerTrust.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ARTURLSessionServerTrust.h; sourceTree = "<group>"; };
D7588AF21BFF91B800BB8279 /* ARTURLSessionServerTrust.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARTURLSessionServerTrust.m; sourceTree = "<group>"; };
D7B17EE11C07208B00A6958E /* ARTConnectionDetails.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ARTConnectionDetails.h; sourceTree = "<group>"; };
D7B17EE21C07208B00A6958E /* ARTConnectionDetails.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARTConnectionDetails.m; sourceTree = "<group>"; };
D7C1B8761BBEA81A0087B55F /* Auth.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Auth.swift; sourceTree = "<group>"; };
D7C1B8781BBF5F460087B55F /* ARTAuth+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ARTAuth+Private.h"; sourceTree = "<group>"; };
D7D29B401BE3DD0600374295 /* ARTConnection.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ARTConnection.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -530,6 +534,8 @@
D746AE3B1BBC5AE1003ECEF8 /* ARTRealtimeChannel.m */,
D7D29B401BE3DD0600374295 /* ARTConnection.h */,
D7D29B411BE3DEB300374295 /* ARTConnection.m */,
D7B17EE11C07208B00A6958E /* ARTConnectionDetails.h */,
D7B17EE21C07208B00A6958E /* ARTConnectionDetails.m */,
D7F1D3751BF4DE72001A4B5E /* ARTRealtimePresence.h */,
D7F1D3761BF4DE72001A4B5E /* ARTRealtimePresence.m */,
D746AE321BBC29EB003ECEF8 /* Transport */,
Expand Down Expand Up @@ -695,6 +701,7 @@
D746AE431BBC5CD0003ECEF8 /* ARTRealtimeChannel+Private.h in Headers */,
D746AE251BBB611C003ECEF8 /* ARTChannel+Private.h in Headers */,
D7F1D3731BF4DE07001A4B5E /* ARTRestPresence.h in Headers */,
D7B17EE31C07208B00A6958E /* ARTConnectionDetails.h in Headers */,
1C8065051AE7C8FA00D49357 /* ARTPayload+Private.h in Headers */,
960D07971A46FFC300ED8C8C /* ARTRest+Private.h in Headers */,
1C05CF201AC1D7EB00687AC9 /* ARTRealtime+Private.h in Headers */,
Expand Down Expand Up @@ -959,6 +966,7 @@
960D07941A45F1D800ED8C8C /* ARTCrypto.m in Sources */,
D7D8F8221BC2BE16009718F2 /* ARTAuthOptions.m in Sources */,
1C55427D1B148306003068DB /* ARTStatus.m in Sources */,
D7B17EE41C07208B00A6958E /* ARTConnectionDetails.m in Sources */,
96BF61591A35B52C004CF2B3 /* ARTHttp.m in Sources */,
1C578E201B3435CA00EF46EC /* ARTFallback.m in Sources */,
96A507B61A37881C0077CDF8 /* ARTNSDate+ARTUtil.m in Sources */,
Expand Down
80 changes: 56 additions & 24 deletions ablySpec/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,34 +177,66 @@ class Auth : QuickSpec {
// RSA15
context("token auth and clientId") {
// RSA15a
it("should check clientId consistency") {
let expectedClientId = "client_string"
let options = AblyTests.setupOptions(AblyTests.jsonRestOptions)
options.clientId = expectedClientId

let client = ARTRest(options: options)
client.httpExecutor = mockExecutor

waitUntil(timeout: 10) { done in
// Token
client.calculateAuthorization(ARTAuthMethod.Token) { token, error in
if let e = error {
XCTFail(e.description)
context("should check clientId consistency") {

it("on rest") {
let expectedClientId = "client_string"
let options = AblyTests.setupOptions(AblyTests.jsonRestOptions)
options.clientId = expectedClientId

let client = ARTRest(options: options)
client.httpExecutor = mockExecutor

waitUntil(timeout: 10) { done in
// Token
client.calculateAuthorization(ARTAuthMethod.Token) { token, error in
Copy link
Member

Choose a reason for hiding this comment

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

what is calculateAuthorization?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mattheworiordan It is the REST method that checks the Auth method and completes the request with the Authorisation header: Basic key or Bearer token. When it is Token, it calls the Auth#authorise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The name could be better. Maybe: calculateAuthorization -> buildAuthorisationHeader

Copy link
Member

Choose a reason for hiding this comment

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

prepareAuthorisationHeader?

if let e = error {
XCTFail(e.description)
}
expect(client.auth.clientId).to(equal(expectedClientId))
done()
}
expect(client.auth.clientId).to(equal(expectedClientId))
done()
}

switch extractBodyAsJSON(mockExecutor.requests.first) {
case .Failure(let error):
XCTFail(error)
case .Success(let httpBody):
guard let requestedClientId = httpBody.unbox["clientId"] as? String else { XCTFail("No clientId field in HTTPBody"); return }
expect(requestedClientId).to(equal(expectedClientId))
}
}

switch extractBodyAsJSON(mockExecutor.requests.first) {
case .Failure(let error):
XCTFail(error)
case .Success(let httpBody):
guard let requestedClientId = httpBody.unbox["clientId"] as? String else { XCTFail("No clientId field in HTTPBody"); return }
expect(requestedClientId).to(equal(expectedClientId))

it("on realtime") {
let expectedClientId = "client_string"
let options = AblyTests.setupOptions(AblyTests.jsonRestOptions)
options.clientId = expectedClientId
options.autoConnect = false

let client = ARTRealtime(options: options)
client.setTransportClass(MockTransport.self)
client.connect()

waitUntil(timeout: testTimeout) { done in
client.eventEmitter.on({ state, error in
if state == .Connected && error == nil {
let currentChannel = client.channel("test")
currentChannel.subscribe({ message, errorInfo in
done()
})
currentChannel.publish("ping", cb:nil)
}
})
}

if let transport = client.transport as? MockTransport, let connectionDetails = transport.connectedMessage?.connectionDetails {
// CONNECTED ProtocolMessage
expect(connectionDetails.clientId).to(equal(expectedClientId))
}
else {
XCTFail("MockTransport is not working")
}
}

// TODO: Realtime.connectionDetails of the CONNECTED ProtocolMessage
}

// RSA15b
Expand Down
14 changes: 14 additions & 0 deletions ablySpec/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,25 @@ class MockHTTPExecutor: NSObject, ARTHTTPExecutor {
class MockTransport: ARTWebSocketTransport {

var lastUrl: NSURL?
var lastSentMessage: ARTProtocolMessage?
var connectedMessage: ARTProtocolMessage?

override func setupWebSocket(params: [NSURLQueryItem], withOptions options: ARTClientOptions) -> NSURL {
let url = super.setupWebSocket(params, withOptions: options)
lastUrl = url
return url
}

override func send(msg: ARTProtocolMessage) {
lastSentMessage = msg
super.send(msg)
}

override func receive(msg: ARTProtocolMessage) {
if msg.action == .Connected {
connectedMessage = msg
}
super.receive(msg)
}

}