Skip to content

Commit 8415416

Browse files
author
AndreasTextunes
committed
Merge pull request #1 from ghvillasboas/develop
Wiring up the custom http headers for all types of requests
2 parents d0d98f1 + 9b41e68 commit 8415416

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

Sources/OAuth2Client/NXOAuth2Account.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ - (NXOAuth2Client *)oauthClient;
9292
NSString *tokenType = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenType];
9393
NSString *keychainGroup = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenType];
9494
NSDictionary *additionalQueryParams = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters];
95+
NSDictionary *customHeaderFields = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationCustomHeaderFields];
9596

9697
oauthClient = [[NXOAuth2Client alloc] initWithClientID:clientID
9798
clientSecret:clientSecret
@@ -106,6 +107,10 @@ - (NXOAuth2Client *)oauthClient;
106107
oauthClient.additionalAuthenticationParameters = additionalQueryParams;
107108
}
108109

110+
if (customHeaderFields) {
111+
oauthClient.customHeaderFields = customHeaderFields;
112+
}
113+
109114
}
110115
}
111116
return oauthClient;

Sources/OAuth2Client/NXOAuth2AccountStore.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ extern NSString * const kNXOAuth2AccountStoreConfigurationRedirectURL;
3535
extern NSString * const kNXOAuth2AccountStoreConfigurationScope;
3636
extern NSString * const kNXOAuth2AccountStoreConfigurationTokenType;
3737
extern NSString * const kNXOAuth2AccountStoreConfigurationTokenRequestHTTPMethod;
38-
extern NSString * const kNXOAuth2AccountStoreConfigurationCustomHeaderFields;
3938

4039
/*
4140
* Requires a NSDictionary as a value.
@@ -45,6 +44,13 @@ extern NSString * const kNXOAuth2AccountStoreConfigurationCustomHeaderFields;
4544
*/
4645
extern NSString * const kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters;
4746

47+
/*
48+
* Requires a NSDictionary as a value.
49+
* They are passed onto the HTTP Header Fields request as additional parameters.
50+
* Example of a valid setup: @{ @"Content-type" : @"application/x-www-form-urlencoded" }
51+
*/
52+
extern NSString * const kNXOAuth2AccountStoreConfigurationCustomHeaderFields;
53+
4854

4955
#pragma mark Account Type
5056

Sources/OAuth2Client/NXOAuth2AccountStore.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,14 +426,13 @@ - (NXOAuth2Client *)pendingOAuthClientForAccountType:(NSString *)accountType;
426426
NSAssert([additionalAuthenticationParameters isKindOfClass:[NSDictionary class]], @"additionalAuthenticationParameters have to be a NSDictionary");
427427
client.additionalAuthenticationParameters = additionalAuthenticationParameters;
428428
}
429+
if (customHeaderFields) {
430+
client.customHeaderFields = customHeaderFields;
431+
}
429432

430433
if (scope != nil) {
431434
client.desiredScope = scope;
432435
}
433-
434-
if (customHeaderFields) {
435-
client.customHeaderFields = customHeaderFields;
436-
}
437436

438437
[self.pendingOAuthClients setObject:client forKey:accountType];
439438
}
@@ -517,7 +516,9 @@ - (void)oauthClientDidLoseAccessToken:(NXOAuth2Client *)client;
517516
NSString *accountType;
518517
@synchronized (self.pendingOAuthClients) {
519518
accountType = [self accountTypeOfPendingOAuthClient:client];
520-
[self.pendingOAuthClients removeObjectForKey:accountType];
519+
if (accountType) {
520+
[self.pendingOAuthClients removeObjectForKey:accountType];
521+
}
521522
}
522523
}
523524

Sources/OAuth2Client/NXOAuth2Client.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ - (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)red
330330
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
331331
}
332332

333+
if (self.customHeaderFields) {
334+
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
335+
[tokenRequest addValue:obj forHTTPHeaderField:key];
336+
}];
337+
}
338+
333339
if (self.additionalAuthenticationParameters) {
334340
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
335341
}
@@ -360,6 +366,13 @@ - (void)authenticateWithClientCredentials;
360366
if (self.desiredScope) {
361367
[parameters setObject:[[self.desiredScope allObjects] componentsJoinedByString:@" "] forKey:@"scope"];
362368
}
369+
370+
if (self.customHeaderFields) {
371+
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
372+
[tokenRequest addValue:obj forHTTPHeaderField:key];
373+
}];
374+
}
375+
363376
authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
364377
requestParameters:parameters
365378
oauthClient:self
@@ -394,7 +407,9 @@ - (void)authenticateWithUsername:(NSString *)username password:(NSString *)passw
394407
}
395408

396409
if (self.customHeaderFields) {
397-
[tokenRequest setAllHTTPHeaderFields:self.customHeaderFields];
410+
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
411+
[tokenRequest addValue:obj forHTTPHeaderField:key];
412+
}];
398413
}
399414

400415
authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest

0 commit comments

Comments
 (0)