Skip to content
This repository has been archived by the owner on Oct 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1 from ryanmaxwell/master
Browse files Browse the repository at this point in the history
decouple refreshToken and expiration properties.
  • Loading branch information
chris-unleashed committed Mar 1, 2016
2 parents d7a84bc + 4aaf78d commit 2bd10e0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
10 changes: 8 additions & 2 deletions GROAuth2SessionManager/AFOAuthCredential.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
/**
The OAuth refresh token.
*/
@property (readonly, nonatomic) NSString *refreshToken;
@property (copy, nonatomic) NSString *refreshToken;

/**
The expiration of the access token. This must not be `nil`.
*/
@property (strong, nonatomic) NSDate *expiration;

/**
Whether the OAuth credentials are expired.
Expand Down Expand Up @@ -84,7 +89,8 @@
@param expiration The expiration of the access token. This must not be `nil`.
*/
- (void)setRefreshToken:(NSString *)refreshToken
expiration:(NSDate *)expiration;
expiration:(NSDate *)expiration
__deprecated_msg("use refreshToken and expiration instead.");

///-----------------------------------------
/// @name Storing and Retrieving Credentials
Expand Down
13 changes: 7 additions & 6 deletions GROAuth2SessionManager/AFOAuthCredential.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
@interface AFOAuthCredential ()
@property (readwrite, nonatomic) NSString *accessToken;
@property (readwrite, nonatomic) NSString *tokenType;
@property (readwrite, nonatomic) NSString *refreshToken;
@property (readwrite, nonatomic) NSDate *expiration;
@end

@implementation AFOAuthCredential
Expand Down Expand Up @@ -77,14 +75,17 @@ - (NSString *)description {
- (void)setRefreshToken:(NSString *)refreshToken
expiration:(NSDate *)expiration
{
if (!refreshToken || !expiration) {
return;
}

self.refreshToken = refreshToken;
self.expiration = expiration;
}

- (void)setExpiration:(NSDate *)expiration
{
NSParameterAssert(expiration);

_expiration = expiration;
}

- (BOOL)isExpired {
return [self.expiration compare:[NSDate date]] == NSOrderedAscending;
}
Expand Down
10 changes: 6 additions & 4 deletions GROAuth2SessionManager/GROAuth2SessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,22 @@ - (void)authenticateUsingOAuthWithPath:(NSString *)path parameters:(NSDictionary
return;
}

AFOAuthCredential *credential = [AFOAuthCredential credentialWithOAuthToken:[responseObject valueForKey:@"access_token"] tokenType:[responseObject valueForKey:@"token_type"]];

NSString *refreshToken = [responseObject valueForKey:@"refresh_token"];
if (refreshToken == nil || [refreshToken isEqual:[NSNull null]]) {
refreshToken = [parameters valueForKey:@"refresh_token"];
}

AFOAuthCredential *credential = [AFOAuthCredential credentialWithOAuthToken:[responseObject valueForKey:@"access_token"] tokenType:[responseObject valueForKey:@"token_type"]];
credential.refreshToken = refreshToken;

NSDate *expireDate = [NSDate distantFuture];
id expiresIn = [responseObject valueForKey:@"expires_in"];
if (expiresIn != nil && ![expiresIn isEqual:[NSNull null]]) {
expireDate = [NSDate dateWithTimeIntervalSinceNow:[expiresIn doubleValue]];
}

[credential setRefreshToken:refreshToken expiration:expireDate];
credential.expiration = expireDate;

[self setAuthorizationHeaderWithCredential:credential];

Expand Down

0 comments on commit 2bd10e0

Please sign in to comment.