Skip to content

Commit

Permalink
Update RSA10j for 0.9 (#521)
Browse files Browse the repository at this point in the history
* Update RSA10j

* Fix: ttl when omitted should set the default value
  • Loading branch information
ricardopereira committed Nov 30, 2016
1 parent a8ec7cd commit 5728e01
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Source/ARTTokenParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ - (ARTTokenRequest *)sign:(NSString *)key withNonce:(NSString *)nonce {
NSString *keyName = keyComponents[0];
NSString *keySecret = keyComponents[1];
NSString *clientId = self.clientId ? self.clientId : @"";
NSTimeInterval ttl = self.ttl ? self.ttl : [ARTDefault ttl];

NSString *signText = [NSString stringWithFormat:@"%@\n%lld\n%@\n%@\n%lld\n%@\n", keyName, timeIntervalToMilliseconds(self.ttl), self.capability, clientId, dateToMilliseconds(self.timestamp), nonce];
NSString *signText = [NSString stringWithFormat:@"%@\n%lld\n%@\n%@\n%lld\n%@\n", keyName, timeIntervalToMilliseconds(ttl), self.capability, clientId, dateToMilliseconds(self.timestamp), nonce];
NSString *mac = hmacForDataAndKey([signText dataUsingEncoding:NSUTF8StringEncoding], [keySecret dataUsingEncoding:NSUTF8StringEncoding]);

return [[ARTTokenRequest alloc] initWithTokenParams:self keyName:keyName nonce:nonce mac:mac];
Expand Down
3 changes: 2 additions & 1 deletion Source/ARTTokenRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
#import "ARTTokenRequest.h"
#import "ARTTokenParams.h"
#import "ARTAuth+Private.h"
#import "ARTDefault.h"

@implementation ARTTokenRequest

- (instancetype)initWithTokenParams:(ARTTokenParams *)tokenParams keyName:(NSString *)keyName nonce:(NSString *)nonce mac:(NSString *)mac {
if (self = [super init]) {
self.ttl = tokenParams.ttl;
self.ttl = tokenParams.ttl ? tokenParams.ttl : [ARTDefault ttl];
self.capability = tokenParams.capability;
self.clientId = tokenParams.clientId;
self.timestamp = tokenParams.timestamp;
Expand Down
53 changes: 53 additions & 0 deletions Spec/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,59 @@ class Auth : QuickSpec {
}
}

it("example: if a client is initialised with TokenParams#ttl configured with a custom value, and a TokenParams object is passed in as an argument to #authorize with a null value for ttl, then the ttl used for every subsequent authorization will be null") {
let options = AblyTests.commonAppSetup()
options.defaultTokenParams = {
$0.ttl = 0.1;
$0.clientId = "tester";
return $0
}(ARTTokenParams())

let rest = ARTRest(options: options)

let testTokenParams = ARTTokenParams()
testTokenParams.ttl = 0
testTokenParams.clientId = nil

waitUntil(timeout: testTimeout) { done in
rest.auth.authorise(testTokenParams, options: nil) { tokenDetails, error in
expect(error).to(beNil())
guard let tokenDetails = tokenDetails else {
fail("TokenDetails is nil"); done(); return
}
guard let issued = tokenDetails.issued else {
fail("TokenDetails.issued is nil"); done(); return
}
guard let expires = tokenDetails.expires else {
fail("TokenDetails.expires is nil"); done(); return
}
expect(tokenDetails.clientId).to(beNil())
// `ttl` when omitted, the default value is applied
expect(issued.dateByAddingTimeInterval(ARTDefault.ttl())).to(equal(expires))
done()
}
}

// Subsequent authorization
waitUntil(timeout: testTimeout) { done in
rest.auth.authorise(nil, options: nil) { tokenDetails, error in
expect(error).to(beNil())
guard let tokenDetails = tokenDetails else {
fail("TokenDetails is nil"); done(); return
}
guard let issued = tokenDetails.issued else {
fail("TokenDetails.issued is nil"); done(); return
}
guard let expires = tokenDetails.expires else {
fail("TokenDetails.expires is nil"); done(); return
}
expect(tokenDetails.clientId).to(beNil())
expect(issued.dateByAddingTimeInterval(ARTDefault.ttl())).to(equal(expires))
done()
}
}
}

}

// RSA10k
Expand Down

0 comments on commit 5728e01

Please sign in to comment.