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

decouple refreshToken and expiration properties. #1

Merged
merged 1 commit into from
Mar 1, 2016
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
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