diff --git a/Sources/OAuth2Client/NXOAuth2AccountStore.m b/Sources/OAuth2Client/NXOAuth2AccountStore.m index b14cbfcd..4791679e 100644 --- a/Sources/OAuth2Client/NXOAuth2AccountStore.m +++ b/Sources/OAuth2Client/NXOAuth2AccountStore.m @@ -544,9 +544,13 @@ - (void)oauthClientDidRefreshAccessToken:(NXOAuth2Client *)client } } - foundAccount.accessToken = client.accessToken; - NSDictionary *userInfo = [NSDictionary dictionaryWithObject: foundAccount - forKey: NXOAuth2AccountStoreNewAccountUserInfoKey]; + NSDictionary *userInfo = @{}; + + if (foundAccount) + { + foundAccount.accessToken = client.accessToken; + userInfo = @{ NXOAuth2AccountStoreNewAccountUserInfoKey : foundAccount}; + } [[NSNotificationCenter defaultCenter] postNotificationName:NXOAuth2AccountStoreAccountsDidChangeNotification object:self @@ -589,13 +593,24 @@ - (void)oauthClient:(NXOAuth2Client *)client didFailToGetAccessTokenWithError:(N NSString *accountType; @synchronized (self.pendingOAuthClients) { accountType = [self accountTypeOfPendingOAuthClient:client]; - [self.pendingOAuthClients removeObjectForKey:accountType]; + + if (nil != accountType) + { + [self.pendingOAuthClients removeObjectForKey:accountType]; + } } - NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: - accountType, kNXOAuth2AccountStoreAccountType, - error, NXOAuth2AccountStoreErrorKey, nil]; - + NSDictionary *userInfo = nil; + if (nil != accountType) + { + userInfo = @{kNXOAuth2AccountStoreAccountType: accountType, NXOAuth2AccountStoreErrorKey : error}; + } + else + { + userInfo = @{NXOAuth2AccountStoreErrorKey : error}; + } + + [[NSNotificationCenter defaultCenter] postNotificationName:NXOAuth2AccountStoreDidFailToRequestAccessNotification object:self userInfo:userInfo]; @@ -640,6 +655,13 @@ - (void)accountDidChangeAccessToken:(NSNotification *)aNotification; // Save all accounts in the default keychain. [self storeAccountsInDefaultKeychain:self.accountsDict withAccessGroup:self.keychainAccessGroup]; } + + NXOAuth2Account* notificationSender = aNotification.object; + + NSDictionary* userInfo = @{ NXOAuth2AccountStoreNewAccountUserInfoKey : notificationSender}; + [[NSNotificationCenter defaultCenter] postNotificationName: NXOAuth2AccountStoreAccountsDidChangeNotification + object: self + userInfo: userInfo]; } - (void)accountDidLoseAccessToken:(NSNotification *)aNotification; diff --git a/Sources/OAuth2Client/NXOAuth2Connection.m b/Sources/OAuth2Client/NXOAuth2Connection.m index db90e71f..4232b20d 100644 --- a/Sources/OAuth2Client/NXOAuth2Connection.m +++ b/Sources/OAuth2Client/NXOAuth2Connection.m @@ -181,6 +181,20 @@ - (NSURLConnection *)createConnection; oauthAuthorizationHeader = [NSString stringWithFormat:@"%@ %@", tokenType, client.accessToken.accessToken]; } + else if (isRefreshToken) + { + NSString* nonEncodedIdAndSecret = + [NSString stringWithFormat: + @"%@:%@", + requestParameters[@"client_id"], + requestParameters[@"client_secret"] ]; + + + NSData* nonEncodedIdAndSecretBinary = [nonEncodedIdAndSecret dataUsingEncoding: NSUTF8StringEncoding]; + + NSString* base64IdAndSecret = [nonEncodedIdAndSecretBinary base64EncodedStringWithOptions: (NSDataBase64EncodingOptions)0]; + oauthAuthorizationHeader = [NSString stringWithFormat: @"Basic %@", base64IdAndSecret]; + } NSMutableURLRequest *startRequest = [request mutableCopy]; [self applyParameters:requestParameters onRequest:startRequest];