Skip to content

Commit

Permalink
Revert "Added iOS SF/ASWeb AuthenticationSession support (auth0#187)"
Browse files Browse the repository at this point in the history
This reverts commit 14efe5f.
  • Loading branch information
nfcampos authored Mar 12, 2019
1 parent a7ecdfb commit cea775f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 73 deletions.
82 changes: 15 additions & 67 deletions ios/A0Auth0.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#import "A0Auth0.h"

#import <SafariServices/SafariServices.h>
#if __has_include("AuthenticationServices/AuthenticationServices.h")
#import <AuthenticationServices/AuthenticationServices.h>
#endif
#import <CommonCrypto/CommonCrypto.h>

#if __has_include("RCTUtils.h")
Expand All @@ -13,12 +10,8 @@
#import <React/RCTUtils.h>
#endif

#define ERROR_CANCELLED @{@"error": @"a0.session.user_cancelled",@"error_description": @"User cancelled the Auth"}
#define ERROR_FAILED_TO_LOAD @{@"error": @"a0.session.failed_load",@"error_description": @"Failed to load url"}

@interface A0Auth0 () <SFSafariViewControllerDelegate>
@property (weak, nonatomic) SFSafariViewController *last;
@property (strong, nonatomic) NSObject *authenticationSession;
@property (copy, nonatomic) RCTResponseSenderBlock sessionCallback;
@property (assign, nonatomic) BOOL closeOnLoad;
@end
Expand All @@ -37,15 +30,9 @@ - (dispatch_queue_t)methodQueue
}

RCT_EXPORT_METHOD(showUrl:(NSString *)urlString closeOnLoad:(BOOL)closeOnLoad callback:(RCTResponseSenderBlock)callback) {
if (@available(iOS 11.0, *)) {
self.sessionCallback = callback;
self.closeOnLoad = closeOnLoad;
[self presentAuthenticationSession:[NSURL URLWithString:urlString]];
} else {
[self presentSafariWithURL:[NSURL URLWithString:urlString]];
self.sessionCallback = callback;
self.closeOnLoad = closeOnLoad;
}
[self presentSafariWithURL:[NSURL URLWithString:urlString]];
self.closeOnLoad = closeOnLoad;
self.sessionCallback = callback;
}

RCT_EXPORT_METHOD(oauthParameters:(RCTResponseSenderBlock)callback) {
Expand All @@ -71,64 +58,17 @@ - (void)presentSafariWithURL:(NSURL *)url {
self.last = controller;
}

- (void)presentAuthenticationSession:(NSURL *)url {

NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:NO];
NSArray *queryItems = urlComponents.queryItems;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name=%@", @"redirect_uri"];
NSURLQueryItem *queryItem = [[queryItems
filteredArrayUsingPredicate:predicate]
firstObject];
NSString *callbackURLScheme = queryItem.value;
RCTResponseSenderBlock callback = self.sessionCallback ? self.sessionCallback : ^void(NSArray *_unused) {};

if (@available(iOS 12.0, *)) {
self.authenticationSession = [[ASWebAuthenticationSession alloc]
initWithURL:url callbackURLScheme:callbackURLScheme
completionHandler:^(NSURL * _Nullable callbackURL,
NSError * _Nullable error) {
if ([[error domain] isEqualToString:ASWebAuthenticationSessionErrorDomain] &&
[error code] == ASWebAuthenticationSessionErrorCodeCanceledLogin) {
callback(@[ERROR_CANCELLED, [NSNull null]]);
} else if(error) {
callback(@[error, [NSNull null]]);
} else if(callbackURL) {
callback(@[[NSNull null], callbackURL.absoluteString]);
}
self.authenticationSession = nil;
}];
[(ASWebAuthenticationSession*) self.authenticationSession start];
} else if (@available(iOS 11.0, *)) {
self.authenticationSession = [[SFAuthenticationSession alloc]
initWithURL:url callbackURLScheme:callbackURLScheme
completionHandler:^(NSURL * _Nullable callbackURL,
NSError * _Nullable error) {
if ([[error domain] isEqualToString:SFAuthenticationErrorDomain] &&
[error code] == SFAuthenticationErrorCanceledLogin) {
callback(@[ERROR_CANCELLED, [NSNull null]]);
} else if(error) {
callback(@[error, [NSNull null]]);
} else if(callbackURL) {
callback(@[[NSNull null], callbackURL.absoluteString]);
}
self.authenticationSession = nil;
}];
[(SFAuthenticationSession*) self.authenticationSession start];
}
}

- (void)terminateWithError:(id)error dismissing:(BOOL)dismissing animated:(BOOL)animated {
RCTResponseSenderBlock callback = self.sessionCallback ? self.sessionCallback : ^void(NSArray *_unused) {};
if (dismissing) {
[self.last.presentingViewController dismissViewControllerAnimated:animated
completion:^{
if (error) {
callback(@[error, [NSNull null]]);
callback(@[error]);
}
}];
} else if (error) {
callback(@[error, [NSNull null]]);
callback(@[error]);
}
self.sessionCallback = nil;
self.last = nil;
Expand Down Expand Up @@ -182,14 +122,22 @@ - (NSDictionary *)generateOAuthParameters {
#pragma mark - SFSafariViewControllerDelegate

- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
[self terminateWithError:ERROR_CANCELLED dismissing:NO animated:NO];
NSDictionary *error = @{
@"error": @"a0.session.user_cancelled",
@"error_description": @"User cancelled the Auth"
};
[self terminateWithError:error dismissing:NO animated:NO];
}

- (void)safariViewController:(SFSafariViewController *)controller didCompleteInitialLoad:(BOOL)didLoadSuccessfully {
if (self.closeOnLoad && didLoadSuccessfully) {
[self terminateWithError:[NSNull null] dismissing:YES animated:YES];
} else if (!didLoadSuccessfully) {
[self terminateWithError:ERROR_FAILED_TO_LOAD dismissing:YES animated:YES];
NSDictionary *error = @{
@"error": @"a0.session.failed_load",
@"error_description": @"Failed to load url"
};
[self terminateWithError:error dismissing:YES animated:YES];
}
}

Expand Down
10 changes: 4 additions & 6 deletions webauth/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ export default class Agent {
resolve(event.url);
};
Linking.addEventListener('url', urlHandler);
NativeModules.A0Auth0.showUrl(url, closeOnLoad, (error, redirectURL) => {
NativeModules.A0Auth0.showUrl(url, closeOnLoad, err => {
Linking.removeEventListener('url', urlHandler);
if (error) {
reject(error);
} else if(redirectURL) {
resolve(redirectURL);
} else if(closeOnLoad) {
if (err) {
reject(err);
} else if (closeOnLoad) {
resolve();
}
});
Expand Down

0 comments on commit cea775f

Please sign in to comment.