Skip to content

Commit

Permalink
When refreshing the token, the expires at value should be updated fro…
Browse files Browse the repository at this point in the history
…m the new token. (Fixes #3)
  • Loading branch information
Luke Redpath committed Jan 20, 2011
1 parent 5ed9d52 commit 78d359d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
36 changes: 23 additions & 13 deletions Classes/LROAuth2AccessToken.m
Expand Up @@ -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;
}
Expand All @@ -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 -
Expand Down
19 changes: 13 additions & 6 deletions LROAuth2Client.xcodeproj/project.pbxproj
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -367,7 +374,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 3.0;
OTHER_LDFLAGS = "-ObjC";
PREBINDING = NO;
SDKROOT = iphoneos4.0;
SDKROOT = iphoneos;
};
name = Debug;
};
Expand All @@ -381,7 +388,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 3.0;
OTHER_LDFLAGS = "-ObjC";
PREBINDING = NO;
SDKROOT = iphoneos4.0;
SDKROOT = iphoneos;
};
name = Release;
};
Expand All @@ -405,7 +412,7 @@
);
PREBINDING = NO;
PRODUCT_NAME = "LROAuth2Client-Device";
SDKROOT = iphoneos4.0;
SDKROOT = iphoneos;
};
name = Debug;
};
Expand All @@ -429,7 +436,7 @@
);
PREBINDING = NO;
PRODUCT_NAME = "LROAuth2Client-Device";
SDKROOT = iphoneos4.0;
SDKROOT = iphoneos;
ZERO_LINK = NO;
};
name = Release;
Expand All @@ -454,7 +461,7 @@
);
PREBINDING = NO;
PRODUCT_NAME = "LROAuth2Client-Simulator";
SDKROOT = iphonesimulator4.0;
SDKROOT = iphoneos;
};
name = Debug;
};
Expand All @@ -478,7 +485,7 @@
);
PREBINDING = NO;
PRODUCT_NAME = "LROAuth2Client-Simulator";
SDKROOT = iphonesimulator4.0;
SDKROOT = iphoneos;
ZERO_LINK = NO;
};
name = Release;
Expand Down

0 comments on commit 78d359d

Please sign in to comment.