From f0c0c90cc8eb625327f123e2d9d7d68285e2d910 Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 14 Sep 2015 19:08:51 -0700 Subject: [PATCH] Cleanup PFUser third party authentication. --- .../PFUserAuthenticationController.h | 6 ++-- .../PFUserAuthenticationController.m | 32 +++++++++---------- .../PFAnonymousAuthenticationProvider.h | 2 +- .../PFAnonymousAuthenticationProvider.m | 2 +- Parse/PFAnonymousUtils.m | 10 +++--- Parse/PFUser.m | 20 ++++++------ Parse/PFUserAuthenticationDelegate.h | 4 +-- 7 files changed, 37 insertions(+), 39 deletions(-) diff --git a/Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.h b/Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.h index 9d0c4710f..dc06afac5 100644 --- a/Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.h +++ b/Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.h @@ -14,7 +14,7 @@ NS_ASSUME_NONNULL_BEGIN -@class BFTask; +@class BFTask PF_GENERIC(__covariant BFGenericType); @class PFUser; @interface PFUserAuthenticationController : NSObject @@ -33,8 +33,8 @@ NS_ASSUME_NONNULL_BEGIN ///-------------------------------------- - (BFTask PF_GENERIC(NSNumber *) *)restoreAuthenticationAsyncWithAuthData:(nullable NSDictionary *)authData - forProviderWithAuthType:(NSString *)authType; -- (BFTask PF_GENERIC(NSNumber *) *)deauthenticateAsyncWithProviderForAuthType:(NSString *)authType; + forAuthType:(NSString *)authType; +- (BFTask PF_GENERIC(NSNumber *) *)deauthenticateAsyncWithAuthType:(NSString *)authType; ///-------------------------------------- /// @name Log In diff --git a/Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.m b/Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.m index 43950dcb4..d508f38bb 100644 --- a/Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.m +++ b/Parse/Internal/User/AuthenticationProviders/Controller/PFUserAuthenticationController.m @@ -20,7 +20,7 @@ @interface PFUserAuthenticationController () { dispatch_queue_t _dataAccessQueue; - NSMutableDictionary *_authenticationProviders; + NSMutableDictionary PF_GENERIC(NSString *, id) *_authenticationDelegates; } @end @@ -36,7 +36,7 @@ - (instancetype)init { if (!self) return nil; _dataAccessQueue = dispatch_queue_create("com.parse.user.authenticationManager", DISPATCH_QUEUE_SERIAL); - _authenticationProviders = [NSMutableDictionary dictionary]; + _authenticationDelegates = [NSMutableDictionary dictionary]; return self; } @@ -47,16 +47,16 @@ - (instancetype)init { - (void)registerAuthenticationDelegate:(id)delegate forAuthType:(NSString *)authType { PFParameterAssert(delegate, @"Authentication delegate can't be `nil`."); - PFParameterAssert(authType, @"Authentication provider's `authType` can't be `nil`."); + PFParameterAssert(authType, @"`authType` can't be `nil`."); PFConsistencyAssert(![self authenticationDelegateForAuthType:authType], - @"Authentication provider already registered for authType `%@`.", authType); + @"Authentication delegate already registered for authType `%@`.", authType); dispatch_sync(_dataAccessQueue, ^{ - _authenticationProviders[authType] = delegate; + _authenticationDelegates[authType] = delegate; }); // TODO: (nlutsenko) Decouple this further. - if (![authType isEqualToString:@"anonymous"]) { + if (![authType isEqualToString:PFAnonymousUserAuthenticationType]) { [[PFUser currentUser] synchronizeAuthDataWithAuthType:authType]; } } @@ -66,7 +66,7 @@ - (void)unregisterAuthenticationDelegateForAuthType:(NSString *)authType { return; } dispatch_sync(_dataAccessQueue, ^{ - [_authenticationProviders removeObjectForKey:authType]; + [_authenticationDelegates removeObjectForKey:authType]; }); } @@ -75,23 +75,19 @@ - (void)unregisterAuthenticationDelegateForAuthType:(NSString *)authType { return nil; } - __block id provider = nil; + __block id delegate = nil; dispatch_sync(_dataAccessQueue, ^{ - provider = _authenticationProviders[authType]; + delegate = _authenticationDelegates[authType]; }); - return provider; + return delegate; } ///-------------------------------------- #pragma mark - Authentication ///-------------------------------------- -- (BFTask PF_GENERIC(NSNumber *)*)deauthenticateAsyncWithProviderForAuthType:(NSString *)authType { - return [self restoreAuthenticationAsyncWithAuthData:nil forProviderWithAuthType:authType]; -} - - (BFTask PF_GENERIC(NSNumber *)*)restoreAuthenticationAsyncWithAuthData:(nullable NSDictionary *)authData - forProviderWithAuthType:(NSString *)authType { + forAuthType:(NSString *)authType { id provider = [self authenticationDelegateForAuthType:authType]; if (!provider) { return [BFTask taskWithResult:@YES]; @@ -101,6 +97,10 @@ - (void)unregisterAuthenticationDelegateForAuthType:(NSString *)authType { }]; } +- (BFTask PF_GENERIC(NSNumber *)*)deauthenticateAsyncWithAuthType:(NSString *)authType { + return [self restoreAuthenticationAsyncWithAuthData:nil forAuthType:authType]; +} + ///-------------------------------------- #pragma mark - Log In ///-------------------------------------- @@ -115,7 +115,7 @@ - (BFTask *)logInUserAsyncWithAuthType:(NSString *)authType authData:(NSDictiona BFTask *resolveLaziness = nil; NSDictionary *oldAnonymousData = nil; @synchronized(user.lock) { - oldAnonymousData = user.authData[PFUserAnonymousAuthenticationType]; + oldAnonymousData = user.authData[PFAnonymousUserAuthenticationType]; // Replace any anonymity with the new linked authData [user stripAnonymity]; diff --git a/Parse/Internal/User/AuthenticationProviders/Providers/Anonymous/PFAnonymousAuthenticationProvider.h b/Parse/Internal/User/AuthenticationProviders/Providers/Anonymous/PFAnonymousAuthenticationProvider.h index 29408dc48..d906dd132 100644 --- a/Parse/Internal/User/AuthenticationProviders/Providers/Anonymous/PFAnonymousAuthenticationProvider.h +++ b/Parse/Internal/User/AuthenticationProviders/Providers/Anonymous/PFAnonymousAuthenticationProvider.h @@ -13,7 +13,7 @@ NS_ASSUME_NONNULL_BEGIN -extern NSString *const PFUserAnonymousAuthenticationType; +extern NSString *const PFAnonymousUserAuthenticationType; @interface PFAnonymousAuthenticationProvider : NSObject diff --git a/Parse/Internal/User/AuthenticationProviders/Providers/Anonymous/PFAnonymousAuthenticationProvider.m b/Parse/Internal/User/AuthenticationProviders/Providers/Anonymous/PFAnonymousAuthenticationProvider.m index 126426712..e33815101 100644 --- a/Parse/Internal/User/AuthenticationProviders/Providers/Anonymous/PFAnonymousAuthenticationProvider.m +++ b/Parse/Internal/User/AuthenticationProviders/Providers/Anonymous/PFAnonymousAuthenticationProvider.m @@ -11,7 +11,7 @@ #import -NSString *const PFUserAnonymousAuthenticationType = @"anonymous"; +NSString *const PFAnonymousUserAuthenticationType = @"anonymous"; @implementation PFAnonymousAuthenticationProvider diff --git a/Parse/PFAnonymousUtils.m b/Parse/PFAnonymousUtils.m index 91f258a6f..073e5254f 100644 --- a/Parse/PFAnonymousUtils.m +++ b/Parse/PFAnonymousUtils.m @@ -23,7 +23,7 @@ @implementation PFAnonymousUtils + (BFTask *)logInInBackground { PFAnonymousAuthenticationProvider *provider = [self _authenticationProvider]; - return [PFUser logInWithAuthTypeInBackground:PFUserAnonymousAuthenticationType authData:provider.authData]; + return [PFUser logInWithAuthTypeInBackground:PFAnonymousUserAuthenticationType authData:provider.authData]; } + (void)logInWithBlock:(PFUserResultBlock)block { @@ -41,7 +41,7 @@ + (void)logInWithTarget:(id)target selector:(SEL)selector { ///-------------------------------------- + (BOOL)isLinkedWithUser:(PFUser *)user { - return [user isLinkedWithAuthType:PFUserAnonymousAuthenticationType]; + return [user isLinkedWithAuthType:PFAnonymousUserAuthenticationType]; } ///-------------------------------------- @@ -65,7 +65,7 @@ + (PFAnonymousAuthenticationProvider *)_authenticationProvider { provider = authenticationProvider_; if (!provider) { provider = [[PFAnonymousAuthenticationProvider alloc] init]; - [PFUser registerAuthenticationDelegate:provider forAuthType:PFUserAnonymousAuthenticationType]; + [PFUser registerAuthenticationDelegate:provider forAuthType:PFAnonymousUserAuthenticationType]; authenticationProvider_ = provider; } }); @@ -73,7 +73,7 @@ + (PFAnonymousAuthenticationProvider *)_authenticationProvider { } + (void)_clearAuthenticationProvider { - [PFUser _unregisterAuthenticationDelegateForAuthType:PFUserAnonymousAuthenticationType]; + [PFUser _unregisterAuthenticationDelegateForAuthType:PFAnonymousUserAuthenticationType]; dispatch_sync([self _providerAccessQueue], ^{ authenticationProvider_ = nil; }); @@ -85,7 +85,7 @@ + (void)_clearAuthenticationProvider { + (PFUser *)_lazyLogIn { PFAnonymousAuthenticationProvider *provider = [self _authenticationProvider]; - return [PFUser logInLazyUserWithAuthType:PFUserAnonymousAuthenticationType authData:provider.authData]; + return [PFUser logInLazyUserWithAuthType:PFAnonymousUserAuthenticationType authData:provider.authData]; } @end diff --git a/Parse/PFUser.m b/Parse/PFUser.m index 89a9add78..9ac146051 100644 --- a/Parse/PFUser.m +++ b/Parse/PFUser.m @@ -269,7 +269,7 @@ - (void)cleanUpAuthData { [self.linkedServiceNames removeObject:key]; [[[[self class] authenticationController] restoreAuthenticationAsyncWithAuthData:nil - forProviderWithAuthType:key] waitForResult:nil withMainThreadWarning:NO]; + forAuthType:key] waitForResult:nil withMainThreadWarning:NO]; } } } @@ -366,7 +366,7 @@ - (void)synchronizeAuthDataWithAuthType:(NSString *)authType { NSDictionary *data = self.authData[authType]; BFTask *restoreTask = [[[self class] authenticationController] restoreAuthenticationAsyncWithAuthData:data - forProviderWithAuthType:authType]; + forAuthType:authType]; [restoreTask waitForResult:nil withMainThreadWarning:NO]; if (restoreTask.faulted || ![restoreTask.result boolValue]) { // TODO: (nlutsenko) Maybe chain this method? [self unlinkWithAuthTypeInBackground:authType]; @@ -432,7 +432,7 @@ - (BFTask *)resolveLazinessAsync:(BFTask *)toAwait { } - (BFTask *)_logOutAsyncWithAuthType:(NSString *)authType { - return [[[self class] authenticationController] deauthenticateAsyncWithProviderForAuthType:authType]; + return [[[self class] authenticationController] deauthenticateAsyncWithAuthType:authType]; } + (instancetype)logInLazyUserWithAuthType:(NSString *)authType authData:(NSDictionary *)authData { @@ -454,7 +454,7 @@ - (BFTask *)signUpAsync:(BFTask *)toAwait { // For anonymous users, there may be an objectId. Setting the userName // will have removed the anonymous link and set the value in the authData // object to [NSNull null], so we can just treat it like a save operation. - if (self.authData[PFUserAnonymousAuthenticationType] == [NSNull null]) { + if (self.authData[PFAnonymousUserAuthenticationType] == [NSNull null]) { return [self saveAsync:toAwait]; } @@ -483,7 +483,7 @@ - (BFTask *)signUpAsync:(BFTask *)toAwait { @synchronized ([currentUser lock]) { NSString *oldUsername = [currentUser.username copy]; NSString *oldPassword = [currentUser.password copy]; - NSArray *oldAnonymousData = currentUser.authData[PFUserAnonymousAuthenticationType]; + NSArray *oldAnonymousData = currentUser.authData[PFAnonymousUserAuthenticationType]; [currentUser checkForChangesToMutableContainers]; @@ -553,7 +553,7 @@ - (BFTask *)signUpAsync:(BFTask *)toAwait { - (void)stripAnonymity { @synchronized ([self lock]) { if ([PFAnonymousUtils isLinkedWithUser:self]) { - NSString *authType = PFUserAnonymousAuthenticationType; + NSString *authType = PFAnonymousUserAuthenticationType; [self.linkedServiceNames removeObject:authType]; @@ -570,7 +570,7 @@ - (void)stripAnonymity { - (void)restoreAnonymity:(id)anonymousData { @synchronized ([self lock]) { if (anonymousData && anonymousData != [NSNull null]) { - NSString *authType = PFUserAnonymousAuthenticationType; + NSString *authType = PFAnonymousUserAuthenticationType; [self.linkedServiceNames addObject:authType]; self.authData[authType] = anonymousData; } @@ -852,7 +852,7 @@ - (BFTask *)linkWithAuthTypeInBackground:(NSString *)authType authData:(NSDictio self.authData[authType] = newAuthData; [self.linkedServiceNames addObject:authType]; - oldAnonymousData = self.authData[PFUserAnonymousAuthenticationType]; + oldAnonymousData = self.authData[PFAnonymousUserAuthenticationType]; [self stripAnonymity]; dirty = YES; @@ -1177,7 +1177,7 @@ - (void)signUpInBackgroundWithBlock:(PFBooleanResultBlock)block { // For anonymous users, there may be an objectId. Setting the userName // will have removed the anonymous link and set the value in the authData // object to [NSNull null], so we can just treat it like a save operation. - if (authData[PFUserAnonymousAuthenticationType] == [NSNull null]) { + if (authData[PFAnonymousUserAuthenticationType] == [NSNull null]) { [self saveInBackgroundWithBlock:block]; return; } @@ -1207,7 +1207,7 @@ + (PFObjectState *)_newObjectStateWithParseClassName:(NSString *)className - (BFTask *)_validateSaveEventuallyAsync { if ([self isDirtyForKey:PFUserPasswordRESTKey]) { NSError *error = [PFErrorUtilities errorWithCode:kPFErrorOperationForbidden - message:@"Unable to saveEventually a PFUser with dirty password."]; + message:@"Unable to saveEventually a PFUser with dirty password."]; return [BFTask taskWithError:error]; } return [BFTask taskWithResult:nil]; diff --git a/Parse/PFUserAuthenticationDelegate.h b/Parse/PFUserAuthenticationDelegate.h index 0a04a899a..d51c1078d 100644 --- a/Parse/PFUserAuthenticationDelegate.h +++ b/Parse/PFUserAuthenticationDelegate.h @@ -9,8 +9,6 @@ #import -#import - #import PF_ASSUME_NONNULL_BEGIN @@ -31,7 +29,7 @@ PF_ASSUME_NONNULL_BEGIN @returns `YES` - if the `authData` was succesfully synchronized, or `NO` if user should not longer be associated because of bad `authData`. */ -- (BOOL)restoreAuthenticationWithAuthData:(PF_NULLABLE NSDictionary PF_GENERIC(NSString *,NSString *) *)authData; +- (BOOL)restoreAuthenticationWithAuthData:(PF_NULLABLE NSDictionary PF_GENERIC(NSString *, NSString *) *)authData; @end