From a4c5f64192e149951ab12124175621b9c37d2ac0 Mon Sep 17 00:00:00 2001 From: mitrenegade Date: Thu, 26 Sep 2013 15:34:29 -0700 Subject: [PATCH 1/2] NXOAuth2Connection applyParameters uses x-www-form-urlencoded format for parameter data --- Sources/OAuth2Client/NXOAuth2Connection.m | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index 7862cbd8..ecc1ab8c 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -10,7 +10,6 @@ // See README.md in this repository for // the full licence. // - #import "NSURL+NXOAuth2.h" #import "NSData+NXOAuth2.h" @@ -233,6 +232,7 @@ - (void)applyParameters:(NSDictionary *)parameters onRequest:(NSMutableURLReques && [httpMethod caseInsensitiveCompare:@"PUT"] != NSOrderedSame) { aRequest.URL = [aRequest.URL nxoauth2_URLByAddingParameters:parameters]; } else { +#if 0 NSInputStream *postBodyStream = [[NXOAuth2PostBodyStream alloc] initWithParameters:parameters]; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", [(NXOAuth2PostBodyStream *)postBodyStream boundary]]; @@ -241,6 +241,17 @@ - (void)applyParameters:(NSDictionary *)parameters onRequest:(NSMutableURLReques [aRequest setValue:contentLength forHTTPHeaderField:@"Content-Length"]; [aRequest setHTTPBodyStream:postBodyStream]; +#else + NSString * get = @""; + for (id key in [parameters allKeys]) { + get = [NSString stringWithFormat:@"%@%@%@=%@", get, [get length]?@"&":@"", key, parameters[key]]; + } + NSData *getData = [get dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]; + NSString *getLength = [NSString stringWithFormat:@"%lu", (unsigned long)[getData length]]; + [aRequest setValue:getLength forHTTPHeaderField:@"Content-Length"]; + [aRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; + [aRequest setHTTPBody:getData]; +#endif } } From 9edeb5143a92729db0c9b4d0cb3f3832d6969f92 Mon Sep 17 00:00:00 2001 From: Geoff Oberhofer Date: Fri, 5 Aug 2016 23:10:52 -0700 Subject: [PATCH 2/2] edited Oauth Client to retain headers, allow grant type to be set ~ ~ ".git/COMMIT_EDITMSG" 14L, 297C --- Sources/OAuth2Client/NXOAuth2Account.m | 6 +++- Sources/OAuth2Client/NXOAuth2AccountStore.h | 11 +++++-- Sources/OAuth2Client/NXOAuth2AccountStore.m | 34 +++++++++++++++----- Sources/OAuth2Client/NXOAuth2Client.h | 11 ++++++- Sources/OAuth2Client/NXOAuth2Client.m | 35 +++++++++++++++------ Sources/OAuth2Client/NXOAuth2Connection.m | 19 ++++++++--- 6 files changed, 90 insertions(+), 26 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2Account.m b/Sources/OAuth2Client/NXOAuth2Account.m index f8e27eec..0d155a3d 100644 --- a/Sources/OAuth2Client/NXOAuth2Account.m +++ b/Sources/OAuth2Client/NXOAuth2Account.m @@ -78,18 +78,22 @@ - (NXOAuth2Client *)oauthClient; NSString *clientID = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationClientID]; NSString *clientSecret = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationSecret]; + NSString *grantType = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationGrantType]; NSURL *authorizeURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAuthorizeURL]; NSURL *tokenURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenURL]; NSString *tokenType = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenType]; NSDictionary *additionalQueryParams = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters]; - + NSDictionary *headerParameters = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationHeaderParameters]; + oauthClient = [[NXOAuth2Client alloc] initWithClientID:clientID clientSecret:clientSecret + grantType:grantType authorizeURL:authorizeURL tokenURL:tokenURL accessToken:self.accessToken tokenType:tokenType persistent:NO + headerParameters:headerParameters delegate:self]; if (additionalQueryParams) { oauthClient.additionalAuthenticationParameters = additionalQueryParams; diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.h b/Sources/OAuth2Client/NXOAuth2AccountStore.h index 9e0aa105..5d152569 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.h +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.h @@ -29,6 +29,7 @@ extern NSString * const NXOAuth2AccountStoreNewAccountUserInfoKey; extern NSString * const kNXOAuth2AccountStoreConfigurationClientID; extern NSString * const kNXOAuth2AccountStoreConfigurationSecret; +extern NSString * const kNXOAuth2AccountStoreConfigurationGrantType; extern NSString * const kNXOAuth2AccountStoreConfigurationAuthorizeURL; extern NSString * const kNXOAuth2AccountStoreConfigurationTokenURL; extern NSString * const kNXOAuth2AccountStoreConfigurationRedirectURL; @@ -36,14 +37,14 @@ extern NSString * const kNXOAuth2AccountStoreConfigurationScope; extern NSString * const kNXOAuth2AccountStoreConfigurationTokenType; -/* +/* * Requires a NSDictionary as a value. * They are passed onto the authentication request as additional query parameters. * The dictionary may not contain the keys "grant_type", "client_id", "client_secret", * "username", "password", "redirect_uri", "code", "assertion_type" and "assertion" are not allowed. */ extern NSString * const kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters; - +extern NSString * const kNXOAuth2AccountStoreConfigurationHeaderParameters; #pragma mark Account Type @@ -81,25 +82,31 @@ typedef void(^NXOAuth2PreparedAuthorizationURLHandler)(NSURL *preparedURL); - (void)setClientID:(NSString *)aClientID secret:(NSString *)aSecret + grantType:(NSString *)aGrantType authorizationURL:(NSURL *)anAuthorizationURL tokenURL:(NSURL *)aTokenURL redirectURL:(NSURL *)aRedirectURL + headerParameters:(NSDictionary *) someHeaderParameters forAccountType:(NSString *)anAccountType; - (void)setClientID:(NSString *)aClientID secret:(NSString *)aSecret + grantType:(NSString *)aGrantType scope:(NSSet *)theScope authorizationURL:(NSURL *)anAuthorizationURL tokenURL:(NSURL *)aTokenURL redirectURL:(NSURL *)aRedirectURL + headerParameters:(NSDictionary *) someHeaderParameters forAccountType:(NSString *)anAccountType; - (void)setClientID:(NSString *)aClientID secret:(NSString *)aSecret + grantType:(NSString *)aGrantType scope:(NSSet *)theScope authorizationURL:(NSURL *)anAuthorizationURL tokenURL:(NSURL *)aTokenURL redirectURL:(NSURL *)aRedirectURL + headerParameters:(NSDictionary *) someHeaderParameters tokenType:(NSString *)aTokenType forAccountType:(NSString *)anAccountType; diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.m b/Sources/OAuth2Client/NXOAuth2AccountStore.m index f6c21690..d1dee6cd 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.m +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.m @@ -37,12 +37,14 @@ NSString * const kNXOAuth2AccountStoreConfigurationClientID = @"kNXOAuth2AccountStoreConfigurationClientID"; NSString * const kNXOAuth2AccountStoreConfigurationSecret = @"kNXOAuth2AccountStoreConfigurationSecret"; +NSString * const kNXOAuth2AccountStoreConfigurationGrantType = @"kNXOAuth2AccountStoreConfigurationGrantType"; NSString * const kNXOAuth2AccountStoreConfigurationAuthorizeURL = @"kNXOAuth2AccountStoreConfigurationAuthorizeURL"; NSString * const kNXOAuth2AccountStoreConfigurationTokenURL = @"kNXOAuth2AccountStoreConfigurationTokenURL"; NSString * const kNXOAuth2AccountStoreConfigurationRedirectURL = @"kNXOAuth2AccountStoreConfigurationRedirectURL"; NSString * const kNXOAuth2AccountStoreConfigurationScope = @"kNXOAuth2AccountStoreConfigurationScope"; NSString * const kNXOAuth2AccountStoreConfigurationTokenType = @"kNXOAuth2AccountStoreConfigurationTokenType"; NSString * const kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters = @"kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters"; +NSString * const kNXOAuth2AccountStoreConfigurationHeaderParameters = @"kNXOAuth2AccountStoreConfigurationHeaderParameters"; #pragma mark Account Type @@ -226,55 +228,67 @@ - (void)removeAccount:(NXOAuth2Account *)account; - (void)setClientID:(NSString *)aClientID secret:(NSString *)aSecret + grantType:(NSString *)aGrantType authorizationURL:(NSURL *)anAuthorizationURL tokenURL:(NSURL *)aTokenURL redirectURL:(NSURL *)aRedirectURL + headerParameters:(NSDictionary *) someHeaderParameters forAccountType:(NSString *)anAccountType; { [self setConfiguration:[NSDictionary dictionaryWithObjectsAndKeys: aClientID, kNXOAuth2AccountStoreConfigurationClientID, aSecret, kNXOAuth2AccountStoreConfigurationSecret, + aGrantType, kNXOAuth2AccountStoreConfigurationGrantType, anAuthorizationURL, kNXOAuth2AccountStoreConfigurationAuthorizeURL, aTokenURL, kNXOAuth2AccountStoreConfigurationTokenURL, - aRedirectURL, kNXOAuth2AccountStoreConfigurationRedirectURL, nil] + aRedirectURL, kNXOAuth2AccountStoreConfigurationRedirectURL, + someHeaderParameters, kNXOAuth2AccountStoreConfigurationHeaderParameters, nil] forAccountType:anAccountType]; } - (void)setClientID:(NSString *)aClientID secret:(NSString *)aSecret + grantType:(NSString *)aGrantType scope:(NSSet *)theScope authorizationURL:(NSURL *)anAuthorizationURL tokenURL:(NSURL *)aTokenURL redirectURL:(NSURL *)aRedirectURL + headerParameters:(NSDictionary *) someHeaderParameters forAccountType:(NSString *)anAccountType; { [self setConfiguration:[NSDictionary dictionaryWithObjectsAndKeys: aClientID, kNXOAuth2AccountStoreConfigurationClientID, aSecret, kNXOAuth2AccountStoreConfigurationSecret, + aGrantType, kNXOAuth2AccountStoreConfigurationGrantType, theScope, kNXOAuth2AccountStoreConfigurationScope, anAuthorizationURL, kNXOAuth2AccountStoreConfigurationAuthorizeURL, aTokenURL, kNXOAuth2AccountStoreConfigurationTokenURL, - aRedirectURL, kNXOAuth2AccountStoreConfigurationRedirectURL, nil] + aRedirectURL, kNXOAuth2AccountStoreConfigurationRedirectURL, + someHeaderParameters, kNXOAuth2AccountStoreConfigurationHeaderParameters, nil] forAccountType:anAccountType]; } - (void)setClientID:(NSString *)aClientID secret:(NSString *)aSecret + grantType:(NSString *)aGrantType scope:(NSSet *)theScope authorizationURL:(NSURL *)anAuthorizationURL tokenURL:(NSURL *)aTokenURL redirectURL:(NSURL *)aRedirectURL + headerParameters:(NSDictionary *) someHeaderParameters tokenType:(NSString *)aTokenType forAccountType:(NSString *)anAccountType; { [self setConfiguration:[NSDictionary dictionaryWithObjectsAndKeys: aClientID, kNXOAuth2AccountStoreConfigurationClientID, aSecret, kNXOAuth2AccountStoreConfigurationSecret, + aGrantType, kNXOAuth2AccountStoreConfigurationGrantType, theScope, kNXOAuth2AccountStoreConfigurationScope, anAuthorizationURL, kNXOAuth2AccountStoreConfigurationAuthorizeURL, aTokenURL, kNXOAuth2AccountStoreConfigurationTokenURL, aTokenType, kNXOAuth2AccountStoreConfigurationTokenType, - aRedirectURL, kNXOAuth2AccountStoreConfigurationRedirectURL, nil] + aRedirectURL, kNXOAuth2AccountStoreConfigurationRedirectURL, + someHeaderParameters, kNXOAuth2AccountStoreConfigurationHeaderParameters, nil] forAccountType:anAccountType]; } @@ -391,19 +405,23 @@ - (NXOAuth2Client *)pendingOAuthClientForAccountType:(NSString *)accountType; NSString *clientID = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationClientID]; NSString *clientSecret = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationSecret]; + NSString *grantType = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationGrantType]; NSSet *scope = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationScope]; NSURL *authorizeURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAuthorizeURL]; NSURL *tokenURL = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenURL]; NSString *tokenType = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationTokenType]; NSDictionary *additionalAuthenticationParameters = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationAdditionalAuthenticationParameters]; + NSDictionary *headerParameters = [configuration objectForKey:kNXOAuth2AccountStoreConfigurationHeaderParameters]; client = [[NXOAuth2Client alloc] initWithClientID:clientID clientSecret:clientSecret + grantType:grantType authorizeURL:authorizeURL tokenURL:tokenURL accessToken:nil tokenType:tokenType persistent:YES + headerParameters:headerParameters delegate:self]; client.persistent = NO; @@ -455,9 +473,9 @@ - (void)oauthClientNeedsAuthentication:(NXOAuth2Client *)client; NSURL *preparedURL = [client authorizationURLWithRedirectURL:redirectURL]; #if TARGET_OS_IPHONE - [[UIApplication sharedApplication] openURL:preparedURL]; + [[UIApplication sharedApplication] openURL:preparedURL]; #else - [[NSWorkspace sharedWorkspace] openURL:preparedURL]; + [[NSWorkspace sharedWorkspace] openURL:preparedURL]; #endif } @@ -596,7 +614,7 @@ + (NSDictionary *)accountsFromDefaultKeychain; + (void)storeAccountsInDefaultKeychain:(NSDictionary *)accounts; { [self removeFromDefaultKeychain]; - + NSString *serviceName = [self keychainServiceName]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:accounts]; @@ -619,7 +637,7 @@ + (void)removeFromDefaultKeychain; nil]; OSStatus __attribute__((unused)) err = SecItemDelete((__bridge CFDictionaryRef)query); NSAssert1((err == noErr || err == errSecItemNotFound), @"Error while deleting token from keychain: %ld", err); - + } #else @@ -675,7 +693,7 @@ + (NSDictionary *)accountsFromDefaultKeychain; + (void)storeAccountsInDefaultKeychain:(NSDictionary *)accounts; { [self removeFromDefaultKeychain]; - + NSString *serviceName = [self keychainServiceName]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:accounts]; diff --git a/Sources/OAuth2Client/NXOAuth2Client.h b/Sources/OAuth2Client/NXOAuth2Client.h index 59162b66..3a65b60c 100644 --- a/Sources/OAuth2Client/NXOAuth2Client.h +++ b/Sources/OAuth2Client/NXOAuth2Client.h @@ -35,9 +35,11 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh; @protected BOOL authenticating; BOOL persistent; - + NSString *clientId; NSString *clientSecret; + NSString *grantType; + NSDictionary *headerParameters; NSSet *desiredScope; NSString *userAgent; @@ -62,8 +64,11 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh; @property (nonatomic, copy, readonly) NSString *clientId; @property (nonatomic, copy, readonly) NSString *clientSecret; +@property (nonatomic, copy, readonly) NSString *grantType; @property (nonatomic, copy, readonly) NSString *tokenType; @property (nonatomic, strong, readwrite) NSDictionary *additionalAuthenticationParameters; +@property (nonatomic, strong, readwrite) NSDictionary *headerParameters; + @property (nonatomic, copy) NSSet *desiredScope; @property (nonatomic, copy) NSString *userAgent; @@ -84,12 +89,14 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh; */ - (id)initWithClientID:(NSString *)clientId clientSecret:(NSString *)clientSecret + grantType:(NSString *)aGrantType authorizeURL:(NSURL *)authorizeURL tokenURL:(NSURL *)tokenURL delegate:(NSObject *)delegate; - (id)initWithClientID:(NSString *)clientId clientSecret:(NSString *)clientSecret + grantType:(NSString *)aGrantType authorizeURL:(NSURL *)authorizeURL tokenURL:(NSURL *)tokenURL accessToken:(NXOAuth2AccessToken *)accessToken @@ -98,11 +105,13 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh; - (id)initWithClientID:(NSString *)clientId clientSecret:(NSString *)clientSecret + grantType:(NSString *)aGrantType authorizeURL:(NSURL *)authorizeURL tokenURL:(NSURL *)tokenURL accessToken:(NXOAuth2AccessToken *)accessToken tokenType:(NSString *)tokenType persistent:(BOOL)shouldPersist + headerParameters:(NSDictionary*)someheaderParameters delegate:(NSObject *)delegate; - (BOOL)openRedirectURL:(NSURL *)URL; diff --git a/Sources/OAuth2Client/NXOAuth2Client.m b/Sources/OAuth2Client/NXOAuth2Client.m index edab79ce..147fcfae 100644 --- a/Sources/OAuth2Client/NXOAuth2Client.m +++ b/Sources/OAuth2Client/NXOAuth2Client.m @@ -39,12 +39,14 @@ @implementation NXOAuth2Client - (id)initWithClientID:(NSString *)aClientId clientSecret:(NSString *)aClientSecret + grantType:(NSString *)aGrantType authorizeURL:(NSURL *)anAuthorizeURL tokenURL:(NSURL *)aTokenURL delegate:(NSObject *)aDelegate; { return [self initWithClientID:aClientId clientSecret:aClientSecret + grantType:(NSString *)aGrantType authorizeURL:anAuthorizeURL tokenURL:aTokenURL accessToken:nil @@ -54,6 +56,7 @@ - (id)initWithClientID:(NSString *)aClientId - (id)initWithClientID:(NSString *)aClientId clientSecret:(NSString *)aClientSecret + grantType:(NSString *)aGrantType authorizeURL:(NSURL *)anAuthorizeURL tokenURL:(NSURL *)aTokenURL accessToken:(NXOAuth2AccessToken *)anAccessToken @@ -62,21 +65,25 @@ - (id)initWithClientID:(NSString *)aClientId { return [self initWithClientID:aClientId clientSecret:aClientSecret + grantType:(NSString *)aGrantType authorizeURL:anAuthorizeURL tokenURL:aTokenURL accessToken:anAccessToken tokenType:nil persistent:shouldPersist + headerParameters:nil delegate:aDelegate]; } - (id)initWithClientID:(NSString *)aClientId clientSecret:(NSString *)aClientSecret + grantType:(NSString *)aGrantType authorizeURL:(NSURL *)anAuthorizeURL tokenURL:(NSURL *)aTokenURL accessToken:(NXOAuth2AccessToken *)anAccessToken tokenType:(NSString *)aTokenType persistent:(BOOL)shouldPersist + headerParameters:(NSDictionary*)someheaderParameters delegate:(NSObject *)aDelegate; { NSAssert(aTokenURL != nil && anAuthorizeURL != nil, @"No token or no authorize URL"); @@ -86,10 +93,12 @@ - (id)initWithClientID:(NSString *)aClientId clientId = [aClientId copy]; clientSecret = [aClientSecret copy]; + grantType = [aGrantType copy]; authorizeURL = [anAuthorizeURL copy]; tokenURL = [aTokenURL copy]; tokenType = [aTokenType copy]; accessToken = anAccessToken; + headerParameters = [someheaderParameters copy]; self.acceptType = @"application/json"; @@ -107,10 +116,10 @@ - (void)dealloc; #pragma mark Accessors -@synthesize clientId, clientSecret, tokenType; +@synthesize clientId, clientSecret, grantType, tokenType; @synthesize desiredScope, userAgent; @synthesize delegate, persistent, accessToken, authenticating; -@synthesize additionalAuthenticationParameters; +@synthesize additionalAuthenticationParameters, headerParameters; - (void)setAdditionalAuthenticationParameters:(NSDictionary *)value; { @@ -146,7 +155,7 @@ - (void)setPersistent:(BOOL)shouldPersist; if (persistent && !shouldPersist) { [accessToken removeFromDefaultKeychainWithServiceProviderName:[tokenURL host]]; } - + [self willChangeValueForKey:@"persistent"]; persistent = shouldPersist; [self didChangeValueForKey:@"persistent"]; @@ -227,6 +236,7 @@ - (NSURL *)authorizationURLWithRedirectURL:(NSURL *)redirectURL; NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"code", @"response_type", clientId, @"client_id", + grantType, @"grant_type", [redirectURL absoluteString], @"redirect_uri", nil]; @@ -310,9 +320,9 @@ - (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)red NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL]; [tokenRequest setHTTPMethod:@"POST"]; [authConnection cancel]; // just to be sure - + self.authenticating = YES; - + NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"authorization_code", @"grant_type", clientId, @"client_id", @@ -328,6 +338,11 @@ - (void)requestTokenWithAuthGrant:(NSString *)authGrant redirectURL:(NSURL *)red [parameters addEntriesFromDictionary:self.additionalAuthenticationParameters]; } + if (self.headerParameters) { + for (NSString * key in [headerParameters allKeys]) + [tokenRequest addValue:[headerParameters valueForKey:key] forHTTPHeaderField:key]; + } + authConnection = [[NXOAuth2Connection alloc] initWithRequest:tokenRequest requestParameters:parameters oauthClient:self @@ -369,9 +384,9 @@ - (void)authenticateWithUsername:(NSString *)username password:(NSString *)passw NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL]; [tokenRequest setHTTPMethod:@"POST"]; [authConnection cancel]; // just to be sure - + self.authenticating = YES; - + NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"password", @"grant_type", clientId, @"client_id", @@ -473,7 +488,7 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFinishWithData:(NSDa { if (connection == authConnection) { self.authenticating = NO; - + NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NXOAuth2AccessToken *newToken = [NXOAuth2AccessToken tokenWithResponseBody:result tokenType:self.tokenType ]; @@ -502,7 +517,7 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFailWithError:(NSErr if (connection == authConnection) { self.authenticating = NO; - + id context = connection.context; authConnection = nil; @@ -527,7 +542,7 @@ - (void)oauthConnection:(NXOAuth2Connection *)connection didFailWithError:(NSErr [waitingConnections removeAllObjects]; for (NXOAuth2Connection *connection in failedConnections) { id connectionDelegate = connection.delegate; - if ([connectionDelegate respondsToSelector:@selector(oauthConnection:didFailWithError:)]) { + if ([connectionDelegate respondsToSelector:@selector(oauthConnection:didFailWithError:)]) { [connectionDelegate oauthConnection:connection didFailWithError:retryFailedError]; } } diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index ecc1ab8c..a0ac857e 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -73,7 +73,7 @@ - (id)initWithRequest:(NSMutableURLRequest *)aRequest sendConnectionDidEndNotification = NO; delegate = aDelegate; // assign only client = aClient; - + request = [aRequest copy]; requestParameters = [someRequestParameters copy]; connection = [self createConnection]; @@ -86,7 +86,7 @@ - (void)dealloc; { if (sendConnectionDidEndNotification) [[NSNotificationCenter defaultCenter] postNotificationName:NXOAuth2ConnectionDidEndNotification object:self]; sendConnectionDidEndNotification = NO; - + [connection cancel]; #if (NXOAuth2ConnectionDebug) @@ -200,6 +200,7 @@ - (NSURLConnection *)createConnection; [startRequest setValue:client.acceptType forHTTPHeaderField:@"Accept"]; } + NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:startRequest delegate:self startImmediately:NO]; // don't start yet [aConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; // let's first schedule it in the current runloop. (see http://github.com/soundcloud/cocoa-api-wrapper/issues#issue/2 ) [aConnection start]; // now start @@ -527,14 +528,24 @@ - (NSURLRequest *)connection:(NSURLConnection *)aConnection willSendRequest:(NSU NSMutableURLRequest *mutableRequest = [aRequest mutableCopy]; mutableRequest.HTTPMethod = request.HTTPMethod; + if (mutableRequest.HTTPBody != aConnection.currentRequest.HTTPBody){ + [mutableRequest setHTTPBody:aConnection.currentRequest.HTTPBody]; + } + if (hostChanged || (schemeChanged && !schemeChangedToHTTPS)) { [mutableRequest setValue:nil forHTTPHeaderField:@"Authorization"]; // strip Authorization information return mutableRequest; } else { // iOS 5 automaticaly strips the authorization 'token' from the header. // Thus we have to add the OAuth2 'token' again. - [mutableRequest setValue:[NSString stringWithFormat:@"%@ %@", client.accessToken.tokenType, client.accessToken.accessToken] - forHTTPHeaderField:@"Authorization"]; + if ([[[aConnection.currentRequest allHTTPHeaderFields] allKeys] containsObject:@"Authorization"]) { + NSString *authorizationValue = [[aConnection.currentRequest allHTTPHeaderFields] objectForKey:@"Authorization"]; + [mutableRequest setValue: authorizationValue forHTTPHeaderField:@"Authorization"]; + } + else { + [mutableRequest setValue:[NSString stringWithFormat:@"%@ %@", client.accessToken.tokenType, client.accessToken.accessToken] + forHTTPHeaderField:@"Authorization"]; + } } return mutableRequest; }