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

Default token TTL to nil, not 60*60. #619

Merged
merged 1 commit into from
Jul 19, 2017
Merged
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
6 changes: 4 additions & 2 deletions Source/ARTJsonLikeEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ - (NSDictionary *)tokenRequestToDictionary:(ARTTokenRequest *)tokenRequest {

NSMutableDictionary *dictionary = [@{
@"keyName":tokenRequest.keyName ? tokenRequest.keyName : @"",
@"ttl":[NSNumber numberWithUnsignedLongLong: timeIntervalToMilliseconds(tokenRequest.ttl)],
@"capability":tokenRequest.capability ? tokenRequest.capability : @"",
@"timestamp":timestamp,
@"nonce":tokenRequest.nonce ? tokenRequest.nonce : @"",
Expand All @@ -414,6 +413,9 @@ - (NSDictionary *)tokenRequestToDictionary:(ARTTokenRequest *)tokenRequest {
if (tokenRequest.clientId) {
dictionary[@"clientId"] = tokenRequest.clientId;
}
if (tokenRequest.ttl) {
dictionary[@"ttl"] = [NSNumber numberWithUnsignedLongLong:timeIntervalToMilliseconds([tokenRequest.ttl doubleValue])];
}

return dictionary;
}
Expand Down Expand Up @@ -441,7 +443,7 @@ - (ARTTokenRequest *)tokenRequestFromDictionary:(NSDictionary *)input error:(NSE

ARTTokenParams *params = [[ARTTokenParams alloc] initWithClientId:[input artString:@"clientId"]
nonce:[input artString:@"nonce"]];
params.ttl = millisecondsToTimeInterval([input artInteger:@"ttl"]);
params.ttl = [NSNumber numberWithDouble:millisecondsToTimeInterval([input artInteger:@"ttl"])];
params.capability = [input artString:@"capability"];
params.timestamp = [input artDate:@"timestamp"];

Expand Down
4 changes: 2 additions & 2 deletions Source/ARTTokenParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ ART_ASSUME_NONNULL_BEGIN
@interface ARTTokenParams : NSObject

/**
Represents time to live (expiry) of this token in seconds.
Represents time to live (expiry) of this token as a NSTimeInterval.
*/
@property (nonatomic, assign) NSTimeInterval ttl;
@property (nonatomic, strong, nullable) NSNumber *ttl;

/**
Contains the capability JSON stringified.
Expand Down
15 changes: 7 additions & 8 deletions Source/ARTTokenParams.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ - (instancetype)initWithClientId:(NSString *)clientId {
- (instancetype)initWithClientId:(NSString *)clientId nonce:(NSString *)nonce {
if (self = [super init]) {
_timestamp = nil;
_ttl = [ARTDefault ttl];
_capability = @"{\"*\":[\"*\"]}"; // allow all
_clientId = clientId;
_nonce = nonce;
Expand All @@ -54,7 +53,7 @@ - (instancetype)initWithTokenParams:(ARTTokenParams *)tokenParams {
}

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

Expand All @@ -63,8 +62,8 @@ - (NSMutableArray *)toArray {

if (self.clientId)
[params addObject:[NSURLQueryItem queryItemWithName:@"clientId" value:self.clientId]];
if (self.ttl > 0)
[params addObject:[NSURLQueryItem queryItemWithName:@"ttl" value:[NSString stringWithFormat:@"%f", self.ttl]]];
if (self.ttl)
[params addObject:[NSURLQueryItem queryItemWithName:@"ttl" value:[NSString stringWithFormat:@"%@", self.ttl]]];
if (self.capability)
[params addObject:[NSURLQueryItem queryItemWithName:@"capability" value:self.capability]];
if (self.timestamp > 0)
Expand All @@ -78,8 +77,8 @@ - (NSMutableDictionary *)toDictionary {

if (self.clientId)
params[@"clientId"] = self.clientId;
if (self.ttl > 0)
params[@"ttl"] = [NSString stringWithFormat:@"%f", self.ttl];
if (self.ttl)
params[@"ttl"] = [NSString stringWithFormat:@"%@", self.ttl];
if (self.capability)
params[@"capability"] = self.capability;
if (self.timestamp > 0)
Expand Down Expand Up @@ -153,9 +152,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 *ttl = self.ttl ? [NSString stringWithFormat:@"%lld", timeIntervalToMilliseconds([self.ttl doubleValue])] : @"";

NSString *signText = [NSString stringWithFormat:@"%@\n%lld\n%@\n%@\n%lld\n%@\n", keyName, timeIntervalToMilliseconds(ttl), self.capability, clientId, dateToMilliseconds(self.timestamp), nonce];
NSString *signText = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%lld\n%@\n", keyName, 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
4 changes: 2 additions & 2 deletions Source/ARTTokenRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ ART_ASSUME_NONNULL_BEGIN
@property (nonatomic, copy) NSString *capability;

/**
Represents time to live (expiry) of this token in seconds.
Represents time to live (expiry) of this token as a NSTimeInterval.
*/
@property (nonatomic, assign) NSTimeInterval ttl;
@property (nonatomic, strong, nullable) NSNumber *ttl;

/**
Timestamp (in millis since the epoch) of this request. Timestamps, in conjunction with the nonce, are used to prevent n requests from being replayed.
Expand Down
6 changes: 3 additions & 3 deletions Source/ARTTokenRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ @implementation ARTTokenRequest

- (instancetype)initWithTokenParams:(ARTTokenParams *)tokenParams keyName:(NSString *)keyName nonce:(NSString *)nonce mac:(NSString *)mac {
if (self = [super init]) {
self.ttl = tokenParams.ttl ? tokenParams.ttl : [ARTDefault ttl];
self.ttl = tokenParams.ttl;
self.capability = tokenParams.capability;
self.clientId = tokenParams.clientId;
self.timestamp = tokenParams.timestamp;
Expand All @@ -31,7 +31,7 @@ - (NSDictionary *)asDictionary {
}

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

Expand All @@ -52,7 +52,7 @@ + (ARTTokenRequest *__art_nullable)fromJson:(id<ARTJsonCompatible>)json error:(N
nonce:dict[@"nonce"]
mac:dict[@"mac"]];
tokenRequest.clientId = dict[@"clientId"];
tokenRequest.ttl = millisecondsToTimeInterval([dict[@"ttl"] doubleValue]);
tokenRequest.ttl = dict[@"ttl"] ? [NSNumber numberWithDouble:millisecondsToTimeInterval([dict[@"ttl"] unsignedLongLongValue])] : nil;
tokenRequest.capability = dict[@"capability"];
tokenRequest.timestamp = [NSDate dateWithTimeIntervalSince1970:[dict[@"timestamp"] doubleValue] / 1000];

Expand Down
Loading