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

RTC8 #526

Merged
merged 13 commits into from
Dec 1, 2016
8 changes: 8 additions & 0 deletions Ably.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
D71D30041C5F7B2F002115B0 /* RealtimeClientChannels.swift in Sources */ = {isa = PBXBuildFile; fileRef = D71D30031C5F7B2F002115B0 /* RealtimeClientChannels.swift */; };
D72304701BB72CED00F1ABDA /* RealtimeClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = D723046F1BB72CED00F1ABDA /* RealtimeClient.swift */; };
D72768211C9C19040022F8B2 /* RestClientPresence.swift in Sources */ = {isa = PBXBuildFile; fileRef = D72768201C9C19040022F8B2 /* RestClientPresence.swift */; };
D73691FF1DB788C40062C150 /* ARTAuthDetails.h in Headers */ = {isa = PBXBuildFile; fileRef = D73691FD1DB788C40062C150 /* ARTAuthDetails.h */; settings = {ATTRIBUTES = (Public, ); }; };
D73692001DB788C40062C150 /* ARTAuthDetails.m in Sources */ = {isa = PBXBuildFile; fileRef = D73691FE1DB788C40062C150 /* ARTAuthDetails.m */; };
D746AE1D1BBB5207003ECEF8 /* ARTDataQuery.h in Headers */ = {isa = PBXBuildFile; fileRef = D746AE1A1BBB5207003ECEF8 /* ARTDataQuery.h */; settings = {ATTRIBUTES = (Public, ); }; };
D746AE1E1BBB5207003ECEF8 /* ARTDataQuery+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = D746AE1B1BBB5207003ECEF8 /* ARTDataQuery+Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
D746AE1F1BBB5207003ECEF8 /* ARTDataQuery.m in Sources */ = {isa = PBXBuildFile; fileRef = D746AE1C1BBB5207003ECEF8 /* ARTDataQuery.m */; };
Expand Down Expand Up @@ -333,6 +335,8 @@
D71D30031C5F7B2F002115B0 /* RealtimeClientChannels.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RealtimeClientChannels.swift; sourceTree = "<group>"; };
D723046F1BB72CED00F1ABDA /* RealtimeClient.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RealtimeClient.swift; sourceTree = "<group>"; };
D72768201C9C19040022F8B2 /* RestClientPresence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RestClientPresence.swift; sourceTree = "<group>"; };
D73691FD1DB788C40062C150 /* ARTAuthDetails.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ARTAuthDetails.h; sourceTree = "<group>"; };
D73691FE1DB788C40062C150 /* ARTAuthDetails.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARTAuthDetails.m; sourceTree = "<group>"; };
D746AE1A1BBB5207003ECEF8 /* ARTDataQuery.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ARTDataQuery.h; sourceTree = "<group>"; };
D746AE1B1BBB5207003ECEF8 /* ARTDataQuery+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ARTDataQuery+Private.h"; sourceTree = "<group>"; };
D746AE1C1BBB5207003ECEF8 /* ARTDataQuery.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ARTDataQuery.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -673,6 +677,8 @@
D7D8F81F1BC2BE15009718F2 /* ARTAuthOptions.h */,
D7D5A6991CA3D9040071BD6D /* ARTAuthOptions+Private.h */,
D7D8F8201BC2BE15009718F2 /* ARTAuthOptions.m */,
D73691FD1DB788C40062C150 /* ARTAuthDetails.h */,
D73691FE1DB788C40062C150 /* ARTAuthDetails.m */,
D7D8F8271BC2C706009718F2 /* ARTTokenRequest.h */,
D7D8F8281BC2C706009718F2 /* ARTTokenRequest.m */,
D7D8F8291BC2C706009718F2 /* ARTTokenParams.h */,
Expand Down Expand Up @@ -855,6 +861,7 @@
EBFA366E1D58B05000B09AA7 /* ARTRestPresence+Private.h in Headers */,
EB2D85011CD769C800F23CDA /* ARTOSReachability.h in Headers */,
960D07971A46FFC300ED8C8C /* ARTRest+Private.h in Headers */,
D73691FF1DB788C40062C150 /* ARTAuthDetails.h in Headers */,
1C05CF201AC1D7EB00687AC9 /* ARTRealtime+Private.h in Headers */,
D7F1D37A1BF4E33A001A4B5E /* ARTRestChannel+Private.h in Headers */,
85B2C2191B6FE8DE00EA5254 /* CompatibilityMacros.h in Headers */,
Expand Down Expand Up @@ -1181,6 +1188,7 @@
1C55427D1B148306003068DB /* ARTStatus.m in Sources */,
D7B17EE41C07208B00A6958E /* ARTConnectionDetails.m in Sources */,
96BF61591A35B52C004CF2B3 /* ARTHttp.m in Sources */,
D73692001DB788C40062C150 /* ARTAuthDetails.m in Sources */,
1C578E201B3435CA00EF46EC /* ARTFallback.m in Sources */,
96A507B61A37881C0077CDF8 /* ARTNSDate+ARTUtil.m in Sources */,
850BFB4D1B79323C009D0ADD /* ARTPaginatedResult.m in Sources */,
Expand Down
14 changes: 14 additions & 0 deletions Source/ARTAuth+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@
//

#import "ARTAuth.h"
#import "ARTEventEmitter.h"

typedef NS_ENUM(NSUInteger, ARTAuthorizationState) {
ARTAuthorizationSucceeded, //ItemType: nil
ARTAuthorizationFailed //ItemType: NSError
};

ART_ASSUME_NONNULL_BEGIN

/// Messages related to the ARTAuth
@protocol ARTAuthDelegate <NSObject>
@property (nonatomic, readonly) __GENERIC(ARTEventEmitter, NSNumber * /*ARTAuthorizationState*/, id) *authorizationEmitter;
- (void)auth:(ARTAuth *)auth didAuthorize:(ARTTokenDetails *)tokenDetails;
@end

@interface ARTAuth ()

@property (nonatomic, readonly, strong) ARTClientOptions *options;
Expand All @@ -19,6 +31,8 @@ ART_ASSUME_NONNULL_BEGIN
@property (art_nullable, nonatomic, readonly, strong) ARTTokenDetails *tokenDetails;
@property (nonatomic, readonly, assign) NSTimeInterval timeOffset;

@property (art_nullable, weak) id<ARTAuthDelegate> delegate;

@end

@interface ARTAuth (Private)
Expand Down
56 changes: 45 additions & 11 deletions Source/ARTAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "ARTStatus.h"
#import "ARTJsonEncoder.h"
#import "ARTGCD.h"
#import "ARTEventEmitter+Private.h"

@implementation ARTAuth {
__weak ARTRest *_rest;
Expand Down Expand Up @@ -53,7 +54,6 @@ - (instancetype)init:(ARTRest *)rest withOptions:(ARTClientOptions *)options {
object:nil];
#endif
}

return self;
}

Expand Down Expand Up @@ -346,21 +346,55 @@ - (void)authorize:(ARTTokenParams *)tokenParams options:(ARTAuthOptions *)authOp
ARTTokenParams *currentTokenParams = [self mergeParams:tokenParams];
[self storeParams:currentTokenParams];

// Success
void (^successBlock)(ARTTokenDetails *) = ^(ARTTokenDetails *tokenDetails) {
[self.logger verbose:@"RS:%p ARTAuth: token request succeeded: %@", _rest, tokenDetails];
if (callback) {
callback(self.tokenDetails, nil);
}
};

// Failure
void (^failureBlock)(NSError *) = ^(NSError *error) {
[self.logger verbose:@"RS:%p ARTAuth: token request failed: %@", _rest, error];
if (callback) {
callback(nil, error);
}
};

__weak id<ARTAuthDelegate> lastDelegate = self.delegate;
if (lastDelegate) {
// Only the last request should remain
[lastDelegate.authorizationEmitter off];
[lastDelegate.authorizationEmitter once:[NSNumber numberWithInt:ARTAuthorizationSucceeded] callback:^(id null) {
successBlock(_tokenDetails);
[lastDelegate.authorizationEmitter off];
}];
[lastDelegate.authorizationEmitter once:[NSNumber numberWithInt:ARTAuthorizationFailed] callback:^(NSError *error) {
failureBlock(error);
[lastDelegate.authorizationEmitter off];
}];
}

// Request always a new token
[self.logger verbose:@"RS:%p ARTAuth: requesting new token.", _rest];
[self requestToken:currentTokenParams withOptions:replacedOptions callback:^(ARTTokenDetails *tokenDetails, NSError *error) {
if (error) {
[self.logger verbose:@"RS:%p ARTAuth: token request failed: %@", _rest, error];
if (callback) {
callback(nil, error);
}
} else {
_tokenDetails = tokenDetails;
_method = ARTAuthMethodToken;
[self.logger verbose:@"RS:%p ARTAuth: token request succeeded: %@", _rest, tokenDetails];
if (callback) {
callback(self.tokenDetails, nil);
failureBlock(error);
if (lastDelegate) {
[lastDelegate.authorizationEmitter off];
}
return;
}

_tokenDetails = tokenDetails;
_method = ARTAuthMethodToken;

if (lastDelegate) {
[lastDelegate auth:self didAuthorize:tokenDetails];
}
else {
successBlock(tokenDetails);
}
}];
}
Expand Down
23 changes: 23 additions & 0 deletions Source/ARTAuthDetails.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ARTAuthDetails.h
// Ably
//
// Created by Ricardo Pereira on 19/10/2016.
// Copyright © 2016 Ably. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CompatibilityMacros.h"

ART_ASSUME_NONNULL_BEGIN

/// Used with an AUTH protocol messages to send authentication details
@interface ARTAuthDetails : NSObject<NSCopying>

@property (nonatomic, copy) NSString *accessToken;

- (instancetype)initWithToken:(NSString *)token;

@end

ART_ASSUME_NONNULL_END
30 changes: 30 additions & 0 deletions Source/ARTAuthDetails.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// ARTAuthDetails.m
// Ably
//
// Created by Ricardo Pereira on 19/10/2016.
// Copyright © 2016 Ably. All rights reserved.
//

#import "ARTAuthDetails.h"

@implementation ARTAuthDetails

- (instancetype)initWithToken:(NSString *)token {
if (self = [super init]) {
_accessToken = token;
}
return self;
}

- (NSString *)description {
return [NSString stringWithFormat:@"%@ - \n\t accessToken: %@; \n", [super description], self.accessToken];
}

- (id)copyWithZone:(NSZone *)zone {
ARTAuthDetails *authDetails = [[[self class] allocWithZone:zone] init];
authDetails.accessToken = self.accessToken;
return authDetails;
}

@end
17 changes: 17 additions & 0 deletions Source/ARTJsonLikeEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "ARTStatus.h"
#import "ARTTokenDetails.h"
#import "ARTTokenRequest.h"
#import "ARTAuthDetails.h"
#import "ARTConnectionDetails.h"
#import "ARTRest+Private.h"

Expand All @@ -45,6 +46,8 @@ - (ARTProtocolMessage *)protocolMessageFromDictionary:(NSDictionary *)input;

- (NSDictionary *)tokenRequestToDictionary:(ARTTokenRequest *)tokenRequest;

- (NSDictionary *)authDetailsToDictionary:(ARTAuthDetails *)authDetails;

- (NSArray *)statsFromArray:(NSArray *)input;
- (ARTStats *)statsFromDictionary:(NSDictionary *)input;
- (ARTStatsMessageTypes *)statsMessageTypesFromDictionary:(NSDictionary *)input;
Expand Down Expand Up @@ -291,6 +294,15 @@ - (NSDictionary *)messageToDictionary:(ARTMessage *)message {
return output;
}

- (NSDictionary *)authDetailsToDictionary:(ARTAuthDetails *)authDetails {
NSMutableDictionary *output = [NSMutableDictionary dictionary];

[output setObject:authDetails.accessToken forKey:@"accessToken"];

[_logger verbose:@"RS:%p ARTJsonLikeEncoder<%@>: authDetailsToDictionary %@", _rest, [_delegate formatAsString], output];
return output;
}

- (NSArray *)messagesToArray:(NSArray *)messages {
NSMutableArray *output = [NSMutableArray array];

Expand Down Expand Up @@ -358,6 +370,11 @@ - (NSDictionary *)protocolMessageToDictionary:(ARTProtocolMessage *)message {
if (message.presence) {
output[@"presence"] = [self presenceMessagesToArray:message.presence];
}

if (message.auth) {
output[@"auth"] = [self authDetailsToDictionary:message.auth];
}

[_logger verbose:@"RS:%p ARTJsonLikeEncoder<%@>: protocolMessageToDictionary %@", _rest, [_delegate formatAsString], output];
return output;
}
Expand Down
33 changes: 18 additions & 15 deletions Source/ARTProtocolMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "ARTPresenceMessage.h"

@class ARTConnectionDetails;
@class ARTAuthDetails;
@class ARTErrorInfo;
@class ARTMessage;
@class ARTPresenceMessage;
Expand All @@ -34,6 +35,7 @@ typedef NS_ENUM(NSUInteger, ARTProtocolMessageAction) {
ARTProtocolMessagePresence = 14,
ARTProtocolMessageMessage = 15,
ARTProtocolMessageSync = 16,
ARTProtocolMessageAuth = 17,
};

ART_ASSUME_NONNULL_BEGIN
Expand All @@ -45,21 +47,22 @@ ART_ASSUME_NONNULL_BEGIN
*/
@interface ARTProtocolMessage : NSObject

@property (readwrite, assign, nonatomic) ARTProtocolMessageAction action;
@property (readwrite, assign, nonatomic) int count;
@property (art_nullable, readwrite, strong, nonatomic) ARTErrorInfo *error;
@property (art_nullable, readwrite, strong, nonatomic) NSString *id;
@property (art_nullable, readwrite, strong, nonatomic) NSString *channel;
@property (art_nullable, readwrite, strong, nonatomic) NSString *channelSerial;
@property (art_nullable, readwrite, strong, nonatomic) NSString *connectionId;
@property (art_nullable, readwrite, strong, nonatomic, getter=getConnectionKey) NSString *connectionKey;
@property (readwrite, assign, nonatomic) int64_t connectionSerial;
@property (readwrite, assign, nonatomic) int64_t msgSerial;
@property (art_nullable, readwrite, strong, nonatomic) NSDate *timestamp;
@property (art_nullable, readwrite, strong, nonatomic) __GENERIC(NSArray, ARTMessage *) *messages;
@property (art_nullable, readwrite, strong, nonatomic) __GENERIC(NSArray, ARTPresenceMessage *) *presence;
@property (readwrite, assign, nonatomic) int64_t flags;
@property (art_nullable, readwrite, nonatomic) ARTConnectionDetails *connectionDetails;
@property (assign, nonatomic) ARTProtocolMessageAction action;
@property (assign, nonatomic) int count;
@property (art_nullable, strong, nonatomic) ARTErrorInfo *error;
@property (art_nullable, strong, nonatomic) NSString *id;
@property (art_nullable, strong, nonatomic) NSString *channel;
@property (art_nullable, strong, nonatomic) NSString *channelSerial;
@property (art_nullable, strong, nonatomic) NSString *connectionId;
@property (art_nullable, strong, nonatomic, getter=getConnectionKey) NSString *connectionKey;
@property (assign, nonatomic) int64_t connectionSerial;
@property (assign, nonatomic) int64_t msgSerial;
@property (art_nullable, strong, nonatomic) NSDate *timestamp;
@property (art_nullable, strong, nonatomic) __GENERIC(NSArray, ARTMessage *) *messages;
@property (art_nullable, strong, nonatomic) __GENERIC(NSArray, ARTPresenceMessage *) *presence;
@property (assign, nonatomic) int64_t flags;
@property (art_nullable, nonatomic) ARTConnectionDetails *connectionDetails;
@property (art_nullable, nonatomic) ARTAuthDetails *auth;

@end

Expand Down
3 changes: 2 additions & 1 deletion Source/ARTRealtime+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "ARTReachability.h"

#import "ARTRealtimeTransport.h"
#import "ARTAuth+Private.h"

@class ARTRest;
@class ARTErrorInfo;
Expand All @@ -22,7 +23,7 @@

ART_ASSUME_NONNULL_BEGIN

@interface ARTRealtime () <ARTRealtimeTransportDelegate>
@interface ARTRealtime () <ARTRealtimeTransportDelegate, ARTAuthDelegate>

@property (readonly, strong, nonatomic) __GENERIC(ARTEventEmitter, NSNumber *, ARTConnectionStateChange *) *internalEventEmitter;
@property (readonly, strong, nonatomic) __GENERIC(ARTEventEmitter, NSNull *, NSNull *) *connectedEventEmitter;
Expand Down
Loading