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

TK2d #508

Merged
merged 1 commit into from
Oct 11, 2016
Merged

TK2d #508

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
4 changes: 3 additions & 1 deletion Source/ARTAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ - (NSURL *)buildURL:(ARTAuthOptions *)options withParams:(ARTTokenParams *)param
}

- (NSMutableURLRequest *)buildRequest:(ARTAuthOptions *)options withParams:(ARTTokenParams *)params {
if (!params.timestamp) params.timestamp = [self currentDate];
NSURL *url = [self buildURL:options withParams:params];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = options.authMethod;
Expand Down Expand Up @@ -189,7 +190,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 = [self currentDate];
currentTokenParams.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 @@ -373,6 +374,7 @@ - (void)authorise:(ARTTokenParams *)tokenParams options:(ARTAuthOptions *)authOp
- (void)createTokenRequest:(ARTTokenParams *)tokenParams options:(ARTAuthOptions *)options callback:(void (^)(ARTTokenRequest *, NSError *))callback {
ARTAuthOptions *replacedOptions = options ? : self.options;
ARTTokenParams *currentTokenParams = tokenParams ? : _tokenParams;
currentTokenParams.timestamp = [self currentDate];

// Validate: Capability JSON text
NSError *errorCapability;
Expand Down
5 changes: 5 additions & 0 deletions Source/ARTClientOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "ARTAuthOptions+Private.h"

#import "ARTDefault.h"
#import "ARTTokenParams.h"

NSString *ARTDefaultEnvironment = nil;

Expand Down Expand Up @@ -125,4 +126,8 @@ + (void)setDefaultEnvironment:(NSString *)environment {
ARTDefaultEnvironment = environment;
}

- (void)setDefaultTokenParams:(ARTTokenParams *)value {
_defaultTokenParams = [[ARTTokenParams alloc] initWithTokenParams:value];
}

@end
3 changes: 2 additions & 1 deletion Source/ARTTokenParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ ART_ASSUME_NONNULL_BEGIN
/**
Timestamp (in millis since the epoch) of this request. Timestamps, in conjunction with the nonce, are used to prevent n requests from being replayed.
*/
@property (nonatomic, strong, null_resettable, setter=setTimestamp:, getter=getTimestamp) NSDate *timestamp;
@property (art_nullable, nonatomic, copy, readwrite) NSDate *timestamp;

@property (nonatomic, readonly, strong) NSString *nonce;

- (instancetype)init;
- (instancetype)initWithClientId:(NSString *__art_nullable)clientId;
- (instancetype)initWithClientId:(NSString *__art_nullable)clientId nonce:(NSString *__art_nullable)nonce;
- (instancetype)initWithOptions:(ARTClientOptions *)options;
- (instancetype)initWithTokenParams:(ARTTokenParams *)tokenParams;

- (__GENERIC(NSMutableArray, NSURLQueryItem *) *)toArray;
- (__GENERIC(NSArray, NSURLQueryItem *) *)toArrayWithUnion:(NSArray *)items;
Expand Down
23 changes: 9 additions & 14 deletions Source/ARTTokenParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
#import "ARTEncoder.h"
#import "ARTTokenRequest.h"

@implementation ARTTokenParams {
NSDate *_timestamp;
}
@implementation ARTTokenParams

- (instancetype)init {
return [self initWithClientId:nil nonce:nil];
Expand Down Expand Up @@ -47,22 +45,19 @@ - (instancetype)initWithOptions:(ARTClientOptions *)options {
return self;
}

- (instancetype)initWithTokenParams:(ARTTokenParams *)tokenParams {
self = [self initWithClientId:tokenParams.clientId];
self.timestamp = nil;
self.ttl = tokenParams.ttl;
self.capability = tokenParams.capability;
return self;
}

- (NSString *)description {
return [NSString stringWithFormat: @"ARTTokenParams: ttl=%f capability=%@ timestamp=%@",
self.ttl, self.capability, self.timestamp];
}

- (void)setTimestamp:(NSDate *)timestamp {
_timestamp = timestamp;
}

- (NSDate *)getTimestamp {
if (_timestamp == nil) {
_timestamp = [NSDate date];
}
return _timestamp;
}

- (NSMutableArray *)toArray {
NSMutableArray *params = [[NSMutableArray alloc] init];

Expand Down
63 changes: 53 additions & 10 deletions Spec/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ class Auth : QuickSpec {
}
}

let transport = client.transport as! TestProxyTransport
guard let transport = client.transport as? TestProxyTransport else {
fail("Transport is nil"); return
}
guard let connectedMessage = transport.protocolMessagesReceived.filter({ $0.action == .Connected }).last else {
XCTFail("No CONNECTED protocol action received"); return
}
Expand Down Expand Up @@ -1106,7 +1108,10 @@ class Auth : QuickSpec {
waitUntil(timeout: testTimeout) { done in
let message = ARTMessage(name: nil, data: "message with an explicit clientId", clientId: "john")
channel.publish([message]) { error in
expect(error!.message).to(contain("mismatched clientId"))
guard let error = error else {
fail("Error is nil"); done(); return
}
expect(error.message).to(contain("mismatched clientId"))
done()
}
}
Expand Down Expand Up @@ -2590,21 +2595,29 @@ class Auth : QuickSpec {
expect(params.timestamp).to(equal(NSDate(timeIntervalSince1970: 123)))
}

it("if not explicitly set, should be generated at the getter and stick") {
it("if explicitly set, the value should stick") {
let params = ARTTokenParams()
params.timestamp = NSDate()

waitUntil(timeout: testTimeout) { done in
let now = NSDate().artToIntegerMs()
guard let timestamp = params.timestamp else {
fail("timestamp is nil"); done(); return
}
let firstParamsTimestamp = timestamp.artToIntegerMs()
expect(firstParamsTimestamp).to(beCloseTo(now, within: 1.5))
delay(0.25) {
let now = NSDate().artToIntegerMs()
let firstParamsTimestamp = params.timestamp.artToIntegerMs()
expect(firstParamsTimestamp).to(beCloseTo(now, within: 1.5))
delay(0.25) {
expect(params.timestamp.artToIntegerMs()).to(equal(firstParamsTimestamp))
done()
}
expect(timestamp.artToIntegerMs()).to(equal(firstParamsTimestamp))
done()
}
}
}

// https://github.com/ably/ably-ios/pull/508#discussion_r82577728
it("object has no timestamp value unless explicitly set") {
let params = ARTTokenParams()
expect(params.timestamp).to(beNil())
}
}
}

Expand Down Expand Up @@ -2706,5 +2719,35 @@ class Auth : QuickSpec {
}
}
}

describe("TokenParams") {
// TK2d
it("timestamp should not be a member of any default token params") {
let rest = ARTRest(options: AblyTests.commonAppSetup())
waitUntil(timeout: testTimeout) { done in
rest.auth.authorise(nil, options: nil) { _, error in
expect(error).to(beNil())
guard let defaultTokenParams = rest.auth.options.defaultTokenParams else {
fail("DefaultTokenParams is nil"); done(); return
}
expect(defaultTokenParams.timestamp).to(beNil())

var defaultTokenParamsCallCount = 0
let hook = rest.auth.options.testSuite_injectIntoMethodAfter(NSSelectorFromString("defaultTokenParams")) {
defaultTokenParamsCallCount += 1
}
defer { hook.remove() }

let newTokenParams = ARTTokenParams(options: rest.auth.options)
expect(defaultTokenParamsCallCount) > 0

newTokenParams.timestamp = NSDate()
expect(newTokenParams.timestamp).toNot(equal(defaultTokenParams.timestamp))
expect(defaultTokenParams.timestamp).to(beNil()) //remain nil
done()
}
}
}
}
}
}