Skip to content

Commit afae991

Browse files
author
toto
committed
Merge pull request #124 from AndreasTextunes/develop
Adding custom header in order to provide a POST with custom MIME types
2 parents c54f193 + c016a7a commit afae991

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
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
@@ -36,7 +36,6 @@ extern NSString * const kNXOAuth2AccountStoreConfigurationScope;
3636
extern NSString * const kNXOAuth2AccountStoreConfigurationTokenType;
3737
extern NSString * const kNXOAuth2AccountStoreConfigurationTokenRequestHTTPMethod;
3838

39-
4039
/*
4140
* Requires a NSDictionary as a value.
4241
* They are passed onto the authentication request as additional query parameters.
@@ -45,6 +44,13 @@ extern NSString * const kNXOAuth2AccountStoreConfigurationTokenRequestHTTPMethod
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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
NSString * const kNXOAuth2AccountStoreConfigurationTokenRequestHTTPMethod = @"kNXOAuth2AccountStoreConfigurationTokenRequestHTTPMethod";
4646
NSString * const kNXOAuth2AccountStoreConfigurationKeyChainGroup = @"kNXOAuth2AccountStoreConfigurationKeyChainGroup";
4747
NSString * const kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters = @"kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters";
48+
NSString * const kNXOAuth2AccountStoreConfigurationCustomHeaderFields = @"kNXOAuth2AccountStoreConfigurationCustomHeaderFields";
4849

4950
#pragma mark Account Type
5051

@@ -404,6 +405,7 @@ - (NXOAuth2Client *)pendingOAuthClientForAccountType:(NSString *)accountType;
404405
NSString *tokenRequestHTTPMethod = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenRequestHTTPMethod];
405406
NSString *keychainGroup = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationKeyChainGroup];
406407
NSDictionary *additionalAuthenticationParameters = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters];
408+
NSDictionary *customHeaderFields = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationCustomHeaderFields];
407409

408410
client = [[NXOAuth2Client alloc] initWithClientID:clientID
409411
clientSecret:clientSecret
@@ -424,6 +426,9 @@ - (NXOAuth2Client *)pendingOAuthClientForAccountType:(NSString *)accountType;
424426
NSAssert([additionalAuthenticationParameters isKindOfClass:[NSDictionary class]], @"additionalAuthenticationParameters have to be a NSDictionary");
425427
client.additionalAuthenticationParameters = additionalAuthenticationParameters;
426428
}
429+
if (customHeaderFields) {
430+
client.customHeaderFields = customHeaderFields;
431+
}
427432

428433
if (scope != nil) {
429434
client.desiredScope = scope;
@@ -606,7 +611,7 @@ + (NSDictionary *)accountsFromDefaultKeychain;
606611
result = (__bridge_transfer NSDictionary *)cfResult;
607612

608613
if (status != noErr) {
609-
NSAssert1(status == errSecItemNotFound, @"Unexpected error while fetching accounts from keychain: %d", (int)status);
614+
NSAssert1(status == errSecItemNotFound, @"Unexpected error while fetching accounts from keychain: %zd", status);
610615
return nil;
611616
}
612617

@@ -627,7 +632,7 @@ + (void)storeAccountsInDefaultKeychain:(NSDictionary *)accounts;
627632
data, kSecAttrGeneric,
628633
nil];
629634
OSStatus __attribute__((unused)) err = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
630-
NSAssert1(err == noErr, @"Error while adding token to keychain: %d", (int)err);
635+
NSAssert1(err == noErr, @"Error while adding token to keychain: %zd", err);
631636
}
632637

633638
+ (void)removeFromDefaultKeychain;
@@ -638,7 +643,7 @@ + (void)removeFromDefaultKeychain;
638643
serviceName, kSecAttrService,
639644
nil];
640645
OSStatus __attribute__((unused)) err = SecItemDelete((__bridge CFDictionaryRef)query);
641-
NSAssert1((err == noErr || err == errSecItemNotFound), @"Error while deleting token from keychain: ld", (int)err);
646+
NSAssert1((err == noErr || err == errSecItemNotFound), @"Error while deleting token from keychain: %zd", err);
642647

643648
}
644649

Sources/OAuth2Client/NXOAuth2Client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh;
6565
@property (nonatomic, copy, readonly) NSString *clientSecret;
6666
@property (nonatomic, copy, readonly) NSString *tokenType;
6767
@property (nonatomic, strong, readwrite) NSDictionary *additionalAuthenticationParameters;
68+
@property (nonatomic, strong, readwrite) NSDictionary *customHeaderFields;
6869

6970
@property (nonatomic, copy) NSSet *desiredScope;
7071
@property (nonatomic, copy) NSString *tokenRequestHTTPMethod; // defaults to POST

Sources/OAuth2Client/NXOAuth2Client.m

Lines changed: 19 additions & 0 deletions
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
@@ -393,6 +406,12 @@ - (void)authenticateWithUsername:(NSString *)username password:(NSString *)passw
393406
[parameters addEntriesFromDictionary:self.additionalAuthenticationParameters];
394407
}
395408

409+
if (self.customHeaderFields) {
410+
[self.customHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
411+
[tokenRequest addValue:obj forHTTPHeaderField:key];
412+
}];
413+
}
414+
396415
authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest
397416
requestParameters:parameters
398417
oauthClient:self

Sources/OAuth2Client/NXOAuth2Connection.m

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,38 @@ - (void)applyParameters:(NSDictionary *)parameters onRequest:(NSMutableURLReques
223223
NSString *httpMethod = [aRequest HTTPMethod];
224224
if ([httpMethod caseInsensitiveCompare:@"POST"] != NSOrderedSame
225225
&& [httpMethod caseInsensitiveCompare:@"PUT"] != NSOrderedSame) {
226+
226227
aRequest.URL = [aRequest.URL nxoauth2_URLByAddingParameters:parameters];
228+
227229
} else {
228-
NSInputStream *postBodyStream = [[NXOAuth2PostBodyStream alloc] initWithParameters:parameters];
229230

230-
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", [(NXOAuth2PostBodyStream *)postBodyStream boundary]];
231-
NSString *contentLength = [NSString stringWithFormat:@"%lld", [(NXOAuth2PostBodyStream *)postBodyStream length]];
232-
[aRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
233-
[aRequest setValue:contentLength forHTTPHeaderField:@"Content-Length"];
231+
NSString *contentType = [aRequest valueForHTTPHeaderField:@"Content-Type"];
232+
233+
if (!contentType || [contentType isEqualToString:@"multipart/form-data"]) {
234234

235-
[aRequest setHTTPBodyStream:postBodyStream];
235+
// sends the POST/PUT request as multipart/form-data as default
236+
237+
NSInputStream *postBodyStream = [[NXOAuth2PostBodyStream alloc] initWithParameters:parameters];
238+
239+
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",[(NXOAuth2PostBodyStream *)postBodyStream boundary]];
240+
NSString *contentLength = [NSString stringWithFormat:@"%lld", [(NXOAuth2PostBodyStream *)postBodyStream length]];
241+
[aRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
242+
[aRequest setValue:contentLength forHTTPHeaderField:@"Content-Length"];
243+
244+
[aRequest setHTTPBodyStream:postBodyStream];
245+
246+
} else if ([contentType isEqualToString:@"application/x-www-form-urlencoded"]) {
247+
248+
// sends the POST/PUT request as application/x-www-form-urlencoded
249+
250+
NSString *query = [[aRequest.URL nxoauth2_URLByAddingParameters:parameters] query];
251+
[aRequest setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];
252+
253+
}
254+
236255
}
237256
}
238257

239-
240258
- (BOOL)trustsAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
241259
forHostname:(NSString *)hostname
242260
withTrustMode:(NXOAuth2TrustMode)trustMode;

0 commit comments

Comments
 (0)