- expose oauthmode for all methods and fix oauthmode

This commit is contained in:
Giuseppe Nucifora 2018-03-26 23:31:40 +02:00
parent 6c8cf96e65
commit cd55374ea8
23 changed files with 1447 additions and 1393 deletions

View File

@ -39,7 +39,7 @@
/* Begin PBXFileReference section */
0ABF8340F735722CEE937FA3 /* libPods-PNObject_Example.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-PNObject_Example.a"; sourceTree = BUILT_PRODUCTS_DIR; };
116AF818F8117B91EF290C9A /* Pods-PNObject_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PNObject_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-PNObject_Example/Pods-PNObject_Example.release.xcconfig"; sourceTree = "<group>"; };
2661CEE1F223361CD1DACC25 /* PNObject.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = PNObject.podspec; path = ../PNObject.podspec; sourceTree = "<group>"; };
2661CEE1F223361CD1DACC25 /* PNObject.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = PNObject.podspec; path = ../PNObject.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
3E392C278361604C1602CAF0 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
6003F58A195388D20070C39A /* PNObject_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PNObject_Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
6003F58D195388D20070C39A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };

View File

@ -33,23 +33,27 @@
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
/**
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @{BaseUrl:@"http://pnobject.local/",EndpointPath:@"api/v1/"},
EnvironmentStage : @{BaseUrl:@"https://idd.ppreview.it/",EndpointPath:@"wp-json/v1/"},
EnvironmentStage : @{BaseUrl:@"https://pnobject.stage.it/",EndpointPath:@"api/v1/"},
EnvironmentProduction : @{BaseUrl:@"http://pnobject.prod.it/",EndpointPath:@"api/v1/"},
} userSubclass:[PNUser class] withOauthMode:OAuthModeClientCredential];
} andUserSubclass:[PNUser class]];
[[PNObjectConfig sharedInstance] setClientID:@"xVdEbNLLierj9CJoLNo5lsbg7VFs8UikqADbcwKA" clientSecret:@"2WWSJrDNbZhbUUCXIOTBiYIJv9muiRGK68f2B2Eb" oAuthEndpointAction:@"oauth/token" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setOauthClientID:@"tXYhKtcvfYdCM4tNor6WfbclEWYkoGWqBimUBzqZ" oauthClientSecret:@"3UMEQthBHp1oEo0pjFmgkifhig689ZL5L9DsSETd" oAuthEndpointAction:@"oauth/token" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"******" clientSecret:@"******" oAuthEndpointAction:@"api/v1/" oauthMode:OAuthModeClientCredential forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"******" clientSecret:@"******" oAuthEndpointAction:@"api/v1/" oauthMode:OAuthModePassword forEnv:EnvironmentStage];
*/
/** 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] setEnvironment:EnvironmentStage];
//[[PNObjectConfig sharedInstance] setHTTPHeaderValue:@"XMLHttpRequest" forKey:@"X-Request-With"];
NSLogDebug(@"%@",[[PNObjectConfig sharedInstance] baseUrl]);
NSLogDebug(@"%@",[[PNObjectConfig sharedInstance] endPointPath]);
NSLogDebug(@"%@",[[PNObjectConfig sharedInstance] endPointUrl]);
PNObjViewController *viewController = [[PNObjViewController alloc] init];
switch ([[UIDevice currentDevice] deviceFamily]) {

View File

@ -93,11 +93,15 @@
}
- (void) refreshTokenAction {
[[PNObjectConfig sharedInstance] refreshTokenForClientCredential];
[[PNObjectConfig sharedInstance] refreshTokenForClientCredentialWithBlockSuccess:^(BOOL refreshSuccess) {
} failure:^(NSError * _Nonnull error) {
}];
}
- (void) cancelTokenAction {
[[PNObjectConfig sharedInstance] resetToken];
[[PNObjectConfig sharedInstance] resetAllTokens];
}
- (void) apiCallAction {

View File

@ -38,7 +38,7 @@ PODS:
- NSString-Helper (1.0.6)
- nv-ios-http-status (0.0.1)
- PEAR-FileManager-iOS (1.3.1)
- PNObject (1.5):
- PNObject (2.0):
- AFNetworking
- CodFis-Helper
- DDDKeychainWrapper
@ -85,7 +85,7 @@ SPEC CHECKSUMS:
NSString-Helper: 389265918b2a4c5b0d09f0e03c607e8e5e254dd7
nv-ios-http-status: b6c2b5fc8656cc19e0d3000dadce2080b99d0e2f
PEAR-FileManager-iOS: 3bc403f68a53483f5629aa822f4649e40275c4d3
PNObject: 75d5a262457e538027c6f7b9cbd4f8ee5700f21f
PNObject: 1f79df2779559eaad82d9a345bc300eeb947a5d8
PureLayout: 4d550abe49a94f24c2808b9b95db9131685fe4cd
RZDataBinding: 6981e90ddaae2f5e02028323b1043f8c31013109
Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66

View File

@ -0,0 +1 @@
../../../../../PNObject/Classes/PNObject+PNObjectDELETEConnection.h

View File

@ -0,0 +1 @@
../../../../../PNObject/Classes/PNObject+PNObjectGETConnection.h

View File

@ -0,0 +1 @@
../../../../../PNObject/Classes/PNObject+PNObjectPOSTConnection.h

View File

@ -0,0 +1 @@
../../../../../PNObject/Classes/PNObject+PNObjectDELETEConnection.h

View File

@ -0,0 +1 @@
../../../../../PNObject/Classes/PNObject+PNObjectGETConnection.h

View File

@ -0,0 +1 @@
../../../../../PNObject/Classes/PNObject+PNObjectPOSTConnection.h

View File

@ -1,6 +1,6 @@
{
"name": "PNObject",
"version": "1.5",
"version": "2.0",
"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": "1.5"
"tag": "2.0"
},
"platforms": {
"ios": "8.0"

View File

@ -38,7 +38,7 @@ PODS:
- NSString-Helper (1.0.6)
- nv-ios-http-status (0.0.1)
- PEAR-FileManager-iOS (1.3.1)
- PNObject (1.5):
- PNObject (2.0):
- AFNetworking
- CodFis-Helper
- DDDKeychainWrapper
@ -85,7 +85,7 @@ SPEC CHECKSUMS:
NSString-Helper: 389265918b2a4c5b0d09f0e03c607e8e5e254dd7
nv-ios-http-status: b6c2b5fc8656cc19e0d3000dadce2080b99d0e2f
PEAR-FileManager-iOS: 3bc403f68a53483f5629aa822f4649e40275c4d3
PNObject: 75d5a262457e538027c6f7b9cbd4f8ee5700f21f
PNObject: 1f79df2779559eaad82d9a345bc300eeb947a5d8
PureLayout: 4d550abe49a94f24c2808b9b95db9131685fe4cd
RZDataBinding: 6981e90ddaae2f5e02028323b1043f8c31013109
Specta: 3e1bd89c3517421982dc4d1c992503e48bd5fe66

File diff suppressed because it is too large Load Diff

View File

@ -8,7 +8,7 @@
Pod::Spec.new do |s|
s.name = 'PNObject'
s.version = '1.5'
s.version = '2.0'
s.summary = 'PNObject is a simple replica of the more complex ParseObject'

View File

@ -25,14 +25,14 @@
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) DELETEWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) DELETEWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress

View File

@ -22,7 +22,7 @@
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self DELETEWithEndpointAction:endPoint authMode:OAuthModeClientCredential parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
return [self DELETEWithEndpointAction:endPoint oauthMode:OAuthModeNo parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
}
+ (void) DELETEWithEndpointAction:(NSString * _Nonnull) endPoint
@ -31,29 +31,28 @@
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self DELETEWithEndpointAction:endPoint authMode:OAuthModeClientCredential parameters:parameters retries:retries progress:uploadProgress success:success failure:failure];
return [self DELETEWithEndpointAction:endPoint oauthMode:OAuthModeNo parameters:parameters retries:retries progress:uploadProgress success:success failure:failure];
}
+ (void) DELETEWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self DELETEWithEndpointAction:endPoint authMode:authMode parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
return [self DELETEWithEndpointAction:endPoint oauthMode:oauthMode parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
}
+ (void) DELETEWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
if([[PNObjectConfig sharedInstance] setCredentialTokenForOauthMode:oauthMode]){
if ([[PNObjectConfig sharedInstance] currentOauthUserCredential] && ![[[PNObjectConfig sharedInstance] currentOauthUserCredential] isExpired]) {
[[[PNObjectConfig sharedInstance] manager] DELETE:[[[PNObjectConfig sharedInstance] endPointUrl] stringByAppendingFormat:@"%@",endPoint]
parameters:parameters
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
@ -65,7 +64,7 @@
[[PNObjectConfig sharedInstance] refreshTokenWithBlockSuccess:^(BOOL refreshSuccess) {
[self DELETEWithEndpointAction:endPoint
authMode:authMode
oauthMode:oauthMode
parameters:parameters
retries:retries-1
progress:uploadProgress
@ -87,15 +86,16 @@
}
else {
[[PNObjectConfig sharedInstance] refreshTokenWithBlockSuccess:^(BOOL refreshSuccess) {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode WithBlockSuccess:^(BOOL refreshSuccess) {
[self DELETEWithEndpointAction:endPoint
authMode:authMode
oauthMode:oauthMode
parameters:parameters
retries:retries-1
progress:uploadProgress
success:success
failure:failure];
} failure:^(NSError * _Nonnull error) {
if (failure) {
@ -105,18 +105,4 @@
}
}
+ (BOOL) checkCredential {
switch ([[PNObjectConfig sharedInstance] oauthMode]) {
case OAuthModeClientCredential:
return NO;
break;
case OAuthModePassword:
default:
return NO;
break;
}
return YES;
}
@end

View File

@ -33,20 +33,20 @@
+ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
progress:(nullable void (^)(NSProgress * _Nullable downloadProgress)) downloadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nullable downloadProgress)) downloadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nullable downloadProgress)) downloadProgress

View File

@ -32,7 +32,7 @@
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self GETWithEndpointAction:endPoint authMode:OAuthModeClientCredential parameters:parameters retries:MAX_RETRIES progress:downloadProgress success:success failure:failure];
return [self GETWithEndpointAction:endPoint oauthMode:OAuthModeClientCredential parameters:parameters retries:MAX_RETRIES progress:downloadProgress success:success failure:failure];
}
+ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint
@ -42,35 +42,35 @@
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self GETWithEndpointAction:endPoint authMode:OAuthModeClientCredential parameters:parameters retries:retries progress:downloadProgress success:success failure:failure];
return [self GETWithEndpointAction:endPoint oauthMode:OAuthModeClientCredential parameters:parameters retries:retries progress:downloadProgress success:success failure:failure];
}
+ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
progress:(nullable void (^)(NSProgress * _Nullable downloadProgress)) downloadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self GETWithEndpointAction:endPoint authMode:authMode parameters:nil retries:MAX_RETRIES progress:downloadProgress success:success failure:failure];
return [self GETWithEndpointAction:endPoint oauthMode:oauthMode parameters:nil retries:MAX_RETRIES progress:downloadProgress success:success failure:failure];
}
+ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nullable downloadProgress)) downloadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self GETWithEndpointAction:endPoint authMode:authMode parameters:parameters retries:MAX_RETRIES progress:downloadProgress success:success failure:failure];
return [self GETWithEndpointAction:endPoint oauthMode:oauthMode parameters:parameters retries:MAX_RETRIES progress:downloadProgress success:success failure:failure];
}
+ (void) GETWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nullable downloadProgress)) downloadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
if ([[PNObjectConfig sharedInstance] currentOauthClientCredential] && ![[[PNObjectConfig sharedInstance] currentOauthClientCredential] isExpired]) {
if([[PNObjectConfig sharedInstance] setCredentialTokenForOauthMode:oauthMode]){
[[[PNObjectConfig sharedInstance] manager] GET:[[[PNObjectConfig sharedInstance] endPointUrl] stringByAppendingFormat:@"%@",endPoint] parameters:parameters progress:downloadProgress success:^(NSURLSessionDataTask *task, id responseObject) {
if (success) {
@ -82,7 +82,7 @@
[[PNObjectConfig sharedInstance] refreshTokenWithBlockSuccess:^(BOOL refreshSuccess) {
return [self GETWithEndpointAction:endPoint
authMode:authMode
oauthMode:oauthMode
parameters:parameters
retries:retries-1
progress:downloadProgress
@ -102,10 +102,10 @@
}];
}
else {
[[PNObjectConfig sharedInstance] refreshTokenWithBlockSuccess:^(BOOL refreshSuccess) {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode WithBlockSuccess:^(BOOL refreshSuccess) {
return [self GETWithEndpointAction:endPoint
authMode:authMode
oauthMode:oauthMode
parameters:parameters
retries:retries-1
progress:downloadProgress

View File

@ -11,9 +11,7 @@
@interface PNObject (PNObjectPOSTConnection)
#pragma mark - GET
#pragma mark - POST
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
parameters:(NSDictionary * _Nullable) parameters
@ -29,37 +27,24 @@
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nullable uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
#pragma mark - FORM DATA
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
@ -67,7 +52,23 @@
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
oauthMode:(OAuthMode) oauthMode
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure;
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
oauthMode:(OAuthMode) oauthMode
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries

View File

@ -23,11 +23,30 @@
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self POSTWithEndpointAction:endPoint authMode:OAuthModeClientCredential parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
return [self POSTWithEndpointAction:endPoint oauthMode:OAuthModeClientCredential parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
}
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self POSTWithEndpointAction:endPoint oauthMode:OAuthModeClientCredential parameters:parameters retries:retries progress:uploadProgress success:success failure:failure];
}
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nullable uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self POSTWithEndpointAction:endPoint oauthMode:oauthMode parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
}
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
oauthMode:(OAuthMode) oauthMode
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
@ -74,17 +93,18 @@
}
}
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
authMode:(OAuthMode) authMode
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self POSTWithEndpointAction:endPoint authMode:OAuthModeClientCredential formData:postFormData parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
return [self POSTWithEndpointAction:endPoint oauthMode:OAuthModeClientCredential formData:postFormData parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
}
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
@ -92,8 +112,30 @@
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self POSTWithEndpointAction:endPoint oauthMode:OAuthModeClientCredential formData:postFormData parameters:parameters retries:retries progress:uploadProgress success:success failure:failure];
}
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
oauthMode:(OAuthMode) oauthMode
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
return [self POSTWithEndpointAction:endPoint oauthMode:oauthMode formData:postFormData parameters:parameters retries:MAX_RETRIES progress:uploadProgress success:success failure:failure];
}
+ (void) POSTWithEndpointAction:(NSString * _Nonnull) endPoint
oauthMode:(OAuthMode) oauthMode
formData:(NSArray * _Nullable) postFormData
parameters:(NSDictionary * _Nullable) parameters
retries:(NSInteger) retries
progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
success:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error))failure {
if ([[PNObjectConfig sharedInstance] currentOauthUserCredential] && ![[[PNObjectConfig sharedInstance] currentOauthUserCredential] isExpired]) {
if([[PNObjectConfig sharedInstance] setCredentialTokenForOauthMode:oauthMode]){
[[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] endPointUrl] stringByAppendingFormat:@"%@",endPoint]
parameters:parameters
constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
@ -130,9 +172,9 @@
}];
}
else {
[[PNObjectConfig sharedInstance] refreshTokenWithBlockSuccess:^(BOOL refreshSuccess) {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode WithBlockSuccess:^(BOOL refreshSuccess) {
return [self POSTWithEndpointAction:endPoint formData:postFormData parameters:parameters retries:retries-1 progress:uploadProgress success:success failure:failure];
return [self POSTWithEndpointAction:endPoint oauthMode:oauthMode formData:postFormData parameters:parameters retries:retries-1 progress:uploadProgress success:success failure:failure];
} failure:^(NSError * _Nonnull error) {
if (failure) {

View File

@ -39,8 +39,6 @@ extern NSString* _Nonnull const PNObjectLocalNotificationPNInstallationUserDelet
extern NSString* _Nonnull const BaseUrl;
extern NSString* _Nonnull const EndpointPath;
extern NSString* _Nonnull const OAuthEndpointPath;
extern NSString* _Nonnull const OAuthEndpointAction;
extern NSString* _Nonnull const EnvironmentProduction;
extern NSString* _Nonnull const EnvironmentStage;
@ -49,8 +47,11 @@ extern NSString* _Nonnull const EnvironmentDevelopment;
extern NSString* _Nonnull const Client_ID;
extern NSString* _Nonnull const Client_Secret;
extern NSString* _Nonnull const OAuthClient_ID;
extern NSString* _Nonnull const OAuthClient_Secret;
extern NSString* _Nonnull const Client_Credential_ID;
extern NSString* _Nonnull const Client_Credential_Secret;
extern NSString* _Nonnull const Client_Username;
extern NSString* _Nonnull const Client_Password;
typedef NS_ENUM(NSInteger, OAuthMode) {
OAuthModeNo = 0,
@ -95,36 +96,6 @@ typedef NS_ENUM(NSInteger, OAuthMode) {
*/
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary * _Nonnull) endpointUrlsForEnvironments andUserSubclass:(Class _Nonnull) userSubClass;
/**
*
*
* @param endpointUrlsForEnvironments
* For example,
* @{ PNObjectConfigDevelopment : @"https://development.it/api/v1",
* PNObjectConfigEnvStage : @"https://stage.it/api/v1",
* PNObjectConfigEnvProduction : @"https://production.it/api/v1"
* }
* @param oauthMode <#oauthEnabled description#>
*
* @return singleton
*/
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary * _Nonnull) endpointUrlsForEnvironments withOauthMode:(OAuthMode) oauthMode;
/**
* <#Description#>
*
* @param endpointUrlsForEnvironments
* For example,
* @{ PNObjectConfigDevelopment : @"https://development.it/api/v1",
* PNObjectConfigEnvStage : @"https://stage.it/api/v1",
* PNObjectConfigEnvProduction : @"https://production.it/api/v1"
* }
* @param userSubClass <#userSubClass description#>
* @param oauthMode <#oauthEnabled description#>
*
* @return <#return value description#>
*/
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary * _Nonnull) endpointUrlsForEnvironments userSubclass:(Class _Nonnull) userSubClass withOauthMode:(OAuthMode) oauthMode;
/**
* <#Description#>
*
@ -151,12 +122,17 @@ typedef NS_ENUM(NSInteger, OAuthMode) {
- (void) removeHTTPHeaderValueForKey:(NSString * _Nonnull) key;
- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction forEnv:(NSString * _Nonnull) environment;
- (void) setOauthClientID:(NSString * _Nonnull) oauthClientID oauthClientSecret:(NSString* _Nonnull) oauthClientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction forEnv:(NSString *) environment;
- (void) setOauthUserName:(NSString * _Nonnull)oauthUserName oauthPassword:(NSString* _Nonnull) oauthPassword oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction forEnv:(NSString * _Nonnull) environment;
/**
*
*
* @param clientID Client ID for selected environment
* @param clientSecret Client Secret for selected environment
* @param oAuthEndpointAction endpoint action. You can pass specia string "%@" to autoset EndpointPath to Oauth endpointPath
* @param oauthMode OauthMode
* @param environment environment
*
*/
- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction oauthMode:(OAuthMode) oauthMode forEnv:(NSString* _Nonnull) environment;
/**
* <#Description#>
@ -232,22 +208,40 @@ typedef NS_ENUM(NSInteger, OAuthMode) {
withBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
/**
* <#Description#>
*/
- (void) refreshTokenForUserWithFacebookId:(NSString * _Nonnull) facebookId
facebookToken:(NSString * _Nonnull) facebookToken
withBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
/**
* <#Description#>
*/
- (BOOL) resetToken;
- (void) refreshTokenForOauthMode:(OAuthMode) oauthMode
WithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
/**
* <#Description#>
*/
- (BOOL) resetTokenForOauthMode:(OAuthMode) oauthMode;
/**
* <#Description#>
*/
- (void) resetAllTokens;
/**
* <#Description#>
*/
- (BOOL) setCredentialTokenForOauthMode:(OAuthMode) oauthMode;
///--------------------------------------
#pragma mark - PNObjectConfig Properties
///--------------------------------------
@property (nonatomic, readonly) OAuthMode oauthMode;
@property (nonatomic, readonly, nonnull) Class userSubClass;
/**
* <#Description#>
@ -257,12 +251,22 @@ typedef NS_ENUM(NSInteger, OAuthMode) {
/**
* <#Description#>
*/
@property (nonatomic, strong, readonly, nonnull) AFJSONRequestSerializer *jsonSerializer;
@property (nonatomic, strong, readonly, nonnull) AFJSONRequestSerializer *managerJsonRequestSerializer;
/**
* <#Description#>
*/
@property (nonatomic, strong, readonly, nonnull) AFHTTPRequestSerializer *httpSerializer;
@property (nonatomic, strong, readonly, nonnull) AFHTTPRequestSerializer *managerHttpRequestSerializer;
/**
* <#Description#>
*/
@property (nonatomic, strong, readonly, nonnull) AFJSONRequestSerializer *oauthJsonRequestSerializer;
/**
* <#Description#>
*/
@property (nonatomic, strong, readonly, nonnull) AFHTTPRequestSerializer *oauthHttpRequestSerializer;
/**
* <#Description#>

View File

@ -47,15 +47,14 @@ NSString* const EnvironmentDevelopment = @"PNObjectConfigDevelopment";
NSString* const BaseUrl = @"base_url";
NSString* const EndpointPath = @"endpoint_path";
NSString* const OAuthEndpointPath = @"oauth_endpoint_path";
NSString* const OAuthEndpointAction = @"oauth_endpoint_action";
NSString* const Client_ID = @"client_id";
NSString* const Client_Secret = @"client_secret";
NSString* const OAuthClient_ID = @"client_id";
NSString* const OAuthClient_Secret = @"client_secret";
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_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_Username = @"client_username";
NSString* const Client_Password = @"client_password";
@ -66,18 +65,21 @@ NSString* const Client_Password = @"client_password";
@property (nonatomic, strong) NSMutableDictionary *headerFields;
@property (nonatomic, strong) NSString *currentEnv;
@property (nonatomic, strong) NSString *currentBaseUrl;
@property (nonatomic, strong) NSString *currentEndPointPath;
@property (nonatomic, strong) NSString *currentEndPointUrl;
@property (nonatomic, strong) NSString *currentOAuthClientID;
@property (nonatomic, strong) NSString *currentOAuthClientSecret;
/* Client credential configurations */
@property (nonatomic, strong) NSString *currentClientCredenzialEndPointPath;
@property (nonatomic, strong) NSString *currentClientCredenzialEndPointUrl;
@property (nonatomic, strong) NSString *currentClientCredenzialClientID;
@property (nonatomic, strong) NSString *currentClientCredenzialClientSecret;
/* 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, strong) NSString *currentOauthEndPointPath;
@property (nonatomic, strong) NSString *currentOauthEndPointUrl;
@property (nonatomic, strong) NSString *currentOauthEndPointAction;
@property (nonatomic, strong) NSString *currentClientID;
@property (nonatomic, strong) NSString *currentClientSecret;
@property (nonatomic, strong) NSString *currentOAuthUserName;
@property (nonatomic, strong) NSString *currentOAuthPassword;
@ -106,24 +108,19 @@ static bool isFirstAccess = YES;
#pragma mark - Life Cycle
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary *) endpointUrlsForEnvironments {
return [self initSharedInstanceForEnvironments:endpointUrlsForEnvironments userSubclass:[PNUser class] withOauthMode:OAuthModeClientCredential];
return [self initSharedInstanceForEnvironments:endpointUrlsForEnvironments userSubclass:[PNUser class] withoauthMode:OAuthModeClientCredential];
}
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary *)endpointUrlsForEnvironments andUserSubclass:(Class)userSubClass {
return [self initSharedInstanceForEnvironments:endpointUrlsForEnvironments userSubclass:userSubClass withOauthMode:OAuthModeClientCredential];
return [self initSharedInstanceForEnvironments:endpointUrlsForEnvironments userSubclass:userSubClass withoauthMode:OAuthModeClientCredential];
}
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary *) endpointUrlsForEnvironments withOauthMode:(OAuthMode) oauthMode {
return [self initSharedInstanceForEnvironments:endpointUrlsForEnvironments userSubclass:[PNUser class] withOauthMode:oauthMode];
}
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary * _Nonnull) endpointUrlsForEnvironments userSubclass:(Class _Nonnull) userSubClass withOauthMode:(OAuthMode) oauthMode {
+ (instancetype _Nonnull) initSharedInstanceForEnvironments:(NSDictionary * _Nonnull) endpointUrlsForEnvironments userSubclass:(Class _Nonnull) userSubClass withoauthMode:(OAuthMode) oauthMode {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
isFirstAccess = NO;
SINGLETON_PNObjectConfig = [[super allocWithZone:NULL] initWithUserSubclass:userSubClass withOauthMode:oauthMode];
SINGLETON_PNObjectConfig = [[super allocWithZone:NULL] initWithUserSubclass:userSubClass];
if (SINGLETON_PNObjectConfig) {
@ -145,7 +142,6 @@ static bool isFirstAccess = YES;
}
}
NSAssert([SINGLETON_PNObjectConfig.configuration objectForKey:EnvironmentProduction], @"EnvironmentProduction must be valid endpoint url");
}
});
@ -177,7 +173,7 @@ static bool isFirstAccess = YES;
return [[PNObjectConfig alloc] init];
}
- (id) initWithUserSubclass:(Class _Nonnull) userSubClass withOauthMode:(OAuthMode) oauthMode
- (id) initWithUserSubclass:(Class _Nonnull) userSubClass
{
if(SINGLETON_PNObjectConfig){
return SINGLETON_PNObjectConfig;
@ -188,13 +184,15 @@ static bool isFirstAccess = YES;
self = [super init];
if (self) {
_oauthMode = oauthMode;
_userSubClass = userSubClass;
_configuration = [[NSMutableDictionary alloc] init];
_minPasswordLenght = minPassLenght;
_currentEnv = EnvironmentProduction;
_jsonSerializer = [AFJSONRequestSerializer serializer];
_httpSerializer = [AFHTTPRequestSerializer serializer];
_managerJsonRequestSerializer = [AFJSONRequestSerializer serializer];
_managerHttpRequestSerializer = [AFHTTPRequestSerializer serializer];
_oauthJsonRequestSerializer = [AFJSONRequestSerializer serializer];
_oauthHttpRequestSerializer = [AFHTTPRequestSerializer serializer];
_headerFields = [[NSMutableDictionary alloc] init];
@ -207,24 +205,16 @@ static bool isFirstAccess = YES;
}
switch (_oauthMode) {
case OAuthModePassword:
case OAuthModeClientCredential:
default: {
AFOAuthCredential *clientCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceClientCredentialIdentifier];
if (clientCredential) {
_currentOauthClientCredential = clientCredential;
}
AFOAuthCredential *userCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceUserCredentialIdentifier];
if (userCredential) {
_currentOauthUserCredential = userCredential;
}
}
break;
AFOAuthCredential *clientCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceClientCredentialIdentifier];
if (clientCredential) {
_currentOauthClientCredential = clientCredential;
}
AFOAuthCredential *userCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:PNObjectServiceUserCredentialIdentifier];
if (userCredential) {
_currentOauthUserCredential = userCredential;
}
}
return self;
@ -235,16 +225,18 @@ static bool isFirstAccess = YES;
_currentEnv = environment;
_currentBaseUrl = nil;
_currentOauthEndPointPath = nil;
_currentOauthEndPointUrl = nil;
_currentOAuthClientID = nil;
_currentOAuthClientSecret = nil;
_currentEndPointPath = nil;
_currentEndPointUrl = nil;
_currentClientID = nil;
_currentClientSecret = nil;
_currentClientCredenzialEndPointPath = nil;
_currentClientCredenzialEndPointUrl = nil;
_currentClientCredenzialClientID = nil;
_currentClientCredenzialClientSecret = nil;
_currentUserCredenzialEndPointPath = nil;
_currentUserCredenzialEndPointUrl = nil;
_currentUserCredenzialClientID = nil;
_currentUserCredenzialClientSecret = nil;
_currentOAuthUserName = nil;
_currentOAuthPassword = nil;
@ -255,31 +247,35 @@ static bool isFirstAccess = YES;
_currentBaseUrl = [currentEnvConfig objectForKey:BaseUrl];
_currentEndPointPath = ([currentEnvConfig objectForKey:EndpointPath] ? [currentEnvConfig objectForKey:EndpointPath] : @"");
_currentClientID = [currentEnvConfig objectForKey:Client_ID];
_currentClientSecret = [currentEnvConfig objectForKey:Client_Secret];
_currentEndPointUrl = [_currentBaseUrl stringByAppendingString:_currentEndPointPath];
_currentOauthEndPointAction = [currentEnvConfig objectForKey:OAuthEndpointAction];
_currentClientCredenzialClientID = [currentEnvConfig objectForKey:Client_Credential_ID];
_currentClientCredenzialClientSecret = [currentEnvConfig objectForKey:Client_Credential_Secret];
_currentClientCredenzialEndPointPath = ([currentEnvConfig objectForKey:Client_CredentialEndpointAction] ? [currentEnvConfig objectForKey:Client_CredentialEndpointAction] : @"");
if (![currentEnvConfig objectForKey:OAuthClient_ID] || ![currentEnvConfig objectForKey:OAuthClient_Secret]) {
_currentOAuthClientID = _currentClientID;
_currentOAuthClientSecret = _currentClientSecret;
if([_currentClientCredenzialEndPointPath containsString:@"%@"]){
_currentClientCredenzialEndPointPath = [NSString stringWithFormat:_currentClientCredenzialEndPointPath,_currentEndPointPath];
}
_currentClientCredenzialEndPointUrl = [_currentBaseUrl stringByAppendingString:_currentClientCredenzialEndPointPath];
if (![currentEnvConfig objectForKey:Client_ID] || ![currentEnvConfig objectForKey:Client_Secret]) {
_currentUserCredenzialClientID = _currentClientCredenzialClientID;
_currentUserCredenzialClientSecret = _currentClientCredenzialClientSecret;
_currentUserCredenzialEndPointPath = _currentClientCredenzialEndPointPath;
_currentUserCredenzialEndPointUrl = _currentClientCredenzialEndPointUrl;
}
else {
_currentOAuthClientID = [currentEnvConfig objectForKey:OAuthClient_ID];
_currentOAuthClientSecret = [currentEnvConfig objectForKey:OAuthClient_Secret];
}
if (![currentEnvConfig objectForKey:OAuthClient_ID] || ![currentEnvConfig objectForKey:OAuthClient_Secret]) {
_currentOauthEndPointPath = _currentEndPointPath;
_currentOauthEndPointUrl = _currentEndPointUrl;
}
else {
_currentOauthEndPointPath = ([currentEnvConfig objectForKey:OAuthEndpointPath] ? [currentEnvConfig objectForKey:OAuthEndpointPath] : @"");
_currentOauthEndPointUrl = [_currentBaseUrl stringByAppendingString:_currentOauthEndPointPath];
_currentUserCredenzialClientID = [currentEnvConfig objectForKey:Client_ID];
_currentUserCredenzialClientSecret = [currentEnvConfig objectForKey:Client_Secret];
_currentUserCredenzialEndPointPath = [currentEnvConfig objectForKey:Client_EndpointAction];
if([_currentUserCredenzialEndPointPath containsString:@"%@"]){
_currentUserCredenzialEndPointPath = [NSString stringWithFormat:_currentUserCredenzialEndPointPath,_currentEndPointPath];
}
_currentUserCredenzialEndPointUrl = [_currentBaseUrl stringByAppendingString:_currentUserCredenzialEndPointPath];
}
@ -291,15 +287,20 @@ static bool isFirstAccess = YES;
NSLogDebug(@"%@",[[_configuration objectForKey:_currentEnv] objectForKey:BaseUrl]);
NSAssert(_currentEndPointUrl,@"Selected environment generate error. Please check configuration");
NSAssert(_currentUserCredenzialEndPointUrl,@"Selected environment generate error. Please check configuration");
if (_currentOAuthClientID && _currentOAuthClientSecret) {
if (_currentClientCredenzialClientID && _currentClientCredenzialClientSecret) {
[self clientCredentialAuthManager];
[self userCredentialAuthManager];
[self manager];
}
if (_currentClientCredenzialClientID && _currentClientCredenzialClientSecret) {
[self userCredentialAuthManager];
}
[self manager];
}
- (NSString * _Nonnull) getEnvironment {
return _currentEnv;
}
@ -309,11 +310,11 @@ static bool isFirstAccess = YES;
}
- (NSString *) endPointPath {
return _currentEndPointPath;
return _currentUserCredenzialEndPointPath;
}
- (NSString *) endPointUrl {
return _currentEndPointUrl;
return _currentUserCredenzialEndPointUrl;
}
- (AFHTTPSessionManager *) manager {
@ -323,20 +324,20 @@ static bool isFirstAccess = YES;
for (NSString *key in [_headerFields allKeys]) {
[_httpSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_jsonSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_managerHttpRequestSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_managerJsonRequestSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
}
if (_currentOauthUserCredential && ![_currentOauthUserCredential isExpired] && ![[_manager requestSerializer] hasAuthorizationHeaderField]) {
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_managerHttpRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_managerJsonRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
}
else if (_currentOauthClientCredential && ![_currentOauthClientCredential isExpired] && ![[_manager requestSerializer] hasAuthorizationHeaderField]) {
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_managerHttpRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_managerJsonRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
}
else {
@ -344,34 +345,12 @@ static bool isFirstAccess = YES;
}
_manager.responseSerializer = [AFJSONResponseSerializerWithData serializer];
_manager.requestSerializer = _jsonSerializer;
_manager.requestSerializer = _managerJsonRequestSerializer;
}
return _manager;
}
- (BOOL) setCredentialIfPossible {
BOOL response = NO;
if (_currentOauthUserCredential && ![_currentOauthUserCredential isExpired] && ![[_manager requestSerializer] hasAuthorizationHeaderField]) {
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
response = YES;
}
else if (_currentOauthClientCredential && ![_currentOauthClientCredential isExpired] && ![[_manager requestSerializer] hasAuthorizationHeaderField]) {
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
response = YES;
}
return response;
}
- (AFOAuth2Manager *) clientCredentialAuthManager {
BOOL canTryRefreh = NO;
@ -379,41 +358,34 @@ static bool isFirstAccess = YES;
if (!_clientCredentialAuthManager) {
_clientCredentialAuthManager = [AFOAuth2Manager manager];
switch (_oauthMode) {
case OAuthModeClientCredential:{
if (_currentOAuthClientID && _currentOAuthClientSecret) {
if (![_clientCredentialAuthManager clientID]) {
_clientCredentialAuthManager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentOauthEndPointUrl] clientID:_currentOAuthClientID secret:_currentOAuthClientSecret];
}
[_clientCredentialAuthManager setUseHTTPBasicAuthentication:NO];
canTryRefreh = YES;
}
}
break;
case OAuthModePassword:
case OAuthModeNo:
default:{
if (_currentClientCredenzialClientID && _currentClientCredenzialClientSecret) {
if (![_clientCredentialAuthManager clientID]) {
_clientCredentialAuthManager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentClientCredenzialEndPointUrl] clientID:_currentClientCredenzialClientID secret:_currentClientCredenzialClientSecret];
}
break;
[_clientCredentialAuthManager setUseHTTPBasicAuthentication:NO];
canTryRefreh = YES;
}
for (NSString *key in [_headerFields allKeys]) {
[_httpSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_jsonSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_oauthHttpRequestSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_oauthJsonRequestSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
}
//[_oauthJsonRequestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
_clientCredentialAuthManager.responseSerializer = [AFJSONResponseSerializerWithData serializer];
_clientCredentialAuthManager.requestSerializer = _oauthJsonRequestSerializer;
if (canTryRefreh) {
if (_currentOauthClientCredential && ![_currentOauthClientCredential isExpired] && ![[_manager requestSerializer] hasAuthorizationHeaderField]) {
if (_currentOauthClientCredential && ![_currentOauthClientCredential isExpired]) {
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthClientCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthClientCredential];
[_oauthHttpRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthClientCredential];
[_oauthJsonRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthClientCredential];
[_clientCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthClientCredential];
}
else {
@ -421,7 +393,6 @@ static bool isFirstAccess = YES;
}
}
_clientCredentialAuthManager.responseSerializer = [AFJSONResponseSerializerWithData serializer];
}
return _clientCredentialAuthManager;
@ -434,41 +405,29 @@ static bool isFirstAccess = YES;
if (!_userCredentialAuthManager) {
_userCredentialAuthManager = [AFOAuth2Manager manager];
switch (_oauthMode) {
if (_currentUserCredenzialClientID && _currentUserCredenzialClientID && _currentOAuthUserName && _currentOAuthPassword) {
case OAuthModePassword:{
if (_currentClientID && _currentClientID && _currentOAuthUserName && _currentOAuthPassword) {
if (![_userCredentialAuthManager clientID]) {
_userCredentialAuthManager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentOauthEndPointUrl] clientID:_currentClientID secret:_currentClientID];
}
[_userCredentialAuthManager setUseHTTPBasicAuthentication:NO];
canTryRefreh = YES;
}
if (![_userCredentialAuthManager clientID]) {
_userCredentialAuthManager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentClientCredenzialEndPointUrl] clientID:_currentUserCredenzialClientID secret:_currentUserCredenzialClientID];
}
break;
case OAuthModeClientCredential:
case OAuthModeNo:
default:{
}
break;
[_userCredentialAuthManager setUseHTTPBasicAuthentication:NO];
canTryRefreh = YES;
}
for (NSString *key in [_headerFields allKeys]) {
[_httpSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_jsonSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_oauthJsonRequestSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
[_oauthHttpRequestSerializer setValue:[_headerFields objectForKey:key] forHTTPHeaderField:key];
}
if (canTryRefreh) {
if (_currentOauthUserCredential && ![_currentOauthUserCredential isExpired] && ![[_manager requestSerializer] hasAuthorizationHeaderField]) {
[_httpSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_jsonSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_oauthHttpRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_oauthJsonRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_userCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
}
else {
@ -477,25 +436,42 @@ static bool isFirstAccess = YES;
}
_userCredentialAuthManager.responseSerializer = [AFJSONResponseSerializerWithData serializer];
_userCredentialAuthManager.requestSerializer = _oauthJsonRequestSerializer;
}
return _userCredentialAuthManager;
}
- (BOOL) resetToken {
if (_currentOauthClientCredential) {
_currentOauthClientCredential = nil;
[AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceClientCredentialIdentifier];
return [AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceUserCredentialIdentifier];
}
if (_currentOauthUserCredential) {
_currentOauthUserCredential = nil;
[AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceClientCredentialIdentifier];
return [AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceUserCredentialIdentifier];
- (BOOL) resetTokenForOauthMode:(OAuthMode) oauthMode {
switch (oauthMode) {
case OAuthModeClientCredential:
if (_currentOauthClientCredential) {
_currentOauthClientCredential = nil;
[AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceClientCredentialIdentifier];
return [AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceUserCredentialIdentifier];
}
break;
case OAuthModePassword:
if (_currentOauthUserCredential) {
_currentOauthUserCredential = nil;
[AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceClientCredentialIdentifier];
return [AFOAuthCredential deleteCredentialWithIdentifier:PNObjectServiceUserCredentialIdentifier];
}
break;
default:
break;
}
return NO;
}
- (void) resetAllTokens {
[self resetTokenForOauthMode:OAuthModeClientCredential];
[self resetTokenForOauthMode:OAuthModePassword];
}
- (void) refreshToken {
[self refreshTokenWithBlockSuccess:nil failure:nil];
@ -517,71 +493,207 @@ static bool isFirstAccess = YES;
[self refreshTokenForUserWithBlockSuccess:nil failure:nil];
}
- (void) refreshTokenForClientCredential {
[self refreshTokenForClientCredentialWithBlockSuccess:nil failure:nil];
}
- (void) refreshTokenForClientCredentialWithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
[self refreshTokenForOauthMode:OAuthModeClientCredential WithBlockSuccess:success failure:failure];
}
- (void) refreshTokenForUserWithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
__block __typeof__(_currentOauthUserCredential) __weak wCurrentOauthCredential = _currentOauthUserCredential;
[self refreshTokenForOauthMode:OAuthModePassword WithBlockSuccess:success failure:failure];
}
- (void) refreshTokenForOauthMode:(OAuthMode) oauthMode
WithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
__block __typeof__(_httpSerializer) __weak wHttpSerializer = _httpSerializer;
__block __typeof__(_jsonSerializer) __weak wJsonSerializer = _jsonSerializer;
__block __typeof__(_manager) __weak wManager = _manager;
__block __typeof__(_currentOauthClientCredential) __weak wCurrentOauthCredential = _currentOauthClientCredential;
__block __typeof__(_managerHttpRequestSerializer) __weak wHttpSerializer = _managerHttpRequestSerializer;
__block __typeof__(_managerJsonRequestSerializer) __weak wJsonSerializer = _managerJsonRequestSerializer;
__block __typeof__(_clientCredentialAuthManager) __weak wClientCredentialAuthManager = _clientCredentialAuthManager;
__block __typeof__(_userCredentialAuthManager) __weak wUserCredentialAuthManager = _userCredentialAuthManager;
__block __typeof__(_manager) __weak wManager = _manager;
if (_currentOauthUserCredential) {
[_clientCredentialAuthManager authenticateUsingOAuthWithURLString:[_currentOauthEndPointUrl stringByAppendingString:_currentOauthEndPointAction] refreshToken:[_currentOauthUserCredential refreshToken] success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceUserCredentialIdentifier];
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wUserCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserSuccess object:nil];
if (success) {
success(YES);
switch (oauthMode) {
case OAuthModeClientCredential:
if (_currentOauthClientCredential && ![_currentOauthClientCredential isExpired]) {
[_clientCredentialAuthManager authenticateUsingOAuthWithURLString:_currentClientCredenzialEndPointUrl refreshToken:[_currentOauthClientCredential refreshToken] success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceClientCredentialIdentifier];
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wClientCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wUserCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialSuccess object:nil];
if (success) {
success(YES);
}
return;
} failure:^(NSError * _Nonnull error) {
[self resetTokenForOauthMode:oauthMode];
[self refreshTokenForClientCredentialWithBlockSuccess:success failure:failure];
return;
}];
}
return;
} failure:^(NSError * _Nonnull error) {
[self resetToken];
[self refreshTokenForUserWithBlockSuccess:success failure:failure];
return;
}];
}
else {
if([SINGLETON_PNObjectConfig.userSubClass currentUser] && [[SINGLETON_PNObjectConfig.userSubClass currentUser] hasValidEmailAndPasswordData]) {
[self refreshTokenForUserWithEmail:[[SINGLETON_PNObjectConfig.userSubClass currentUser] email] password:[[(PNUser*)[SINGLETON_PNObjectConfig.userSubClass currentUser] password] password] withBlockSuccess:success failure:failure];
return;
}
else if ([SINGLETON_PNObjectConfig.userSubClass currentUser] && [[SINGLETON_PNObjectConfig.userSubClass currentUser] facebookId]){
[FBSDKAccessToken refreshCurrentAccessToken:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (error) {
else {
[_clientCredentialAuthManager authenticateUsingOAuthWithURLString:_currentClientCredenzialEndPointUrl scope:@"" success:^(AFOAuthCredential * _Nonnull credential) {
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceClientCredentialIdentifier];
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wClientCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wUserCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialSuccess object:nil];
if (success) {
success(YES);
}
return;
} failure:^(NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
break;
case OAuthModePassword:
if (_currentOauthClientCredential && ![_currentOauthClientCredential isExpired]) {
if (_currentOauthUserCredential && ![_currentOauthUserCredential isExpired]) {
[_userCredentialAuthManager authenticateUsingOAuthWithURLString:_currentClientCredenzialEndPointUrl refreshToken:[_currentOauthUserCredential refreshToken] success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceUserCredentialIdentifier];
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wUserCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialSuccess object:nil];
if (success) {
success(YES);
}
return;
} failure:^(NSError * _Nonnull error) {
[self resetTokenForOauthMode:oauthMode];
[self refreshTokenForOauthMode:oauthMode WithBlockSuccess:success failure:failure];
return;
}];
}
else {
[self refreshTokenForUserWithFacebookId:[[SINGLETON_PNObjectConfig.userSubClass currentUser] facebookId] facebookToken:[[FBSDKAccessToken currentAccessToken] tokenString] withBlockSuccess:success failure:failure];
if([SINGLETON_PNObjectConfig.userSubClass currentUser] && [[SINGLETON_PNObjectConfig.userSubClass currentUser] hasValidEmailAndPasswordData]) {
[self refreshTokenForUserWithEmail:[[SINGLETON_PNObjectConfig.userSubClass currentUser] email] password:[[(PNUser*)[SINGLETON_PNObjectConfig.userSubClass currentUser] password] password] withBlockSuccess:success failure:failure];
return;
}
else if ([SINGLETON_PNObjectConfig.userSubClass currentUser] && [[SINGLETON_PNObjectConfig.userSubClass currentUser] facebookId]){
[FBSDKAccessToken refreshCurrentAccessToken:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (error) {
if (failure) {
failure(error);
}
}
else {
[self refreshTokenForUserWithFacebookId:[[SINGLETON_PNObjectConfig.userSubClass currentUser] facebookId] facebookToken:[[FBSDKAccessToken currentAccessToken] tokenString] withBlockSuccess:success failure:failure];
}
}];
}
else {
if (failure) {
NSError *error = [NSError errorWithDomain:@"" code:kHTTPStatusCodeBadRequest userInfo:nil];
failure(error);
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserFail object:nil];
}
}
}
}];
}
else {
if (failure) {
}
else {
NSError *error = [NSError errorWithDomain:@"" code:kHTTPStatusCodeBadRequest userInfo:nil];
failure(error);
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserFail object:nil];
[self refreshTokenForClientCredentialWithBlockSuccess:^(BOOL refreshSuccess) {
[self refreshTokenForOauthMode:oauthMode WithBlockSuccess:success failure:failure];
} failure:failure];
}
break;
default: {
if (success) {
success(YES);
}
}
break;
}
}
- (void) refreshTokenForUserWithFacebookId:(NSString * _Nonnull) facebookId
facebookToken:(NSString * _Nonnull) facebookToken
withBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
if (!facebookId || [facebookId length] == 0) {
if (failure) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"Facebook id is not valid", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
failure(error);
return;
}
}
if (!facebookToken || [facebookToken length] == 0) {
if (failure) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"Facebook token is not valid", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
failure(error);
return;
}
}
__block __typeof__(_currentOauthUserCredential) __weak wCurrentOauthCredential = _currentOauthUserCredential;
__block __typeof__(_managerHttpRequestSerializer) __weak wHttpSerializer = _managerHttpRequestSerializer;
__block __typeof__(_managerJsonRequestSerializer) __weak wJsonSerializer = _managerJsonRequestSerializer;
__block __typeof__(_manager) __weak wManager = _manager;
__block __typeof__(_userCredentialAuthManager) __weak wUserCredentialAuthManager = _userCredentialAuthManager;
[_userCredentialAuthManager authenticateUsingFacebookOAuthWithURLString:_currentClientCredenzialEndPointUrl facebookId:facebookId facebookToken:facebookToken scope:@"" success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceUserCredentialIdentifier];
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wUserCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserSuccess object:nil];
if (success) {
success(YES);
}
} failure:^(NSError * _Nonnull error) {
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserFail object:nil];
if (failure) {
failure(error);
}
}];
}
- (void) refreshTokenForUserWithEmail:(NSString * _Nonnull) email
password:(NSString * _Nonnull) password
withBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
@ -604,13 +716,13 @@ static bool isFirstAccess = YES;
__block __typeof__(_currentOauthUserCredential) __weak wCurrentOauthCredential = _currentOauthUserCredential;
__block __typeof__(_httpSerializer) __weak wHttpSerializer = _httpSerializer;
__block __typeof__(_jsonSerializer) __weak wJsonSerializer = _jsonSerializer;
__block __typeof__(_managerHttpRequestSerializer) __weak wHttpSerializer = _managerHttpRequestSerializer;
__block __typeof__(_managerJsonRequestSerializer) __weak wJsonSerializer = _managerJsonRequestSerializer;
__block __typeof__(_manager) __weak wManager = _manager;
__block __typeof__(_userCredentialAuthManager) __weak wUserCredentialAuthManager = _userCredentialAuthManager;
[_userCredentialAuthManager authenticateUsingOAuthWithURLString:[_currentOauthEndPointUrl stringByAppendingString:_currentOauthEndPointAction] username:email password:password scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
[_userCredentialAuthManager authenticateUsingOAuthWithURLString:_currentClientCredenzialEndPointUrl username:email password:password scope:@"" success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceUserCredentialIdentifier];
@ -634,131 +746,6 @@ static bool isFirstAccess = YES;
}];
}
- (void) refreshTokenForUserWithFacebookId:(NSString * _Nonnull) facebookId
facebookToken:(NSString * _Nonnull) facebookToken
withBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
if (!facebookId || [facebookId length] == 0) {
if (failure) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"Facebook id is not valid", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
failure(error);
return;
}
}
if (!facebookToken || [facebookToken length] == 0) {
if (failure) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"Facebook token is not valid", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
failure(error);
return;
}
}
__block __typeof__(_currentOauthUserCredential) __weak wCurrentOauthCredential = _currentOauthUserCredential;
__block __typeof__(_httpSerializer) __weak wHttpSerializer = _httpSerializer;
__block __typeof__(_jsonSerializer) __weak wJsonSerializer = _jsonSerializer;
__block __typeof__(_manager) __weak wManager = _manager;
__block __typeof__(_userCredentialAuthManager) __weak wUserCredentialAuthManager = _userCredentialAuthManager;
[_userCredentialAuthManager authenticateUsingFacebookOAuthWithURLString:[_currentOauthEndPointUrl stringByAppendingString:_currentOauthEndPointAction] facebookId:facebookId facebookToken:facebookToken scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceUserCredentialIdentifier];
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wUserCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserSuccess object:nil];
if (success) {
success(YES);
}
} failure:^(NSError * _Nonnull error) {
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserFail object:nil];
if (failure) {
failure(error);
}
}];
}
- (void) refreshTokenForClientCredential {
[self refreshTokenForClientCredentialWithBlockSuccess:nil failure:nil];
}
- (void) refreshTokenForClientCredentialWithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
__block __typeof__(_currentOauthClientCredential) __weak wCurrentOauthCredential = _currentOauthClientCredential;
__block __typeof__(_httpSerializer) __weak wHttpSerializer = _httpSerializer;
__block __typeof__(_jsonSerializer) __weak wJsonSerializer = _jsonSerializer;
__block __typeof__(_clientCredentialAuthManager) __weak wAuthManager = _clientCredentialAuthManager;
__block __typeof__(_manager) __weak wManager = _manager;
if (_currentOauthClientCredential) {
[_clientCredentialAuthManager authenticateUsingOAuthWithURLString:[_currentOauthEndPointUrl stringByAppendingString:_currentOauthEndPointAction] refreshToken:[_currentOauthClientCredential refreshToken] success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceUserCredentialIdentifier];
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialSuccess object:nil];
if (success) {
success(YES);
}
return;
} failure:^(NSError * _Nonnull error) {
[self resetToken];
[self refreshTokenForClientCredentialWithBlockSuccess:success failure:failure];
return;
}];
}
else {
switch (_oauthMode) {
case OAuthModeClientCredential:{
[_clientCredentialAuthManager authenticateUsingOAuthWithURLString:[_currentOauthEndPointUrl stringByAppendingString:_currentOauthEndPointAction] scope:nil success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
[AFOAuthCredential storeCredential:wCurrentOauthCredential withIdentifier:PNObjectServiceUserCredentialIdentifier];
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialSuccess object:nil];
if (success) {
success(YES);
}
} failure:^(NSError * _Nonnull error) {
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialFail object:nil];
if (failure) {
failure(error);
}
}];
}
break;
case OAuthModePassword:
case OAuthModeNo:
default:
break;
}
}
}
- (void) setAcceptablePasswordLenght:(NSUInteger) passLenght {
_minPasswordLenght = passLenght;
@ -774,14 +761,26 @@ static bool isFirstAccess = YES;
}
}
- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction forEnv:(NSString *) environment {
- (void) setClientID:(NSString * _Nonnull) clientID clientSecret:(NSString* _Nonnull) clientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction oauthMode:(OAuthMode) oauthMode forEnv:(NSString *) environment {
if ([_configuration objectForKey:environment]) {
NSMutableDictionary *currentConfigurationDict = [[NSMutableDictionary alloc] initWithDictionary:[_configuration objectForKey:environment]];
[currentConfigurationDict setObject:clientID forKey:Client_ID];
[currentConfigurationDict setObject:clientSecret forKey:Client_Secret];
[currentConfigurationDict setObject:oAuthEndpointAction forKey:OAuthEndpointAction];
switch (oauthMode) {
case OAuthModeClientCredential:
[currentConfigurationDict setObject:clientID forKey:Client_Credential_ID];
[currentConfigurationDict setObject:clientSecret forKey:Client_Credential_Secret];
[currentConfigurationDict setObject:oAuthEndpointAction forKey:Client_CredentialEndpointAction];
break;
case OAuthModePassword:{
[currentConfigurationDict setObject:clientID forKey:Client_ID];
[currentConfigurationDict setObject:clientSecret forKey:Client_Secret];
[currentConfigurationDict setObject:oAuthEndpointAction forKey:Client_EndpointAction];
}
break;
default:
break;
}
[_configuration setObject:currentConfigurationDict forKey:environment];
@ -791,38 +790,31 @@ static bool isFirstAccess = YES;
}
}
- (void) setOauthClientID:(NSString * _Nonnull) oauthClientID oauthClientSecret:(NSString* _Nonnull) oauthClientSecret oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction forEnv:(NSString *) environment {
if ([_configuration objectForKey:environment]) {
NSMutableDictionary *currentConfigurationDict = [[NSMutableDictionary alloc] initWithDictionary:[_configuration objectForKey:environment]];
[currentConfigurationDict setObject:oauthClientID forKey:OAuthClient_ID];
[currentConfigurationDict setObject:oauthClientSecret forKey:OAuthClient_Secret];
[currentConfigurationDict setObject:oAuthEndpointAction forKey:OAuthEndpointAction];
[_configuration setObject:currentConfigurationDict forKey:environment];
if (_currentEnv == environment) {
[self setEnvironment:environment];
}
}
}
- (void) setOauthUserName:(NSString * _Nonnull)oauthUserName oauthPassword:(NSString* _Nonnull) oauthPassword oAuthEndpointAction:(NSString* _Nonnull) oAuthEndpointAction forEnv:(NSString *) environment {
if ([_configuration objectForKey:environment]) {
NSMutableDictionary *currentConfigurationDict = [[NSMutableDictionary alloc] initWithDictionary:[_configuration objectForKey:environment]];
[currentConfigurationDict setObject:oauthUserName forKey:Client_Username];
[currentConfigurationDict setObject:oauthPassword forKey:Client_Password];
[currentConfigurationDict setObject:oAuthEndpointAction forKey:OAuthEndpointAction];
[_configuration setObject:currentConfigurationDict forKey:environment];
if (_currentEnv == environment) {
[self setEnvironment:environment];
}
- (BOOL) setCredentialTokenForOauthMode:(OAuthMode) oauthMode {
switch (oauthMode) {
case OAuthModeClientCredential:
if (_currentOauthClientCredential && ![_currentOauthClientCredential isExpired]) {
return NO;
}
[_managerHttpRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthClientCredential];
[_managerJsonRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthClientCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthClientCredential];
break;
case OAuthModePassword:
if (_currentOauthUserCredential && ![_currentOauthUserCredential isExpired]) {
return NO;
}
[_managerHttpRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_managerJsonRequestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
[_manager.requestSerializer setAuthorizationHeaderFieldWithCredential:_currentOauthUserCredential];
break;
default:
[_managerHttpRequestSerializer setValue:@"" forHTTPHeaderField:@"Authorization"];
[_managerJsonRequestSerializer setValue:@"" forHTTPHeaderField:@"Authorization"];
[_manager.requestSerializer setValue:@"" forHTTPHeaderField:@"Authorization"];
break;
}
return YES;
}
@end

View File

@ -37,7 +37,7 @@ Configure PNObject endpoint client ID, client secret and OAuthModePassword with
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @{BaseUrl:@"http://pnobject.local/",EndpointPath:@"api/v1/"},
EnvironmentStage : @{BaseUrl:@"http://pnobject.stage.it/",EndpointPath:@"api/v1/"},
EnvironmentProduction : @{BaseUrl:@"http://pnobject.prod.it/",EndpointPath:@"api/v1/"},
} userSubclass:[PNUser class] withOauthMode:OAuthModeClientCredential];
} userSubclass:[PNUser class]];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentProduction];
@ -71,7 +71,7 @@ Configure PNObject endpoint client ID, client secret and OAuthModePassword
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @"http://pnobject.local/api/v1/",
EnvironmentStage : @"http://pnobject.stage.it/api/v1/",
EnvironmentProduction : @"http://pnobject.prod.it/api/v1/"
} userSubclass:[PNUser class] withOauthMode:OAuthModePassword];
} userSubclass:[PNUser class]];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentProduction];
@ -95,7 +95,7 @@ Configure PNObject endpoint client ID, client secret and OAuthModeClientCredenti
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @"http://pnobject.local/api/v1/",
EnvironmentStage : @"http://pnobject.stage.it/api/v1/",
EnvironmentProduction : @"http://pnobject.prod.it/api/v1/"
} userSubclass:[PNUser class] withOauthMode:OAuthModeClientCredential];
} userSubclass:[PNUser class]];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentProduction];
@ -114,7 +114,7 @@ Configure PNObject endpoint and using custom PNUser object
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @"http://pnobject.local/api/v1/",
EnvironmentStage : @"http://pnobject.stage.it/api/v1/",
EnvironmentProduction : @"http://pnobject.prod.it/api/v1/"
} userSubclass:[PNUser class] withOauthMode:OAuthModeClientCredential];
} userSubclass:[PNUser class]];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentProduction];