Skip to content

Commit

Permalink
Merge pull request #260 from ParsePlatform/nlutsenko.cleanup
Browse files Browse the repository at this point in the history
Cleanup PFUser third party authentication.
  • Loading branch information
nlutsenko committed Sep 15, 2015
2 parents 5c5c887 + f0c0c90 commit d8ed91b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@interface PFUserAuthenticationController () {
dispatch_queue_t _dataAccessQueue;
NSMutableDictionary *_authenticationProviders;
NSMutableDictionary PF_GENERIC(NSString *, id<PFUserAuthenticationDelegate>) *_authenticationDelegates;
}

@end
Expand All @@ -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;
}
Expand All @@ -47,16 +47,16 @@ - (instancetype)init {

- (void)registerAuthenticationDelegate:(id<PFUserAuthenticationDelegate>)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];
}
}
Expand All @@ -66,7 +66,7 @@ - (void)unregisterAuthenticationDelegateForAuthType:(NSString *)authType {
return;
}
dispatch_sync(_dataAccessQueue, ^{
[_authenticationProviders removeObjectForKey:authType];
[_authenticationDelegates removeObjectForKey:authType];
});
}

Expand All @@ -75,23 +75,19 @@ - (void)unregisterAuthenticationDelegateForAuthType:(NSString *)authType {
return nil;
}

__block id<PFUserAuthenticationDelegate> provider = nil;
__block id<PFUserAuthenticationDelegate> 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<PFUserAuthenticationDelegate> provider = [self authenticationDelegateForAuthType:authType];
if (!provider) {
return [BFTask taskWithResult:@YES];
Expand All @@ -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
///--------------------------------------
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

NS_ASSUME_NONNULL_BEGIN

extern NSString *const PFUserAnonymousAuthenticationType;
extern NSString *const PFAnonymousUserAuthenticationType;

@interface PFAnonymousAuthenticationProvider : NSObject <PFUserAuthenticationDelegate>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#import <Bolts/BFTask.h>

NSString *const PFUserAnonymousAuthenticationType = @"anonymous";
NSString *const PFAnonymousUserAuthenticationType = @"anonymous";

@implementation PFAnonymousAuthenticationProvider

Expand Down
10 changes: 5 additions & 5 deletions Parse/PFAnonymousUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,7 +41,7 @@ + (void)logInWithTarget:(id)target selector:(SEL)selector {
///--------------------------------------

+ (BOOL)isLinkedWithUser:(PFUser *)user {
return [user isLinkedWithAuthType:PFUserAnonymousAuthenticationType];
return [user isLinkedWithAuthType:PFAnonymousUserAuthenticationType];
}

///--------------------------------------
Expand All @@ -65,15 +65,15 @@ + (PFAnonymousAuthenticationProvider *)_authenticationProvider {
provider = authenticationProvider_;
if (!provider) {
provider = [[PFAnonymousAuthenticationProvider alloc] init];
[PFUser registerAuthenticationDelegate:provider forAuthType:PFUserAnonymousAuthenticationType];
[PFUser registerAuthenticationDelegate:provider forAuthType:PFAnonymousUserAuthenticationType];
authenticationProvider_ = provider;
}
});
return provider;
}

+ (void)_clearAuthenticationProvider {
[PFUser _unregisterAuthenticationDelegateForAuthType:PFUserAnonymousAuthenticationType];
[PFUser _unregisterAuthenticationDelegateForAuthType:PFAnonymousUserAuthenticationType];
dispatch_sync([self _providerAccessQueue], ^{
authenticationProvider_ = nil;
});
Expand All @@ -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
20 changes: 10 additions & 10 deletions Parse/PFUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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 {
Expand All @@ -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];
}

Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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];

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 1 addition & 3 deletions Parse/PFUserAuthenticationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#import <Foundation/Foundation.h>

#import <Bolts/BFTask.h>

#import <Parse/PFConstants.h>

PF_ASSUME_NONNULL_BEGIN
Expand All @@ -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

Expand Down

0 comments on commit d8ed91b

Please sign in to comment.