From ce5073fcd84cb3572f5350f171ecad40227d0a92 Mon Sep 17 00:00:00 2001 From: Giuseppe Nucifora Date: Wed, 28 Mar 2018 11:53:10 +0200 Subject: [PATCH] - Add refreshtoken enable/disable for client credential and user credential --- Example/PNObject/PNObjAppDelegate.m | 4 ++-- Example/Podfile.lock | 4 ++-- .../Pods/Local Podspecs/PNObject.podspec.json | 4 ++-- Example/Pods/Manifest.lock | 4 ++-- PNObject.podspec | 2 +- PNObject/Classes/PNObjectConfig.h | 2 +- PNObject/Classes/PNObjectConfig.m | 23 +++++++++++++------ 7 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Example/PNObject/PNObjAppDelegate.m b/Example/PNObject/PNObjAppDelegate.m index 70b6084..973940e 100644 --- a/Example/PNObject/PNObjAppDelegate.m +++ b/Example/PNObject/PNObjAppDelegate.m @@ -38,8 +38,8 @@ } andUserSubclass:[PNUser class]]; /** Can user special char %@ to autoset EndpointPath to Oauth endpointPath */ - [[PNObjectConfig sharedInstance] setClientID:@"******" clientSecret:@"******" oAuthEndpointAction:@"%@oauth-token" oauthMode:OAuthModeClientCredential forEnv:EnvironmentStage]; - [[PNObjectConfig sharedInstance] setClientID:@"******" clientSecret:@"******" oAuthEndpointAction:@"%@oauth-token" oauthMode:OAuthModePassword forEnv:EnvironmentStage]; + [[PNObjectConfig sharedInstance] setClientID:@"******" clientSecret:@"******" oAuthEndpointAction:@"%@oauth-token" oauthMode:OAuthModeClientCredential refreshTokenEnabled:NO forEnv:EnvironmentStage]; + [[PNObjectConfig sharedInstance] setClientID:@"******" clientSecret:@"******" oAuthEndpointAction:@"%@oauth-token" oauthMode:OAuthModePassword refreshTokenEnabled:NO forEnv:EnvironmentStage]; [[PNObjectConfig sharedInstance] setOauthUserName:@"admin" oauthPassword:@"admin" forEnv:EnvironmentStage]; diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 1af8856..3e32ed4 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -38,7 +38,7 @@ PODS: - NSString-Helper (1.0.6) - nv-ios-http-status (0.0.1) - PEAR-FileManager-iOS (1.3.1) - - PNObject (2.3): + - PNObject (2.4): - AFNetworking - CodFis-Helper - DDDKeychainWrapper @@ -85,7 +85,7 @@ SPEC CHECKSUMS: NSString-Helper: 389265918b2a4c5b0d09f0e03c607e8e5e254dd7 nv-ios-http-status: b6c2b5fc8656cc19e0d3000dadce2080b99d0e2f PEAR-FileManager-iOS: 3bc403f68a53483f5629aa822f4649e40275c4d3 - PNObject: 73ee7d7b28c1c1bb1f21067770c1459b9314fcd1 + PNObject: cc6af200d331801ff19825af6e381b1809f79f16 PureLayout: 4d550abe49a94f24c2808b9b95db9131685fe4cd RZDataBinding: 6981e90ddaae2f5e02028323b1043f8c31013109 Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66 diff --git a/Example/Pods/Local Podspecs/PNObject.podspec.json b/Example/Pods/Local Podspecs/PNObject.podspec.json index fd25d41..5128f69 100644 --- a/Example/Pods/Local Podspecs/PNObject.podspec.json +++ b/Example/Pods/Local Podspecs/PNObject.podspec.json @@ -1,6 +1,6 @@ { "name": "PNObject", - "version": "2.3", + "version": "2.4", "summary": "PNObject is a simple replica of the more complex ParseObject", "homepage": "https://github.com/giuseppenucifora/PNObject", "license": { @@ -12,7 +12,7 @@ }, "source": { "git": "https://github.com/giuseppenucifora/PNObject.git", - "tag": "2.3" + "tag": "2.4" }, "platforms": { "ios": "8.0" diff --git a/Example/Pods/Manifest.lock b/Example/Pods/Manifest.lock index 1af8856..3e32ed4 100644 --- a/Example/Pods/Manifest.lock +++ b/Example/Pods/Manifest.lock @@ -38,7 +38,7 @@ PODS: - NSString-Helper (1.0.6) - nv-ios-http-status (0.0.1) - PEAR-FileManager-iOS (1.3.1) - - PNObject (2.3): + - PNObject (2.4): - AFNetworking - CodFis-Helper - DDDKeychainWrapper @@ -85,7 +85,7 @@ SPEC CHECKSUMS: NSString-Helper: 389265918b2a4c5b0d09f0e03c607e8e5e254dd7 nv-ios-http-status: b6c2b5fc8656cc19e0d3000dadce2080b99d0e2f PEAR-FileManager-iOS: 3bc403f68a53483f5629aa822f4649e40275c4d3 - PNObject: 73ee7d7b28c1c1bb1f21067770c1459b9314fcd1 + PNObject: cc6af200d331801ff19825af6e381b1809f79f16 PureLayout: 4d550abe49a94f24c2808b9b95db9131685fe4cd RZDataBinding: 6981e90ddaae2f5e02028323b1043f8c31013109 Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66 diff --git a/PNObject.podspec b/PNObject.podspec index 725f0a9..1034a02 100644 --- a/PNObject.podspec +++ b/PNObject.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'PNObject' -s.version = '2.3' +s.version = '2.4' s.summary = 'PNObject is a simple replica of the more complex ParseObject' diff --git a/PNObject/Classes/PNObjectConfig.h b/PNObject/Classes/PNObjectConfig.h index 810507f..27fab8f 100644 --- a/PNObject/Classes/PNObjectConfig.h +++ b/PNObject/Classes/PNObjectConfig.h @@ -132,7 +132,7 @@ typedef NS_ENUM(NSInteger, OAuthMode) { * @param environment environment * */ -- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction oauthMode:(OAuthMode) oauthMode forEnv:(NSString* _Nonnull) environment; +- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction oauthMode:(OAuthMode) oauthMode refreshTokenEnabled:(BOOL) refreshTokenEnabled forEnv:(NSString * _Nonnull) environment; - (void) setOauthUserName:(NSString * _Nonnull)oauthUserName oauthPassword:(NSString* _Nonnull) oauthPassword forEnv:(NSString * _Nonnull) environment; diff --git a/PNObject/Classes/PNObjectConfig.m b/PNObject/Classes/PNObjectConfig.m index 1087844..1d16330 100644 --- a/PNObject/Classes/PNObjectConfig.m +++ b/PNObject/Classes/PNObjectConfig.m @@ -52,10 +52,12 @@ NSString* const EndpointPath = @"endpoint_path"; NSString* const Client_ID = @"oauth_user_credential_client_id"; NSString* const Client_Secret = @"oauth_user_credential_client_secret"; NSString* const Client_EndpointAction = @"oauth_user_credential_endpoint_action"; +NSString* const Client_Refresh_Token_Enabled = @"oauth_user_credential_refresh_token_enabled"; NSString* const Client_Credential_ID = @"oauth_client_credential_client_id"; NSString* const Client_Credential_Secret = @"oauth_client_credential_client_secret"; -NSString* const Client_CredentialEndpointAction = @"oauth_client_credential_endpoint_action"; +NSString* const Client_Credential_EndpointAction = @"oauth_client_credential_endpoint_action"; +NSString* const Client_Credential_Refresh_Token_Enabled = @"oauth_client_credential_refresh_token_enabled"; NSString* const Client_Username = @"client_username"; NSString* const Client_Password = @"client_password"; @@ -74,13 +76,14 @@ NSString* const Client_Password = @"client_password"; @property (nonatomic, strong) NSString *currentClientCredenzialEndPointUrl; @property (nonatomic, strong) NSString *currentClientCredenzialClientID; @property (nonatomic, strong) NSString *currentClientCredenzialClientSecret; +@property (nonatomic) BOOL currentClientCredenzialRefreshTokenEnabled; /* User credential configuration */ @property (nonatomic, strong) NSString *currentUserCredenzialEndPointPath; @property (nonatomic, strong) NSString *currentUserCredenzialEndPointUrl; @property (nonatomic, strong) NSString *currentUserCredenzialClientID; @property (nonatomic, strong) NSString *currentUserCredenzialClientSecret; - +@property (nonatomic) BOOL currentUserCredenzialRefreshTokenEnabled; @property (nonatomic, strong) NSString *currentOAuthUserName; @property (nonatomic, strong) NSString *currentOAuthPassword; @@ -253,7 +256,8 @@ static bool isFirstAccess = YES; _currentClientCredenzialClientID = [currentEnvConfig objectForKey:Client_Credential_ID]; _currentClientCredenzialClientSecret = [currentEnvConfig objectForKey:Client_Credential_Secret]; - _currentClientCredenzialEndPointPath = ([currentEnvConfig objectForKey:Client_CredentialEndpointAction] ? [currentEnvConfig objectForKey:Client_CredentialEndpointAction] : @""); + _currentClientCredenzialEndPointPath = ([currentEnvConfig objectForKey:Client_Credential_EndpointAction] ? [currentEnvConfig objectForKey:Client_Credential_EndpointAction] : @""); + _currentClientCredenzialRefreshTokenEnabled = ([currentEnvConfig objectForKey:Client_Credential_Refresh_Token_Enabled] ? [[currentEnvConfig objectForKey:Client_Credential_Refresh_Token_Enabled] boolValue] : YES); if([_currentClientCredenzialEndPointPath containsString:@"%@"]){ _currentClientCredenzialEndPointPath = [NSString stringWithFormat:_currentClientCredenzialEndPointPath,_currentEndPointPath]; @@ -266,6 +270,7 @@ static bool isFirstAccess = YES; _currentUserCredenzialClientSecret = _currentClientCredenzialClientSecret; _currentUserCredenzialEndPointPath = _currentClientCredenzialEndPointPath; _currentUserCredenzialEndPointUrl = _currentClientCredenzialEndPointUrl; + _currentUserCredenzialRefreshTokenEnabled = _currentClientCredenzialRefreshTokenEnabled; } else { _currentUserCredenzialClientID = [currentEnvConfig objectForKey:Client_ID]; @@ -277,6 +282,8 @@ static bool isFirstAccess = YES; _currentUserCredenzialEndPointPath = [NSString stringWithFormat:_currentUserCredenzialEndPointPath,_currentEndPointPath]; } _currentUserCredenzialEndPointUrl = [_currentBaseUrl stringByAppendingString:_currentUserCredenzialEndPointPath]; + + _currentUserCredenzialRefreshTokenEnabled = [currentEnvConfig objectForKey:Client_Refresh_Token_Enabled]; } @@ -495,7 +502,7 @@ static bool isFirstAccess = YES; case OAuthModeClientCredential: { AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceClientCredentialIdentifier]; - if (credential && ![credential isExpired]) { + if (credential && ![credential isExpired] && _currentClientCredenzialRefreshTokenEnabled) { [_clientCredentialAuthManager authenticateUsingOAuthWithURLString:_currentClientCredenzialEndPointUrl refreshToken:[credential refreshToken] success:^(AFOAuthCredential * _Nonnull credential) { @@ -554,7 +561,7 @@ static bool isFirstAccess = YES; AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceClientCredentialIdentifier]; - if (credential && ![credential isExpired]) { + if (credential && ![credential isExpired] && _currentUserCredenzialRefreshTokenEnabled) { [_userCredentialAuthManager authenticateUsingOAuthWithURLString:_currentClientCredenzialEndPointUrl refreshToken:[credential refreshToken] success:^(AFOAuthCredential * _Nonnull credential) { @@ -776,7 +783,7 @@ static bool isFirstAccess = YES; } } -- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction oauthMode:(OAuthMode) oauthMode forEnv:(NSString *) environment { +- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction oauthMode:(OAuthMode) oauthMode refreshTokenEnabled:(BOOL) refreshTokenEnabled forEnv:(NSString * _Nonnull) environment { if ([_configuration objectForKey:environment]) { @@ -785,12 +792,14 @@ static bool isFirstAccess = YES; case OAuthModeClientCredential: [currentConfigurationDict setObject:clientID forKey:Client_Credential_ID]; [currentConfigurationDict setObject:clientSecret forKey:Client_Credential_Secret]; - [currentConfigurationDict setObject:oAuthEndpointAction forKey:Client_CredentialEndpointAction]; + [currentConfigurationDict setObject:oAuthEndpointAction forKey:Client_Credential_EndpointAction]; + [currentConfigurationDict setObject:[NSNumber numberWithBool:refreshTokenEnabled] forKey:Client_Credential_Refresh_Token_Enabled]; break; case OAuthModePassword:{ [currentConfigurationDict setObject:clientID forKey:Client_ID]; [currentConfigurationDict setObject:clientSecret forKey:Client_Secret]; [currentConfigurationDict setObject:oAuthEndpointAction forKey:Client_EndpointAction]; + [currentConfigurationDict setObject:[NSNumber numberWithBool:refreshTokenEnabled] forKey:Client_Refresh_Token_Enabled]; } break; default: