- try to resolve double rerializer for oauth and normal calls

This commit is contained in:
Giuseppe Nucifora 2016-03-02 18:47:52 +01:00
parent f0771dc889
commit 8d2d6cc5c7
4 changed files with 62 additions and 27 deletions

View File

@ -8,7 +8,7 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "PNObject" 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" s.summary = "PNObject is a simple replica of the more complex ParseObject"
# This description is used to generate tags and improve search results. # This description is used to generate tags and improve search results.

View File

@ -32,7 +32,6 @@
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure { failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
if ([[PNObjectConfig sharedInstance] currentOauthCredential] && ![[[PNObjectConfig sharedInstance] currentOauthCredential] isExpired]) { 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) { [[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",endPoint] parameters:parameters progress:downloadProgress success:^(NSURLSessionDataTask *task, id responseObject) {
if (success) { if (success) {
@ -65,7 +64,6 @@
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure { failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
if ([[PNObjectConfig sharedInstance] currentOauthCredential] && ![[[PNObjectConfig sharedInstance] currentOauthCredential] isExpired]) { 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) { [[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",endPoint] parameters:parameters progress:uploadProgress success:^(NSURLSessionDataTask *task, id responseObject) {
if (success) { if (success) {
@ -100,7 +98,6 @@
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure { failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
if ([[PNObjectConfig sharedInstance] currentOauthCredential] && ![[[PNObjectConfig sharedInstance] currentOauthCredential] isExpired]) { if ([[PNObjectConfig sharedInstance] currentOauthCredential] && ![[[PNObjectConfig sharedInstance] currentOauthCredential] isExpired]) {
//[[[PNObjectConfig sharedInstance] manager] setRequestSerializer:[[PNObjectConfig sharedInstance] jsonSerializer]];
[[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",endPoint] [[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] baseUrl] stringByAppendingFormat:@"%@",endPoint]
parameters:parameters parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {

View File

@ -188,7 +188,7 @@ extern NSString* _Nonnull const Client_Secret;
/** /**
* <#Description#> * <#Description#>
*/ */
@property (nonatomic, strong, nonnull) AFOAuth2Manager *manager; @property (nonatomic, strong, readonly, nonnull) AFHTTPSessionManager *manager;
/** /**
* <#Description#> * <#Description#>

View File

@ -46,13 +46,15 @@ NSString* const Client_Secret = @"client_secret";
@property (nonatomic, strong) NSString *currentEndPointBaseUrl; @property (nonatomic, strong) NSString *currentEndPointBaseUrl;
@property (nonatomic, strong) NSString *currentOAuthClientID; @property (nonatomic, strong) NSString *currentOAuthClientID;
@property (nonatomic, strong) NSString *currentOAuthClientSecret; @property (nonatomic, strong) NSString *currentOAuthClientSecret;
//@property (nonatomic, strong) NSMutableDictionary *environments; @property (nonatomic, strong) AFOAuth2Manager *authManager;
@property (nonatomic) Class userSubClass; @property (nonatomic) Class userSubClass;
@end @end
@implementation PNObjectConfig @implementation PNObjectConfig
@synthesize manager = _manager;
static PNObjectConfig *SINGLETON = nil; static PNObjectConfig *SINGLETON = nil;
@ -185,7 +187,7 @@ static bool isFirstAccess = YES;
_currentOAuthClientSecret = [[_configuration objectForKey:_currentEnv] objectForKey:Client_Secret]; _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"); NSAssert(_currentEndPointBaseUrl,@"Selected environment generate error. Please check configuration");
@ -197,26 +199,16 @@ static bool isFirstAccess = YES;
return _currentEndPointBaseUrl; return _currentEndPointBaseUrl;
} }
- (AFOAuth2Manager *) manager { - (AFHTTPSessionManager *) manager {
BOOL canTryRefreh = NO; BOOL canTryRefreh = NO;
if (!_manager) { if (!_manager) {
_manager = [AFOAuth2Manager manager]; _manager = [AFHTTPSessionManager manager];
} }
//_currentOauthCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceCredentialIdentifier]; //_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]) { for (NSString *key in [_headerFields allKeys]) {
@ -238,10 +230,56 @@ static bool isFirstAccess = YES;
} }
_manager.responseSerializer = [AFJSONResponseSerializerWithData serializer]; _manager.responseSerializer = [AFJSONResponseSerializerWithData serializer];
_manager.requestSerializer = _jsonSerializer;
return _manager; 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 { - (BOOL) resetToken {
if (_currentOauthCredential) { if (_currentOauthCredential) {
return [AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceCredentialIdentifier]; return [AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceCredentialIdentifier];
@ -269,14 +307,14 @@ static bool isFirstAccess = YES;
failure:(nullable void (^)(NSError * _Nonnull error))failure { failure:(nullable void (^)(NSError * _Nonnull error))failure {
if([SINGLETON.userSubClass currentUser] && [[SINGLETON.userSubClass currentUser] hasValidEmailAndPasswordData]) { if([SINGLETON.userSubClass currentUser] && [[SINGLETON.userSubClass currentUser] hasValidEmailAndPasswordData]) {
[_manager setRequestSerializer:_httpSerializer]; [_authManager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:[[SINGLETON.userSubClass currentUser] email] password:[[(PNUser*)[SINGLETON.userSubClass currentUser] password] password] scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
[_manager 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; _currentOauthCredential = credential;
[AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier]; [AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier];
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_authManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
if (success) { if (success) {
@ -318,14 +356,14 @@ static bool isFirstAccess = YES;
return; return;
} }
} }
[_manager setRequestSerializer:_httpSerializer]; [_authManager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:email password:password scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
[_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] username:email password:password scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
_currentOauthCredential = credential; _currentOauthCredential = credential;
[AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier]; [AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier];
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_authManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
if (success) { if (success) {
@ -348,14 +386,14 @@ static bool isFirstAccess = YES;
- (void) refreshTokenForClientCredentialWithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success - (void) refreshTokenForClientCredentialWithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure { failure:(nullable void (^)(NSError * _Nonnull error))failure {
[_manager setRequestSerializer:_httpSerializer]; [_authManager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
[_manager authenticateUsingOAuthWithURLString:[_currentEndPointBaseUrl stringByAppendingString:@"oauth-token"] scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
_currentOauthCredential = credential; _currentOauthCredential = credential;
[AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier]; [AFOAuthCredential storeCredential:_currentOauthCredential withIdentifier:PNObjectServiceCredentialIdentifier];
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_authManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential]; [_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthCredential];
if (success) { if (success) {