- fix refresh token

This commit is contained in:
Giuseppe Nucifora 2018-03-27 03:05:24 +02:00
parent 9266ae3689
commit 45f9930941
9 changed files with 163 additions and 39 deletions

View File

@ -95,6 +95,12 @@
- (void) refreshTokenAction {
[[PNObjectConfig sharedInstance] refreshTokenForClientCredentialWithBlockSuccess:^(BOOL refreshSuccess) {
[PNUser loginCurrentUserWithUsername:@"test" password:@"test" withBlockSuccess:^(PNUser * _Nullable responseObject) {
NSLog(@"success");
} failure:^(NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
} failure:^(NSError * _Nonnull error) {
}];

View File

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

View File

@ -59,6 +59,18 @@
withBlockSuccess:(nullable void (^)(PNUser * _Nullable responseObject))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
/**
* <#Description#>
*
* @param username username description
* @param password <#password description#>
* @param success <#success description#>
* @param failure <#failure description#>
*/
+ (void) loginCurrentUserWithUsername:(NSString * _Nonnull) username
password:(NSString * _Nonnull) password
withBlockSuccess:(nullable void (^)(PNUser * _Nullable responseObject))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
/**
* <#Description#>

View File

@ -390,6 +390,38 @@ static bool isFirstAccess = YES;
}
+ (void) loginCurrentUserWithUsername:(NSString * _Nonnull) username
password:(NSString * _Nonnull) password
withBlockSuccess:(nullable void (^)(PNUser * _Nullable responseObject))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
[[PNObjectConfig sharedInstance] refreshTokenForUserWithUsername:username password:password withBlockSuccess:^(BOOL refreshSuccess) {
if (refreshSuccess) {
PNUser *user = [[self class] new];
PNObjcPassword *objectPassword = [PNObjcPassword new];
[objectPassword setPassword:password];
[objectPassword setConfirmPassword:password];
[user setAuthenticated:YES];
[user setUsername:username];
[user setPassword:objectPassword];
[user saveLocally];
[user reloadFormServer];
USER = user;
if (success) {
success(user);
}
}
} failure:failure];
}
+ (void) uploadAvatar:(UIImage * _Nonnull) avatar
Progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
Success:(nullable void (^)(NSDictionary * _Nullable responseObject))success

View File

@ -86,7 +86,7 @@
}
else {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode WithBlockSuccess:^(BOOL refreshSuccess) {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode retries:MAX_RETRIES WithBlockSuccess:^(BOOL refreshSuccess) {
[self DELETEWithEndpointAction:endPoint
oauthMode:oauthMode

View File

@ -102,7 +102,7 @@
}];
}
else {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode WithBlockSuccess:^(BOOL refreshSuccess) {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode retries:MAX_RETRIES WithBlockSuccess:^(BOOL refreshSuccess) {
return [self GETWithEndpointAction:endPoint
oauthMode:oauthMode

View File

@ -134,6 +134,7 @@
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]){
[[[PNObjectConfig sharedInstance] manager] POST:[[[PNObjectConfig sharedInstance] endPointUrl] stringByAppendingFormat:@"%@",endPoint]
@ -172,7 +173,7 @@
}];
}
else {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode WithBlockSuccess:^(BOOL refreshSuccess) {
[[PNObjectConfig sharedInstance] refreshTokenForOauthMode:oauthMode retries:MAX_RETRIES WithBlockSuccess:^(BOOL refreshSuccess) {
return [self POSTWithEndpointAction:endPoint oauthMode:oauthMode formData:postFormData parameters:parameters retries:retries-1 progress:uploadProgress success:success failure:failure];
} failure:^(NSError * _Nonnull error) {

View File

@ -210,6 +210,11 @@ typedef NS_ENUM(NSInteger, OAuthMode) {
withBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
- (void) refreshTokenForUserWithUsername:(NSString * _Nonnull) username
password:(NSString * _Nonnull) password
withBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
/**
* <#Description#>
*/
@ -222,8 +227,9 @@ typedef NS_ENUM(NSInteger, OAuthMode) {
* <#Description#>
*/
- (void) refreshTokenForOauthMode:(OAuthMode) oauthMode
WithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
retries:(NSUInteger) retries
WithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure;
/**
* <#Description#>

View File

@ -16,6 +16,7 @@
#import <NSDataAES/NSData+AES.h>
#import <DDDKeychainWrapper/DDDKeychainWrapper.h>
#import "HTTPStatusCodes.h"
#import "PNObject+PNObjectConnection.h"
NSString * const PNObjectLocalNotificationRefreshTokenClientCredentialSuccess = @"PNObjectLocalNotificationRefreshTokenClientCredentialSuccess";
NSString * const PNObjectLocalNotificationRefreshTokenClientCredentialFail = @"PNObjectLocalNotificationRefreshTokenClientCredentialFail";
@ -261,10 +262,10 @@ static bool isFirstAccess = YES;
if (![currentEnvConfig objectForKey:Client_ID] || ![currentEnvConfig objectForKey:Client_Secret]) {
_currentUserCredenzialClientID = _currentClientCredenzialClientID;
_currentUserCredenzialClientSecret = _currentClientCredenzialClientSecret;
_currentUserCredenzialEndPointPath = _currentClientCredenzialEndPointPath;
_currentUserCredenzialEndPointUrl = _currentClientCredenzialEndPointUrl;
_currentUserCredenzialClientID = _currentClientCredenzialClientID;
_currentUserCredenzialClientSecret = _currentClientCredenzialClientSecret;
_currentUserCredenzialEndPointPath = _currentClientCredenzialEndPointPath;
_currentUserCredenzialEndPointUrl = _currentClientCredenzialEndPointUrl;
}
else {
_currentUserCredenzialClientID = [currentEnvConfig objectForKey:Client_ID];
@ -405,10 +406,10 @@ static bool isFirstAccess = YES;
if (!_userCredentialAuthManager) {
_userCredentialAuthManager = [AFOAuth2Manager manager];
if (_currentUserCredenzialClientID && _currentUserCredenzialClientID && _currentOAuthUserName && _currentOAuthPassword) {
if ((_currentUserCredenzialClientID && _currentUserCredenzialClientID) || (_currentOAuthUserName && _currentOAuthPassword)) {
if (![_userCredentialAuthManager clientID]) {
_userCredentialAuthManager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentClientCredenzialEndPointUrl] clientID:_currentUserCredenzialClientID secret:_currentUserCredenzialClientID];
_userCredentialAuthManager = [AFOAuth2Manager managerWithBaseURL:[NSURL URLWithString:_currentClientCredenzialEndPointUrl] clientID:_currentUserCredenzialClientID secret:_currentUserCredenzialClientSecret];
}
[_userCredentialAuthManager setUseHTTPBasicAuthentication:NO];
@ -499,27 +500,27 @@ static bool isFirstAccess = YES;
- (void) refreshTokenForClientCredentialWithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
[self refreshTokenForOauthMode:OAuthModeClientCredential WithBlockSuccess:success failure:failure];
[self refreshTokenForOauthMode:OAuthModeClientCredential retries:MAX_RETRIES WithBlockSuccess:success failure:failure];
}
- (void) refreshTokenForUserWithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
[self refreshTokenForOauthMode:OAuthModePassword WithBlockSuccess:success failure:failure];
[self refreshTokenForOauthMode:OAuthModePassword retries:MAX_RETRIES WithBlockSuccess:success failure:failure];
}
- (void) refreshTokenForOauthMode:(OAuthMode) oauthMode
retries:(NSUInteger) retries
WithBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
__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;
__block __typeof__(_currentOauthClientCredential) wCurrentOauthCredential = _currentOauthClientCredential;
__block __typeof__(_managerHttpRequestSerializer) wHttpSerializer = _managerHttpRequestSerializer;
__block __typeof__(_managerJsonRequestSerializer) wJsonSerializer = _managerJsonRequestSerializer;
__block __typeof__(_clientCredentialAuthManager) wClientCredentialAuthManager = _clientCredentialAuthManager;
__block __typeof__(_userCredentialAuthManager) wUserCredentialAuthManager = _userCredentialAuthManager;
__block __typeof__(_manager) wManager = _manager;
switch (oauthMode) {
case OAuthModeClientCredential:
@ -533,7 +534,6 @@ static bool isFirstAccess = YES;
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wClientCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wUserCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialSuccess object:nil];
@ -545,7 +545,15 @@ static bool isFirstAccess = YES;
} failure:^(NSError * _Nonnull error) {
[self resetTokenForOauthMode:oauthMode];
[self refreshTokenForClientCredentialWithBlockSuccess:success failure:failure];
if (retries > 0) {
[self refreshTokenForOauthMode:oauthMode retries:retries-1 WithBlockSuccess:success failure:failure];
}
else {
if (failure) {
failure(error);
}
}
return;
}];
}
@ -557,7 +565,6 @@ static bool isFirstAccess = YES;
[wHttpSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wJsonSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wClientCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wUserCredentialAuthManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[wManager.requestSerializer setAuthorizationHeaderFieldWithCredential:wCurrentOauthCredential];
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenClientCredentialSuccess object:nil];
@ -595,8 +602,15 @@ static bool isFirstAccess = YES;
} failure:^(NSError * _Nonnull error) {
[self resetTokenForOauthMode:oauthMode];
if (retries > 0) {
[self refreshTokenForOauthMode:oauthMode retries:retries-1 WithBlockSuccess:success failure:failure];
}
else {
if (failure) {
failure(error);
}
}
[self refreshTokenForOauthMode:oauthMode WithBlockSuccess:success failure:failure];
return;
}];
}
@ -630,10 +644,18 @@ static bool isFirstAccess = YES;
}
}
else {
[self refreshTokenForClientCredentialWithBlockSuccess:^(BOOL refreshSuccess) {
[self refreshTokenForOauthMode:oauthMode WithBlockSuccess:success failure:failure];
} failure:failure];
if (retries > 0) {
[self refreshTokenForOauthMode:OAuthModeClientCredential retries:retries-1 WithBlockSuccess:^(BOOL refreshSuccess) {
[self refreshTokenForOauthMode:oauthMode retries:retries-1 WithBlockSuccess:success failure:failure];
} failure:failure];
}
else {
if (failure) {
NSError *error = [NSError errorWithDomain:@"" code:kHTTPStatusCodeBadRequest userInfo:nil];
failure(error);
[[NSNotificationCenter defaultCenter] postNotificationName:PNObjectLocalNotificationRefreshTokenUserFail object:nil];
}
}
}
break;
default: {
@ -664,12 +686,12 @@ static bool isFirstAccess = YES;
}
}
__block __typeof__(_currentOauthUserCredential) __weak wCurrentOauthCredential = _currentOauthUserCredential;
__block __typeof__(_currentOauthUserCredential) 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;
__block __typeof__(_managerHttpRequestSerializer) wHttpSerializer = _managerHttpRequestSerializer;
__block __typeof__(_managerJsonRequestSerializer) wJsonSerializer = _managerJsonRequestSerializer;
__block __typeof__(_manager) wManager = _manager;
__block __typeof__(_userCredentialAuthManager) wUserCredentialAuthManager = _userCredentialAuthManager;
[_userCredentialAuthManager authenticateUsingFacebookOAuthWithURLString:_currentClientCredenzialEndPointUrl facebookId:facebookId facebookToken:facebookToken scope:@"" success:^(AFOAuthCredential * _Nonnull credential) {
wCurrentOauthCredential = credential;
@ -714,12 +736,12 @@ static bool isFirstAccess = YES;
}
}
__block __typeof__(_currentOauthUserCredential) __weak wCurrentOauthCredential = _currentOauthUserCredential;
__block __typeof__(_currentOauthUserCredential) 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;
__block __typeof__(_managerHttpRequestSerializer) wHttpSerializer = _managerHttpRequestSerializer;
__block __typeof__(_managerJsonRequestSerializer) wJsonSerializer = _managerJsonRequestSerializer;
__block __typeof__(_manager) wManager = _manager;
__block __typeof__(_userCredentialAuthManager) wUserCredentialAuthManager = _userCredentialAuthManager;
[_userCredentialAuthManager authenticateUsingOAuthWithURLString:_currentClientCredenzialEndPointUrl username:email password:password scope:@"" success:^(AFOAuthCredential * _Nonnull credential) {
@ -746,6 +768,51 @@ static bool isFirstAccess = YES;
}];
}
- (void) refreshTokenForUserWithUsername:(NSString * _Nonnull) username
password:(NSString * _Nonnull) password
withBlockSuccess:(nullable void (^)(BOOL refreshSuccess))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
if (![SINGLETON_PNObjectConfig.userSubClass isValidPassword:password]) {
if (failure) {
NSError *error = [NSError errorWithDomain:NSLocalizedString(@"Password is not valid", @"") code:kHTTPStatusCodeBadRequest userInfo:nil];
failure(error);
return;
}
}
__block __typeof__(_currentOauthUserCredential) wCurrentOauthCredential = _currentOauthUserCredential;
__block __typeof__(_managerHttpRequestSerializer) wHttpSerializer = _managerHttpRequestSerializer;
__block __typeof__(_managerJsonRequestSerializer) wJsonSerializer = _managerJsonRequestSerializer;
__block __typeof__(_manager) wManager = _manager;
__block __typeof__(_userCredentialAuthManager) wUserCredentialAuthManager = _userCredentialAuthManager;
[_userCredentialAuthManager authenticateUsingOAuthWithURLString:_currentClientCredenzialEndPointUrl username:username password:password 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) setAcceptablePasswordLenght:(NSUInteger) passLenght {
_minPasswordLenght = passLenght;