Skip to content

Commit

Permalink
Let handle redirect url methods return whether the url can be handled
Browse files Browse the repository at this point in the history
This is needed on iOS to properly return the value in app delegate method.
  • Loading branch information
Xuyan Ke committed Aug 22, 2020
1 parent 949b05f commit 42d88e2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ To handle the redirection back into the Objective-C SDK once the authentication
}
}
};
[DBClientsManager handleRedirectURL:url completion:completion];
return NO;
BOOL canHandle = [DBClientsManager handleRedirectURL:url completion:completion];
return canHandle;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ - (BOOL)checkAndPresentPlatformSpecificAuth:(id<DBSharedApplication>)sharedAppli
return NO;
}

- (void)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion {
[super handleRedirectURL:url
completion:^(DBOAuthResult *result) {
- (BOOL)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion {
return [super handleRedirectURL:url
completion:^(DBOAuthResult *result) {
[[DBMobileSharedApplication mobileSharedApplication] dismissAuthController];
completion(result);
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ NS_ASSUME_NONNULL_BEGIN
/// @param url The auth redirect url which relaunches the SDK.
/// @param completion Completion block to pass back authorization result.
///
+ (void)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion;
/// @return Whether the URL can be handled.
///
+ (BOOL)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion;

///
/// Handles launching the SDK with a redirect url from an external source to authorize a team API client.
Expand All @@ -103,7 +105,9 @@ NS_ASSUME_NONNULL_BEGIN
/// @param url The auth redirect url which relaunches the SDK.
/// @param completion Completion block to pass back authorization result.
///
+ (void)handleRedirectURLTeam:(NSURL *)url completion:(DBOAuthCompletion)completion;
/// @return Whether the URL can be handled.
///
+ (BOOL)handleRedirectURLTeam:(NSURL *)url completion:(DBOAuthCompletion)completion;

///
/// Multi-Dropbox account use case. Sets to `nil` the active user / team shared authorized client, clears the stored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ + (void)setupWithOAuthManagerTeam:(DBOAuthManager *)oAuthManager
[self db_setupAuthorizedTeamClients];
}

+ (void)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion {
[self db_handleRedirectURL:url isTeam:NO completion:completion];
+ (BOOL)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion {
return [self db_handleRedirectURL:url isTeam:NO completion:completion];
}

+ (void)handleRedirectURLTeam:(NSURL *)url completion:(DBOAuthCompletion)completion {
[self db_handleRedirectURL:url isTeam:YES completion:completion];
+ (BOOL)handleRedirectURLTeam:(NSURL *)url completion:(DBOAuthCompletion)completion {
return [self db_handleRedirectURL:url isTeam:YES completion:completion];
}

+ (void)unlinkAndResetClient:(NSString *)tokenUid {
Expand Down Expand Up @@ -294,12 +294,12 @@ + (void)db_resetClient:(NSString *)tokenUid {
}
}

+ (void)db_handleRedirectURL:(NSURL *)url isTeam:(BOOL)isTeam completion:(DBOAuthCompletion)completion {
+ (BOOL)db_handleRedirectURL:(NSURL *)url isTeam:(BOOL)isTeam completion:(DBOAuthCompletion)completion {
NSAssert([DBOAuthManager sharedOAuthManager],
@"Call the appropriate `[DBClientsManager setupWith...]` before calling this method");

[[DBOAuthManager sharedOAuthManager] handleRedirectURL:url
completion:^(DBOAuthResult *result) {
return [[DBOAuthManager sharedOAuthManager] handleRedirectURL:url
completion:^(DBOAuthResult *result) {
if ([result isSuccess]) {
DBAccessToken *token = result.accessToken;
if (isTeam) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ NS_ASSUME_NONNULL_BEGIN
/// @param completion Completion block for oauth result, called with `nil` if SDK cannot handle the redirect URL,
/// otherwise an instance of `DBOAuthResult`.
///
- (void)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion;
/// @return Whether the URL can be handled.
///
- (BOOL)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion;

#pragma mark - Keychain methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,26 @@ - (instancetype)initWithAppKey:(NSString *)appKey host:(NSString *)host redirect

#pragma mark - Auth flow methods

- (void)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion {
- (BOOL)handleRedirectURL:(NSURL *)url completion:(DBOAuthCompletion)completion {
// check if url is a cancel url
if (([[url host] isEqualToString:@"1"] && [[url path] isEqualToString:@"/cancel"]) ||
([[url host] isEqualToString:@"2"] && [[url path] isEqualToString:@"/cancel"])) {
completion([[DBOAuthResult alloc] initWithCancel]);
} else if (![self canHandleURL:url]) {
completion(nil);
} else {
return YES;
}

if ([self canHandleURL:url]) {
[self extractFromUrl:url
completion:^(DBOAuthResult *result) {
if ([result isSuccess]) {
[self storeAccessToken:result.accessToken];
}
completion(result);
}];
if ([result isSuccess]) {
[self storeAccessToken:result.accessToken];
}
completion(result);
}];
return YES;
} else {
completion(nil);
return NO;
}
}

Expand Down
14 changes: 9 additions & 5 deletions TestObjectiveDropbox/TestObjectiveDropbox_iOS/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
BOOL urlHandled = NO;
if ([[url absoluteString] containsString:@"openWith"]) {
NSLog(@"Successfully retrieved openWith url");

Expand All @@ -101,11 +102,12 @@ - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

DBOpenWithInfo *openWithInfo = [connector openWithInfoFromURL:url];
[((ViewController *)self.window.rootViewController) setOpenWithInfoNSURL:openWithInfo];
urlHandled = YES;
} else {
[self db_handleAuthUrl:url];
urlHandled = [self db_handleAuthUrl:url];
}

return NO;
return urlHandled;
}

- (void)applicationWillResignActive:(UIApplication *)application {
Expand Down Expand Up @@ -138,7 +140,7 @@ - (void)applicationWillTerminate:(UIApplication *)application {
// applicationDidEnterBackground:.
}

- (void)db_handleAuthUrl:(NSURL *)url {
- (BOOL)db_handleAuthUrl:(NSURL *)url {
DBOAuthCompletion completion = ^(DBOAuthResult *authResult) {
if (authResult != nil) {
if ([authResult isSuccess]) {
Expand All @@ -152,16 +154,18 @@ - (void)db_handleAuthUrl:(NSURL *)url {
[((ViewController *)self.window.rootViewController) checkButtons];
};

BOOL handled = NO;
switch (appPermission) {
case FullDropbox: {
[DBClientsManager handleRedirectURL:url completion: completion];
handled = [DBClientsManager handleRedirectURL:url completion:completion];
break;
}
case TeamMemberFileAccess:
case TeamMemberManagement:
[DBClientsManager handleRedirectURLTeam:url completion: completion];
handled = [DBClientsManager handleRedirectURLTeam:url completion:completion];
break;
}
return handled;
}

@end

0 comments on commit 42d88e2

Please sign in to comment.