From c9238aedba0f8714eb356eac81a93e1bc91eae54 Mon Sep 17 00:00:00 2001 From: Bruce McTigue Date: Mon, 27 Apr 2015 11:05:30 -0600 Subject: [PATCH 01/19] Fixes for invalid token and more sensible notification after account store refresh token delegate call. --- Sources/OAuth2Client/NXOAuth2AccountStore.m | 8 ++------ Sources/OAuth2Client/NXOAuth2Connection.m | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.m b/Sources/OAuth2Client/NXOAuth2AccountStore.m index b14cbfcd..7b7321e4 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.m +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.m @@ -545,12 +545,8 @@ - (void)oauthClientDidRefreshAccessToken:(NXOAuth2Client *)client } foundAccount.accessToken = client.accessToken; - NSDictionary *userInfo = [NSDictionary dictionaryWithObject: foundAccount - forKey: NXOAuth2AccountStoreNewAccountUserInfoKey]; - - [[NSNotificationCenter defaultCenter] postNotificationName:NXOAuth2AccountStoreAccountsDidChangeNotification - object:self - userInfo:userInfo]; + [[NSNotificationCenter defaultCenter] postNotificationName:NXOAuth2AccountDidChangeAccessTokenNotification + object:self]; } - (void)addAccount:(NXOAuth2Account *)account; diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index db90e71f..bb80b0b4 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -418,7 +418,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon if (self.statusCode == 401 && client.accessToken.refreshToken != nil && authenticateHeader - && [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound) { + && [authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound) { [self cancel]; [client refreshAccessTokenAndRetryConnection:self]; } else { From 13690a09449a23c8fdebc21a1933639dc6d17cad Mon Sep 17 00:00:00 2001 From: Bruce McTigue Date: Mon, 27 Apr 2015 12:10:59 -0600 Subject: [PATCH 02/19] Update for issue #416. Update so connection code handles invalid_token and invalid_request header values. --- Sources/OAuth2Client/NXOAuth2Connection.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index bb80b0b4..02a47359 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -418,7 +418,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon if (self.statusCode == 401 && client.accessToken.refreshToken != nil && authenticateHeader - && [authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound) { + && ([authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound || [authenticateHeader rangeOfString:@"invalid_request"].location != NSNotFound)) { [self cancel]; [client refreshAccessTokenAndRetryConnection:self]; } else { @@ -469,7 +469,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection; } } if (authenticateHeader - && [authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound) { + && ([authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound || [authenticateHeader rangeOfString:@"invalid_request"].location != NSNotFound)) { client.accessToken = nil; } } From e030ae50d85c03a92c6a3d06e1745c0c6b27e63d Mon Sep 17 00:00:00 2001 From: Jude Venn Date: Sat, 9 May 2015 15:55:35 +0100 Subject: [PATCH 03/19] add headerFields dict property to NXOAuth2Request So we can add custom HTTP headers per request. --- Sources/OAuth2Client/NXOAuth2Request.h | 9 ++++++++ Sources/OAuth2Client/NXOAuth2Request.m | 31 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Sources/OAuth2Client/NXOAuth2Request.h b/Sources/OAuth2Client/NXOAuth2Request.h index aa601af5..09c9799f 100644 --- a/Sources/OAuth2Client/NXOAuth2Request.h +++ b/Sources/OAuth2Client/NXOAuth2Request.h @@ -37,6 +37,14 @@ sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler; ++ (void)performMethod:(NSString *)method + onResource:(NSURL *)resource + usingParameters:(NSDictionary *)parameters + usingHeaders:(NSDictionary *)headers + withAccount:(NXOAuth2Account *)account + sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler + responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler; + #pragma mark Lifecycle @@ -53,6 +61,7 @@ @property (nonatomic, strong, readwrite) NSURL *resource; @property (nonatomic, strong, readwrite) NSDictionary *parameters; +@property (nonatomic, strong, readwrite) NSDictionary *headerFields; #pragma mark Signed NSURLRequest diff --git a/Sources/OAuth2Client/NXOAuth2Request.m b/Sources/OAuth2Client/NXOAuth2Request.m index ad8564bd..3113e738 100644 --- a/Sources/OAuth2Client/NXOAuth2Request.m +++ b/Sources/OAuth2Client/NXOAuth2Request.m @@ -27,6 +27,7 @@ @interface NXOAuth2Request () @property (nonatomic, strong, readwrite) NXOAuth2Request *me; #pragma mark Apply Parameters - (void)applyParameters:(NSDictionary *)someParameters onRequest:(NSMutableURLRequest *)aRequest; +- (void)applyHeaders:(NSDictionary *)someHeaders onRequest:(NSMutableURLRequest *)aRequest; @end @@ -40,11 +41,29 @@ + (void)performMethod:(NSString *)aMethod withAccount:(NXOAuth2Account *)anAccount sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler; +{ + [self performMethod:aMethod + onResource:aResource + usingParameters:someParameters + usingHeaders:nil + withAccount:anAccount + sendProgressHandler:progressHandler + responseHandler:responseHandler]; +} + ++ (void)performMethod:(NSString *)aMethod + onResource:(NSURL *)aResource + usingParameters:(NSDictionary *)someParameters + usingHeaders:(NSDictionary *)someHeaders + withAccount:(NXOAuth2Account *)anAccount + sendProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler + responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler { NXOAuth2Request *request = [[NXOAuth2Request alloc] initWithResource:aResource method:aMethod parameters:someParameters]; request.account = anAccount; + request.headerFields = someHeaders; [request performRequestWithSendingProgressHandler:progressHandler responseHandler:responseHandler]; } @@ -81,6 +100,7 @@ - (NSURLRequest *)signedURLRequest; [request setHTTPMethod:self.requestMethod]; + [self applyHeaders:self.headerFields onRequest:request]; [self applyParameters:self.parameters onRequest:request]; if (self.account.oauthClient.userAgent && ![request valueForHTTPHeaderField:@"User-Agent"]) { @@ -105,6 +125,7 @@ - (void)performRequestWithSendingProgressHandler:(NXOAuth2ConnectionSendingProgr NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.resource]; [request setHTTPMethod:self.requestMethod]; + [self applyHeaders:self.headerFields onRequest:request]; self.connection = [[NXOAuth2Connection alloc] initWithRequest:request requestParameters:self.parameters oauthClient:self.account.oauthClient @@ -171,4 +192,14 @@ - (void)applyParameters:(NSDictionary *)someParameters onRequest:(NSMutableURLRe } } +- (void)applyHeaders:(NSDictionary *)someHeaders onRequest:(NSMutableURLRequest *)aRequest; +{ + for (id key in someHeaders) { + id value = someHeaders[key]; + if ([key isKindOfClass:[NSString class]] && [value isKindOfClass:[NSString class]]) { + [aRequest setValue:value forHTTPHeaderField:key]; + } + } +} + @end From 6c85b3bf6f25f04406e8cad8f14e26fe1c240c0f Mon Sep 17 00:00:00 2001 From: CHENHSIN-PANG Date: Sun, 31 May 2015 09:49:58 +0800 Subject: [PATCH 04/19] add customField support when executing to refresh the access token --- Podfile.lock | 10 +++++----- Sources/OAuth2Client/NXOAuth2Client.m | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Podfile.lock b/Podfile.lock index 873a876b..ddef80ec 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -13,9 +13,9 @@ DEPENDENCIES: - Specta (~> 0.2) SPEC CHECKSUMS: - Expecta: 03aabd0a89d8dea843baecb19a7fd7466a69a31d - OCMock: f6cb8c162ab9d5620dddf411282c7b2c0ee78854 - OHHTTPStubs: c1e362552b71b81e1deb7a80f44c51585b946c43 - Specta: 9141310f46b1f68b676650ff2854e1ed0b74163a + Expecta: a354d4633409dd9fe8c4f5ff5130426adbe31628 + OCMock: a73f69963a8a542b0b343e2617650e4dca0cbbc2 + OHHTTPStubs: 5cb8c4e590851d42cfb02fb6269d5404cfa2e9c3 + Specta: 15a276a6343867b426d5ed135d5aa4d04123a573 -COCOAPODS: 0.36.1 +COCOAPODS: 0.36.3 diff --git a/Sources/OAuth2Client/NXOAuth2Client.m b/Sources/OAuth2Client/NXOAuth2Client.m index d681776a..dffff386 100644 --- a/Sources/OAuth2Client/NXOAuth2Client.m +++ b/Sources/OAuth2Client/NXOAuth2Client.m @@ -484,6 +484,13 @@ - (void)refreshAccessTokenAndRetryConnection:(NXOAuth2Connection *)retryConnecti clientSecret, @"client_secret", accessToken.refreshToken, @"refresh_token", nil]; + + 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"]; } From 0385fa1879efe6a1bbcd8b7cf5f7d5ced536089d Mon Sep 17 00:00:00 2001 From: Bruce McTigue Date: Wed, 10 Jun 2015 14:36:23 -0600 Subject: [PATCH 05/19] Added defensive logic so [self.pendingOAuthClients removeObjectForKey:accountType] will not fail if accountType is nil. This logic already exists in another reference, so I added to 2 other places where trying to remove nil from self.pendingOAuthClients. --- Sources/OAuth2Client/NXOAuth2AccountStore.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.m b/Sources/OAuth2Client/NXOAuth2AccountStore.m index 7b7321e4..ebcff60e 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.m +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.m @@ -520,7 +520,9 @@ - (void)oauthClientDidGetAccessToken:(NXOAuth2Client *)client NSString *accountType; @synchronized (self.pendingOAuthClients) { accountType = [self accountTypeOfPendingOAuthClient:client]; - [self.pendingOAuthClients removeObjectForKey:accountType]; + if (accountType) { + [self.pendingOAuthClients removeObjectForKey:accountType]; + } } NXOAuth2Account *account = [[NXOAuth2Account alloc] initAccountWithOAuthClient:client accountType:accountType]; @@ -585,7 +587,9 @@ - (void)oauthClient:(NXOAuth2Client *)client didFailToGetAccessTokenWithError:(N NSString *accountType; @synchronized (self.pendingOAuthClients) { accountType = [self accountTypeOfPendingOAuthClient:client]; - [self.pendingOAuthClients removeObjectForKey:accountType]; + if (accountType) { + [self.pendingOAuthClients removeObjectForKey:accountType]; + } } NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: From da014b1bf4d122c7e85b17e362ce485ee4600328 Mon Sep 17 00:00:00 2001 From: Mathieu Monney Date: Thu, 20 Aug 2015 09:02:24 +0200 Subject: [PATCH 06/19] Fix non-standard invalid_token header detection This PR fixes a problem where the header of Www-authenticate was checked with a non-standard keywork for invalid token. Per the RFC, the standard keyword **invalid_grant** specify an invalid or expired token. Moreover it has been added a check to try to refresh the token if possible. --- Sources/OAuth2Client/NXOAuth2Connection.m | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index fb2e2114..dca65977 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -470,8 +470,16 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection; } } if (authenticateHeader - && [authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound) { - client.accessToken = nil; + && [authenticateHeader rangeOfString:@"invalid_grant"].location != NSNotFound) { + + // Try to refresh the token if possible, otherwise remove this account. + if (client.accessToken.refreshToken) { + [self cancel]; + [client refreshAccessTokenAndRetryConnection:self]; + return; + } else { + client.accessToken = nil; + } } } From 12241ed9ca37a01b7ba80d09e7a3d01806f7e21c Mon Sep 17 00:00:00 2001 From: Mathieu Monney Date: Thu, 20 Aug 2015 10:03:23 +0200 Subject: [PATCH 07/19] Added invalid_token detection to be backward compatible --- Sources/OAuth2Client/NXOAuth2Connection.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index dca65977..3c7fcbfc 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -470,7 +470,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection; } } if (authenticateHeader - && [authenticateHeader rangeOfString:@"invalid_grant"].location != NSNotFound) { + && ([authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound || [authenticateHeader rangeOfString:@"invalid_grant"].location != NSNotFound)) { // Try to refresh the token if possible, otherwise remove this account. if (client.accessToken.refreshToken) { From 585f0d53e13548e9609a785b7915be291556a2d2 Mon Sep 17 00:00:00 2001 From: sir_nacnud Date: Thu, 24 Sep 2015 17:11:01 +0200 Subject: [PATCH 08/19] Fixed bug where client delegate wasn't reassigned to account When the NXOAuth2Client is created in NXOAuth2AccountStore and passed to initAccountWithOAuthClient of NXOAuth2Account, the delegate wasn't reassigned to the account because oauthClient is nil when initAccountWithAccessToken is called. --- Sources/OAuth2Client/NXOAuth2Account.m | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/OAuth2Client/NXOAuth2Account.m b/Sources/OAuth2Client/NXOAuth2Account.m index 10b0e73e..be2ff895 100644 --- a/Sources/OAuth2Client/NXOAuth2Account.m +++ b/Sources/OAuth2Client/NXOAuth2Account.m @@ -49,6 +49,7 @@ - (instancetype)initAccountWithOAuthClient:(NXOAuth2Client *)anOAuthClient accou accountType:anAccountType]; if (self) { oauthClient = anOAuthClient; + oauthClient.delegate = self; } return self; } From 4d14a02519dd114fb708abb018a4793f350d0a8a Mon Sep 17 00:00:00 2001 From: Jude Venn Date: Thu, 15 Oct 2015 17:46:46 +0100 Subject: [PATCH 09/19] assert that headerFields keys and values are strings --- Sources/OAuth2Client/NXOAuth2Request.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2Request.m b/Sources/OAuth2Client/NXOAuth2Request.m index 3113e738..7c3cc1d1 100644 --- a/Sources/OAuth2Client/NXOAuth2Request.m +++ b/Sources/OAuth2Client/NXOAuth2Request.m @@ -196,9 +196,9 @@ - (void)applyHeaders:(NSDictionary *)someHeaders onRequest:(NSMutableURLRequest { for (id key in someHeaders) { id value = someHeaders[key]; - if ([key isKindOfClass:[NSString class]] && [value isKindOfClass:[NSString class]]) { - [aRequest setValue:value forHTTPHeaderField:key]; - } + NSAssert1([key isKindOfClass:[NSString class]], @"Header name should be a string. name=%@", key); + NSAssert2([value isKindOfClass:[NSString class]], @"Header value should be a string. name=%@, value=%@", key, value); + [aRequest setValue:value forHTTPHeaderField:key]; } } From ea956c27a8e7404b304e96b192e0f616ea92c106 Mon Sep 17 00:00:00 2001 From: Steve Robbins Date: Sun, 8 Nov 2015 20:48:45 +0000 Subject: [PATCH 10/19] Some APIs issue token responses that include scopes as an array instead of just as a string --- Sources/OAuth2Client/NXOAuth2AccessToken.m | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2AccessToken.m b/Sources/OAuth2Client/NXOAuth2AccessToken.m index 2b64be1e..4ff590cd 100644 --- a/Sources/OAuth2Client/NXOAuth2AccessToken.m +++ b/Sources/OAuth2Client/NXOAuth2AccessToken.m @@ -54,7 +54,7 @@ + (instancetype)tokenWithResponseBody:(NSString *)theResponseBody tokenType:(NSS NSString *expiresIn = [jsonDict objectForKey:@"expires_in"]; NSString *anAccessToken = [jsonDict objectForKey:@"access_token"]; NSString *aRefreshToken = [jsonDict objectForKey:@"refresh_token"]; - NSString *scopeString = [jsonDict objectForKey:@"scope"]; + NSObject *scopeObj = [jsonDict objectForKey:@"scope"]; // if the response overrides token_type we take it from the response if ([jsonDict objectForKey:@"token_type"]) { @@ -62,8 +62,13 @@ + (instancetype)tokenWithResponseBody:(NSString *)theResponseBody tokenType:(NSS } NSSet *scope = nil; - if (scopeString && ![scopeString isEqual:[NSNull null]]) { - scope = [NSSet setWithArray:[scopeString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; + if (scopeObj && ![scopeObj isEqual:[NSNull null]]) { + if([scopeObj isKindOfClass:[NSArray class]]) { + scope = [NSSet setWithArray:(NSArray*)scopeObj]; + } + else if([scopeObj isKindOfClass:[NSString class]]) { + scope = [NSSet setWithArray:[(NSString *)scopeObj componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; + } } NSDate *expiryDate = nil; From 0ef5b377755cce41588f40e473278cd0c144f833 Mon Sep 17 00:00:00 2001 From: Kevin Cador Date: Tue, 22 Dec 2015 10:25:35 +0100 Subject: [PATCH 11/19] Fix compilation issues --- Sources/OAuth2Client/NXOAuth2Connection.m | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index 4ca472b2..9bc5fe4c 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -420,7 +420,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon && client.accessToken.refreshToken != nil && authenticateHeader && ([authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound || - [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound ) + [authenticateHeader rangeOfString:@"expired_token"].location != NSNotFound )) { [self cancel]; [client refreshAccessTokenAndRetryConnection:self]; @@ -474,13 +474,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection; } } } - if (authenticateHeader -<<<<<<< HEAD - && ([authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound || [authenticateHeader rangeOfString:@"invalid_request"].location != NSNotFound)) { - client.accessToken = nil; -======= - && ([authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound || [authenticateHeader rangeOfString:@"invalid_grant"].location != NSNotFound)) { - + if (authenticateHeader && ([authenticateHeader rangeOfString:@"invalid_token"].location != NSNotFound || [authenticateHeader rangeOfString:@"invalid_grant"].location != NSNotFound)) { // Try to refresh the token if possible, otherwise remove this account. if (client.accessToken.refreshToken) { [self cancel]; @@ -489,7 +483,6 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection; } else { client.accessToken = nil; } ->>>>>>> develop } } From 40d32d2166eda16066340fa3a6b9bd00ba25d76e Mon Sep 17 00:00:00 2001 From: ReadmeCritic Date: Wed, 17 Feb 2016 07:03:05 -0800 Subject: [PATCH 12/19] Correct the capitalization of Xcode in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 README.md diff --git a/README.md b/README.md old mode 100755 new mode 100644 index ab6f2ddb..c060631a --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ In order to install the library this way add the following line to your `Podfile and run the following command `pod install`. -*Note:* CocoaPods is now the preferred way to integrate NXOAuth2Client into XCode +*Note:* CocoaPods is now the preferred way to integrate NXOAuth2Client into Xcode ### Manually including the library in your Xcode project From 51a2404a9aa240154dc5e8ce4eb308e36e9b8df9 Mon Sep 17 00:00:00 2001 From: sir_nacnud Date: Thu, 31 Mar 2016 13:18:47 +0200 Subject: [PATCH 13/19] Store delegates as weak if available --- Sources/OAuth2Client/NXOAuth2Client.h | 11 ++++++++++- Sources/OAuth2Client/NXOAuth2Connection.h | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2Client.h b/Sources/OAuth2Client/NXOAuth2Client.h index 501779cb..6ee73d29 100644 --- a/Sources/OAuth2Client/NXOAuth2Client.h +++ b/Sources/OAuth2Client/NXOAuth2Client.h @@ -57,7 +57,11 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh; NSInteger refreshConnectionDidRetryCount; // delegates +#if __has_feature(objc_arc_weak) + NSObject* __weak delegate; +#else NSObject* __unsafe_unretained delegate; // assigned +#endif } @property (nonatomic, readonly, getter = isAuthenticating) BOOL authenticating; @@ -74,7 +78,12 @@ extern NSString * const NXOAuth2ClientConnectionContextTokenRefresh; @property (nonatomic, copy) NSString *acceptType; // defaults to application/json @property (nonatomic, strong) NXOAuth2AccessToken *accessToken; -@property (nonatomic, unsafe_unretained) NSObject* delegate; + +#if __has_feature(objc_arc_weak) + @property (nonatomic, weak) NSObject* delegate; +#else + @property (nonatomic, unsafe_unretained) NSObject* delegate; +#endif @property (nonatomic, readonly) NXOAuth2Connection *authConnection; diff --git a/Sources/OAuth2Client/NXOAuth2Connection.h b/Sources/OAuth2Client/NXOAuth2Connection.h index c29feb54..7b885586 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.h +++ b/Sources/OAuth2Client/NXOAuth2Connection.h @@ -65,7 +65,11 @@ typedef void(^NXOAuth2ConnectionSendingProgressHandler)(unsigned long long bytes NXOAuth2Client *client; +#if __has_feature(objc_arc_weak) + NSObject *__weak delegate; +#else NSObject *__unsafe_unretained delegate; // assigned +#endif NXOAuth2ConnectionResponseHandler responseHandler; NXOAuth2ConnectionSendingProgressHandler sendingProgressHandler; @@ -77,7 +81,12 @@ typedef void(^NXOAuth2ConnectionSendingProgressHandler)(unsigned long long bytes #endif } -@property (nonatomic, unsafe_unretained) NSObject *delegate; +#if __has_feature(objc_arc_weak) + @property (nonatomic, weak) NSObject *delegate; +#else + @property (nonatomic, unsafe_unretained) NSObject *delegate; +#endif + @property (nonatomic, strong, readonly) NSData *data; @property (nonatomic, assign) BOOL savesData; @property (nonatomic, assign, readonly) long long expectedContentLength; From 4d4f85f8f1822be30b8bde95b178239dc07ada84 Mon Sep 17 00:00:00 2001 From: Falko Buttler Date: Sun, 15 May 2016 21:41:56 -0700 Subject: [PATCH 14/19] Bugfix for Fitbit OAuth problem --- Sources/OAuth2Client/NXOAuth2Connection.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index 9bc5fe4c..325177db 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -158,7 +158,7 @@ - (void)retry; - (NSURLConnection *)createConnection; { // if the request is a token refresh request don't sign it and don't check for the expiration of the token (we know that already) - NSString *oauthAuthorizationHeader = nil; + NSString *oauthAuthorizationHeader = request.allHTTPHeaderFields[@"Authorization"]; if (client.accessToken && ![[requestParameters objectForKey:@"grant_type"] isEqualToString:@"refresh_token"]) { From 474499fdeb322fb02407307608de8dd63f5d6a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Lima?= Date: Tue, 9 Aug 2016 12:32:56 +0100 Subject: [PATCH 15/19] added PATCH verb to automatic adding of parameters to HTTP body --- Sources/OAuth2Client/NXOAuth2Connection.m | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index 325177db..8cc44405 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -222,7 +222,8 @@ - (void)applyParameters:(NSDictionary *)parameters onRequest:(NSMutableURLReques NSString *httpMethod = [aRequest HTTPMethod]; if ([httpMethod caseInsensitiveCompare:@"POST"] != NSOrderedSame - && [httpMethod caseInsensitiveCompare:@"PUT"] != NSOrderedSame) { + && [httpMethod caseInsensitiveCompare:@"PUT"] != NSOrderedSame + && [httpMethod caseInsensitiveCompare:@"PATCH"] != NSOrderedSame) { aRequest.URL = [aRequest.URL nxoauth2_URLByAddingParameters:parameters]; @@ -232,7 +233,7 @@ - (void)applyParameters:(NSDictionary *)parameters onRequest:(NSMutableURLReques if (!contentType || [contentType isEqualToString:@"multipart/form-data"]) { - // sends the POST/PUT request as multipart/form-data as default + // sends the POST/PUT/PATCH request as multipart/form-data as default NSInputStream *postBodyStream = [[NXOAuth2PostBodyStream alloc] initWithParameters:parameters]; @@ -245,7 +246,7 @@ - (void)applyParameters:(NSDictionary *)parameters onRequest:(NSMutableURLReques } else if ([contentType isEqualToString:@"application/x-www-form-urlencoded"]) { - // sends the POST/PUT request as application/x-www-form-urlencoded + // sends the POST/PUT/PATCH request as application/x-www-form-urlencoded NSString *query = [[aRequest.URL nxoauth2_URLByAddingParameters:parameters] query]; [aRequest setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]]; From 59ff9e24a0ce2e6c7e71e9e0eededa0cc873da5a Mon Sep 17 00:00:00 2001 From: Taras Tsugrii Date: Fri, 18 Nov 2016 13:02:23 -0800 Subject: [PATCH 16/19] Add an explicit cast to long in format specifier https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html suggests an explicit cast to 'long' for 32/64 bit compatibility, since on 32 bit NSInteger is int/ unsigned int and on 64bit architectures it's long/unsigned long. --- Sources/OAuth2Client/NXOAuth2Connection.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index 8cc44405..ef9c7319 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -487,7 +487,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection; } } - NSString *localizedError = [NSString stringWithFormat:NSLocalizedString(@"HTTP Error: %d", @"NXOAuth2HTTPErrorDomain description"), self.statusCode]; + NSString *localizedError = [NSString stringWithFormat:NSLocalizedString(@"HTTP Error: %ld", @"NXOAuth2HTTPErrorDomain description"), (long)self.statusCode]; NSDictionary *errorUserInfo = [NSDictionary dictionaryWithObject:localizedError forKey:NSLocalizedDescriptionKey]; NSError *error = [NSError errorWithDomain:NXOAuth2HTTPErrorDomain code:self.statusCode From a29ddd6136694a251317046be89724cf998efa50 Mon Sep 17 00:00:00 2001 From: Ryan Dignard Date: Sat, 4 Feb 2017 17:52:42 -0800 Subject: [PATCH 17/19] Format string / parameter mismatch --- Sources/OAuth2Client/NXOAuth2Connection.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index 8cc44405..e6869515 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -502,7 +502,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection; - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; { #if (NXOAuth2ConnectionDebug) - NSLog(@"%.0fms (FAIL) - %@ (%@ %i)", -[startDate timeIntervalSinceNow]*1000.0, [self descriptionForRequest:request], [error domain], [error code]); + NSLog(@"%.0fms (FAIL) - %@ (%@ %ld)", -[startDate timeIntervalSinceNow]*1000.0, [self descriptionForRequest:request], [error domain], (long)[error code]); #endif if (sendConnectionDidEndNotification) [[NSNotificationCenter defaultCenter] postNotificationName:NXOAuth2ConnectionDidEndNotification object:self]; From 18a0984882a331daeda9febbfb2de289e8a80264 Mon Sep 17 00:00:00 2001 From: Hwee-Boon Yar Date: Wed, 30 May 2018 15:33:24 +0800 Subject: [PATCH 18/19] Fix: build error due to pragma mark --- Sources/OAuth2Client/NXOAuth2AccountStore.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.h b/Sources/OAuth2Client/NXOAuth2AccountStore.h index 9325e8bf..3180cfc0 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.h +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.h @@ -121,7 +121,7 @@ typedef void(^NXOAuth2PreparedAuthorizationURLHandler)(NSURL *preparedURL); - (NSDictionary *)configurationForAccountType:(NSString *)accountType; -#pragma Trust Mode Handler +#pragma mark Trust Mode Handler - (void)setTrustModeHandlerForAccountType:(NSString *)accountType block:(NXOAuth2TrustModeHandler)handler; - (NXOAuth2TrustModeHandler)trustModeHandlerForAccountType:(NSString *)accountType; From 838b80dbc739d6e1aa4531d1c61e590bab5f29a0 Mon Sep 17 00:00:00 2001 From: Duncan Cunningham Date: Fri, 8 Jun 2018 14:51:33 +0200 Subject: [PATCH 19/19] Fix warnings with OSStatus being incorrectly formatted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OSStatus is defined as SInt32 so it should be formatted as a signed integer. Specifying “zd” expects the type to be size_t. --- Sources/OAuth2Client/NXOAuth2AccountStore.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.m b/Sources/OAuth2Client/NXOAuth2AccountStore.m index 7f8978b2..dde5b927 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.m +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.m @@ -703,7 +703,7 @@ - (NSDictionary *)accountsFromDefaultKeychainWithAccessGroup:(NSString *)keyChai result = (__bridge_transfer NSDictionary *)cfResult; if (status != noErr) { - NSAssert1(status == errSecItemNotFound, @"Unexpected error while fetching accounts from keychain: %zd", status); + NSAssert1(status == errSecItemNotFound, @"Unexpected error while fetching accounts from keychain: %d", status); return nil; } @@ -731,7 +731,7 @@ - (void)storeAccountsInDefaultKeychain:(NSDictionary *)accounts withAccessGroup: #endif OSStatus __attribute__((unused)) err = SecItemAdd((__bridge CFDictionaryRef)query, NULL); - NSAssert1(err == noErr, @"Error while adding token to keychain: %zd", err); + NSAssert1(err == noErr, @"Error while adding token to keychain: %d", err); } - (void)removeFromDefaultKeychainWithAccessGroup:(NSString *)keyChainAccessGroup; @@ -749,7 +749,7 @@ - (void)removeFromDefaultKeychainWithAccessGroup:(NSString *)keyChainAccessGroup #endif OSStatus __attribute__((unused)) err = SecItemDelete((__bridge CFDictionaryRef)query); - NSAssert1((err == noErr || err == errSecItemNotFound), @"Error while deleting token from keychain: %zd", err); + NSAssert1((err == noErr || err == errSecItemNotFound), @"Error while deleting token from keychain: %d", err); }