diff --git a/Sources/OAuth2Client/NXOAuth2Client.m b/Sources/OAuth2Client/NXOAuth2Client.m index dd3843e2..3aff65b4 100644 --- a/Sources/OAuth2Client/NXOAuth2Client.m +++ b/Sources/OAuth2Client/NXOAuth2Client.m @@ -474,6 +474,17 @@ - (void)refreshAccessTokenAndRetryConnection:(NXOAuth2Connection *)retryConnecti clientSecret, @"client_secret", accessToken.refreshToken, @"refresh_token", nil]; + + if (self.additionalAuthenticationParameters) { + [parameters addEntriesFromDictionary:self.additionalAuthenticationParameters]; + } + + if (self.customHeaderFields) { + [self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { + [tokenRequest addValue:obj forHTTPHeaderField:key]; + }]; + } + if (self.desiredScope) { [parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"]; } diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index db90e71f..2a1156cb 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -415,10 +415,14 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon } } } - if (self.statusCode == 401 - && client.accessToken.refreshToken != nil - && authenticateHeader - && [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound) { + + BOOL shouldRefresh = (self.statusCode == 401) + && (client.accessToken.hasExpired) + && (client.accessToken.refreshToken != nil) + && authenticateHeader + && [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound; + + if (shouldRefresh) { [self cancel]; [client refreshAccessTokenAndRetryConnection:self]; } else { diff --git a/Sources/OAuth2Client/NXOAuth2Request.h b/Sources/OAuth2Client/NXOAuth2Request.h index aa601af5..ca795827 100644 --- a/Sources/OAuth2Client/NXOAuth2Request.h +++ b/Sources/OAuth2Client/NXOAuth2Request.h @@ -20,6 +20,7 @@ @interface NXOAuth2Request : NSObject { @private NSDictionary *parameters; + NSDictionary *customHeaderFields; NSURL *resource; NSString *requestMethod; NXOAuth2Account *account; @@ -37,6 +38,14 @@ sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler; ++ (void)performMethod:(NSString *)method + onResource:(NSURL *)resource + usingParameters:(NSDictionary *)parameters + headerFields:(NSDictionary *)headerFields + withAccount:(NXOAuth2Account *)account + sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler + responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler; + #pragma mark Lifecycle @@ -52,6 +61,7 @@ @property (nonatomic, strong, readwrite) NSString *requestMethod; @property (nonatomic, strong, readwrite) NSURL *resource; @property (nonatomic, strong, readwrite) NSDictionary *parameters; +@property (nonatomic, strong, readwrite) NSDictionary *customHeaderFields; #pragma mark Signed NSURLRequest diff --git a/Sources/OAuth2Client/NXOAuth2Request.m b/Sources/OAuth2Client/NXOAuth2Request.m index ad8564bd..f945f2ab 100644 --- a/Sources/OAuth2Client/NXOAuth2Request.m +++ b/Sources/OAuth2Client/NXOAuth2Request.m @@ -37,6 +37,7 @@ @implementation NXOAuth2Request + (void)performMethod:(NSString *)aMethod onResource:(NSURL *)aResource usingParameters:(NSDictionary *)someParameters + headerFields:(NSDictionary *)headerFields withAccount:(NXOAuth2Account *)anAccount sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler; @@ -45,9 +46,24 @@ + (void)performMethod:(NSString *)aMethod method:aMethod parameters:someParameters]; request.account = anAccount; + request.customHeaderFields = headerFields; [request performRequestWithSendingProgressHandler:progressHandler responseHandler:responseHandler]; } ++ (void)performMethod:(NSString *)aMethod + onResource:(NSURL *)aResource + usingParameters:(NSDictionary *)someParameters + withAccount:(NXOAuth2Account *)anAccount + sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler + responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler; +{ + [self performMethod:aMethod + onResource:aResource + usingParameters:someParameters + withAccount:anAccount + sendProgressHandler:progressHandler + responseHandler:responseHandler]; +} #pragma mark Lifecycle @@ -66,6 +82,7 @@ - (instancetype)initWithResource:(NSURL *)aResource method:(NSString *)aMethod p #pragma mark Accessors @synthesize parameters; +@synthesize customHeaderFields; @synthesize resource; @synthesize requestMethod; @synthesize account; @@ -105,6 +122,15 @@ - (void)performRequestWithSendingProgressHandler:(NXOAuth2ConnectionSendingProgr NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.resource]; [request setHTTPMethod:self.requestMethod]; + + if (self.customHeaderFields) { + [self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *field, NSString *value, BOOL *stop) { + NSAssert([field isKindOfClass:NSString.class], @"Invalid Type For Field - String Expected"); + NSAssert([value isKindOfClass:NSString.class], @"Invalid Type For Value - String Expected"); + [request setValue:value forHTTPHeaderField:field]; + }]; + } + self.connection = [[NXOAuth2Connection alloc] initWithRequest:request requestParameters:self.parameters oauthClient:self.account.oauthClient