From 8d2d6cc5c79af48a924d476573fddce7ca01355b Mon Sep 17 00:00:00 2001 From: Giuseppe Nucifora Date: Wed, 2 Mar 2016 18:47:52 +0100 Subject: [PATCH] - try to resolve double rerializer for oauth and normal calls --- PNObject.podspec | 2 +- Pod/Classes/PNObject+PNObjectConnection.m | 3 - Pod/Classes/PNObjectConfig.h | 2 +- Pod/Classes/PNObjectConfig.m | 82 +++++++++++++++++------ 4 files changed, 62 insertions(+), 27 deletions(-) diff --git a/PNObject.podspec b/PNObject.podspec index 0afa461..34ca1d4 100644 --- a/PNObject.podspec +++ b/PNObject.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = "PNObject" -s.version = "0.3.14" +s.version = "0.3.15" s.summary = "PNObject is a simple replica of the more complex ParseObject" # This description is used to generate tags and improve search results. diff --git a/Pod/Classes/PNObject+PNObjectConnection.m b/Pod/Classes/PNObject+PNObjectConnection.m index aa1a7f6..a204496 100644 --- a/Pod/Classes/PNObject+PNObjectConnection.m +++ b/Pod/Classes/PNObject+PNObjectConnection.m @@ -32,7 +32,6 @@ failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure { if ([[PNObjectConfig sharedInstance] currentOauthCredential] && ![[[PNObjectConfig sharedInstance] currentOauthCredential] isExpired]) { - //[[[PNObjectConfig sharedInstance] manager] setRequestSerializer:[[PNObjectConfig sharedInstance] jsonSerializer]]; [[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",endPoint] parameters:parameters progress:downloadProgress success:^(NSURLSessionDataTask *task, id responseObject) { if (success) { @@ -65,7 +64,6 @@ failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure { if ([[PNObjectConfig sharedInstance] currentOauthCredential] && ![[[PNObjectConfig sharedInstance] currentOauthCredential] isExpired]) { - //[[[PNObjectConfig sharedInstance] manager] setRequestSerializer:[[PNObjectConfig sharedInstance] jsonSerializer]]; [[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",endPoint] parameters:parameters progress:uploadProgress success:^(NSURLSessionDataTask *task, id responseObject) { if (success) { @@ -100,7 +98,6 @@ failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure { if ([[PNObjectConfig sharedInstance] currentOauthCredential] && ![[[PNObjectConfig sharedInstance] currentOauthCredential] isExpired]) { - //[[[PNObjectConfig sharedInstance] manager] setRequestSerializer:[[PNObjectConfig sharedInstance] jsonSerializer]]; [[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",endPoint] parameters:parameters constructingBodyWithBlock:^(id _Nonnull formData) { diff --git a/Pod/Classes/PNObjectConfig.h b/Pod/Classes/PNObjectConfig.h index ee38882..9d1029c 100644 --- a/Pod/Classes/PNObjectConfig.h +++ b/Pod/Classes/PNObjectConfig.h @@ -188,7 +188,7 @@ extern NSString* _Nonnull const Client_Secret; /** * <#Description#> */ -@property (nonatomic, strong, nonnull) AFOAuth2Manager *manager; +@property (nonatomic, strong, readonly, nonnull) AFHTTPSessionManager *manager; /** * <#Description#> diff --git a/Pod/Classes/PNObjectConfig.m b/Pod/Classes/PNObjectConfig.m index 2a465d4..baffdc2 100644 --- a/Pod/Classes/PNObjectConfig.m +++ b/Pod/Classes/PNObjectConfig.m @@ -46,13 +46,15 @@ NSString* const Client_Secret = @"client_secret"; @property (nonatomic, strong) NSString *currentEndPointBaseUrl; @property (nonatomic, strong) NSString *currentOAuthClientID; @property (nonatomic, strong) NSString *currentOAuthClientSecret; -//@property (nonatomic, strong) NSMutableDictionary *environments; +@property (nonatomic, strong) AFOAuth2Manager *authManager; + @property (nonatomic) Class userSubClass; @end @implementation PNObjectConfig +@synthesize manager = _manager; static PNObjectConfig *SINGLETON = nil; @@ -185,7 +187,7 @@ static bool isFirstAccess = YES; _currentOAuthClientSecret = [[_configuration objectForKey:_currentEnv] objectForKey:Client_Secret]; } - NSLog(@"%@",[[_configuration objectForKey:_currentEnv] objectForKey:BaseUrl]); + NSLogDebug(@"%@",[[_configuration objectForKey:_currentEnv] objectForKey:BaseUrl]); NSAssert(_currentEndPointBaseUrl,@"Selected environment generate error. Please check configuration"); @@ -197,26 +199,16 @@ static bool isFirstAccess = YES; return _currentEndPointBaseUrl; } -- (AFOAuth2Manager *) manager { +- (AFHTTPSessionManager *) manager { BOOL canTryRefreh = NO; if (!_manager) { - _manager = [AFOAuth2Manager manager]; + _manager = [AFHTTPSessionManager manager]; } //_currentOauthCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceCredentialIdentifier]; - if (_oauthEnabled && _currentOAuthClientID && _currentOAuthClientSecret) { - - if (![_manager clientID]) { - _manager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentEndPointBaseUrl] clientID:_currentOAuthClientID secret:_currentOAuthClientSecret]; - } - - [_manager setUseHTTPBasicAuthentication:NO]; - - canTryRefreh = YES; - } for (NSString *key in [_headerFields allKeys]) { @@ -238,10 +230,56 @@ static bool isFirstAccess = YES; } _manager.responseSerializer = [AFJSONResponseSerializerWithData serializer]; + _manager.requestSerializer = _jsonSerializer; return _manager; } +- (AFOAuth2Manager *) authManager { + + BOOL canTryRefreh = NO; + + if (!_authManager) { + _authManager = [AFOAuth2Manager manager]; + } + + //_currentOauthCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceCredentialIdentifier]; + + if (_oauthEnabled && _currentOAuthClientID && _currentOAuthClientSecret) { + + if (![_authManager clientID]) { + _authManager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentEndPointBaseUrl] clientID:_currentOAuthClientID secret:_currentOAuthClientSecret]; + } + + [_authManager setUseHTTPBasicAuthentication:NO]; + + canTryRefreh = YES; + } + + for (NSString *key in [_headerFields allKeys]) { + + [_httpSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key]; + [_jsonSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key]; + } + + if (canTryRefreh) { + + if (_currentOauthCredential && ![_currentOauthCredential isExpired]) { + + [_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; + [_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; + [_authManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; + } + else { + [self refreshToken]; + } + } + + _authManager.responseSerializer = [AFJSONResponseSerializerWithData serializer]; + + return _authManager; +} + - (BOOL) resetToken { if (_currentOauthCredential) { return [AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceCredentialIdentifier]; @@ -269,14 +307,14 @@ static bool isFirstAccess = YES; failure:(nullable void (^)(NSError * _Nonnull error))failure { if([SINGLETON.userSubClass currentUser] && [[SINGLETON.userSubClass currentUser] hasValidEmailAndPasswordData]) { - [_manager setRequestSerializer:_httpSerializer]; - [_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:[[SINGLETON.userSubClass currentUser] email] password:[[(PNUser*)[SINGLETON.userSubClass currentUser] password] password] scope:nil success:^(AFOAuthCredential * _Nonnull credential) { + [_authManager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:[[SINGLETON.userSubClass currentUser] email] password:[[(PNUser*)[SINGLETON.userSubClass currentUser] password] password] scope:nil success:^(AFOAuthCredential * _Nonnull credential) { _currentOauthCredential = credential; [AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier]; [_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; + [_authManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; if (success) { @@ -318,15 +356,15 @@ static bool isFirstAccess = YES; return; } } - [_manager setRequestSerializer:_httpSerializer]; - [_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:email password:password scope:nil success:^(AFOAuthCredential * _Nonnull credential) { + [_authManager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:email password:password scope:nil success:^(AFOAuthCredential * _Nonnull credential) { _currentOauthCredential = credential; [AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier]; [_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; - [_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; + [_authManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; + [_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; if (success) { success(YES); @@ -348,15 +386,15 @@ static bool isFirstAccess = YES; - (void) refreshTokenForClientCredentialWithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success failure:(nullable void (^)(NSError * _Nonnull error))failure { - [_manager setRequestSerializer:_httpSerializer]; - [_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] scope:nil success:^(AFOAuthCredential * _Nonnull credential) { + [_authManager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] scope:nil success:^(AFOAuthCredential * _Nonnull credential) { _currentOauthCredential = credential; [AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier]; [_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; - [_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; + [_authManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; + [_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; if (success) { success(YES);