From 78d359d43bd9852b9f33691502e7d3646f47b696 Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Thu, 20 Jan 2011 13:24:03 +0000 Subject: [PATCH] When refreshing the token, the expires at value should be updated from the new token. (Fixes #3) --- Classes/LROAuth2AccessToken.m | 36 +++++++++++++++--------- LROAuth2Client.xcodeproj/project.pbxproj | 19 +++++++++---- 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/Classes/LROAuth2AccessToken.m b/Classes/LROAuth2AccessToken.m index 49ff83a..25c9a5b 100644 --- a/Classes/LROAuth2AccessToken.m +++ b/Classes/LROAuth2AccessToken.m @@ -8,20 +8,25 @@ #import "LROAuth2AccessToken.h" +@interface LROAuth2AccessToken () +@property (nonatomic, copy) NSDictionary *authResponseData; +- (void)extractExpiresAtFromResponse; +@end + +#pragma mark - @implementation LROAuth2AccessToken @dynamic accessToken; @dynamic refreshToken; +@synthesize authResponseData; @synthesize expiresAt; -- (id)initWithAuthorizationResponse:(NSDictionary *)_data; +- (id)initWithAuthorizationResponse:(NSDictionary *)data; { if (self = [super init]) { - authResponseData = [_data copy]; - - NSTimeInterval expiresIn = (NSTimeInterval)[[authResponseData valueForKey:@"expires_in"] intValue]; - expiresAt = [[NSDate alloc] initWithTimeIntervalSinceNow:expiresIn]; + authResponseData = [data copy]; + [self extractExpiresAtFromResponse]; } return self; } @@ -43,17 +48,22 @@ - (BOOL)hasExpired; return ([[NSDate date] earlierDate:expiresAt] == expiresAt); } -- (void)refreshFromAuthorizationResponse:(NSDictionary *)_data; +- (void)refreshFromAuthorizationResponse:(NSDictionary *)data; { - NSMutableDictionary *_tokenData = [authResponseData mutableCopy]; - [_tokenData setValue:[_data valueForKey:@"access_token"] forKey:@"access_token"]; + NSMutableDictionary *tokenData = [self.authResponseData mutableCopy]; + + [tokenData setObject:[data valueForKey:@"access_token"] forKey:@"access_token"]; + [tokenData setObject:[data objectForKey:@"expires_in"] forKey:@"expires_in"]; - NSTimeInterval expiresIn = (NSTimeInterval)[[authResponseData valueForKey:@"expires_in"] intValue]; + [self setAuthResponseData:tokenData]; + [tokenData release]; + [self extractExpiresAtFromResponse]; +} + +- (void)extractExpiresAtFromResponse +{ + NSTimeInterval expiresIn = (NSTimeInterval)[[self.authResponseData objectForKey:@"expires_in"] intValue]; expiresAt = [[NSDate alloc] initWithTimeIntervalSinceNow:expiresIn]; - - [authResponseData release]; - authResponseData = [_tokenData copy]; - [_tokenData release]; } #pragma mark - diff --git a/LROAuth2Client.xcodeproj/project.pbxproj b/LROAuth2Client.xcodeproj/project.pbxproj index 9d761d5..5da105d 100644 --- a/LROAuth2Client.xcodeproj/project.pbxproj +++ b/LROAuth2Client.xcodeproj/project.pbxproj @@ -302,7 +302,14 @@ isa = PBXProject; buildConfigurationList = 1DEB922208733DC00010E9CD /* Build configuration list for PBXProject "LROAuth2Client" */; compatibilityVersion = "Xcode 3.1"; + developmentRegion = English; hasScannedForEncodings = 1; + knownRegions = ( + English, + Japanese, + French, + German, + ); mainGroup = 0867D691FE84028FC02AAC07 /* Untitled */; productRefGroup = 034768DFFF38A50411DB9C8B /* Products */; projectDirPath = ""; @@ -367,7 +374,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 3.0; OTHER_LDFLAGS = "-ObjC"; PREBINDING = NO; - SDKROOT = iphoneos4.0; + SDKROOT = iphoneos; }; name = Debug; }; @@ -381,7 +388,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 3.0; OTHER_LDFLAGS = "-ObjC"; PREBINDING = NO; - SDKROOT = iphoneos4.0; + SDKROOT = iphoneos; }; name = Release; }; @@ -405,7 +412,7 @@ ); PREBINDING = NO; PRODUCT_NAME = "LROAuth2Client-Device"; - SDKROOT = iphoneos4.0; + SDKROOT = iphoneos; }; name = Debug; }; @@ -429,7 +436,7 @@ ); PREBINDING = NO; PRODUCT_NAME = "LROAuth2Client-Device"; - SDKROOT = iphoneos4.0; + SDKROOT = iphoneos; ZERO_LINK = NO; }; name = Release; @@ -454,7 +461,7 @@ ); PREBINDING = NO; PRODUCT_NAME = "LROAuth2Client-Simulator"; - SDKROOT = iphonesimulator4.0; + SDKROOT = iphoneos; }; name = Debug; }; @@ -478,7 +485,7 @@ ); PREBINDING = NO; PRODUCT_NAME = "LROAuth2Client-Simulator"; - SDKROOT = iphonesimulator4.0; + SDKROOT = iphoneos; ZERO_LINK = NO; }; name = Release;