Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate A0APIClient & A0IdentityProviderAuthenticator singletons #115

Merged
merged 6 commits into from
May 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lock/Lock/A0AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import <Fabric/Fabric.h>
#import <Crashlytics/Crashlytics.h>
#endif
#import "A0LockApplication.h"

@implementation A0AppDelegate

Expand All @@ -48,6 +49,6 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[A0IdentityProviderAuthenticator sharedInstance] handleURL:url sourceApplication:sourceApplication];
return [[[A0LockApplication sharedInstance] lock] handleURL:url sourceApplication:sourceApplication];
}
@end
23 changes: 7 additions & 16 deletions Lock/Lock/A0HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ - (void)viewDidLoad {

- (void)loginNative:(id)sender {
[self.keychain clearAll];
A0LockViewController *controller = [[A0LockViewController alloc] init];
A0LockViewController *controller = [[[A0LockApplication sharedInstance] lock] newLockViewController];
@weakify(self);
controller.lock = [[A0LockApplication sharedInstance] lock];
controller.closable = YES;
controller.loginAfterSignUp = YES;
controller.usesEmail = YES;
Expand All @@ -102,9 +101,9 @@ - (void)loginNative:(id)sender {

- (void)loginTouchID:(id)sender {
[self.keychain clearAll];
A0TouchIDLockViewController *controller = [[A0TouchIDLockViewController alloc] init];
A0Lock *lock = [[A0LockApplication sharedInstance] lock];
A0TouchIDLockViewController *controller = [lock newTouchIDViewController];
controller.closable = YES;
controller.lock = [[A0LockApplication sharedInstance] lock];
@weakify(self);
controller.onAuthenticationBlock = ^(A0UserProfile *profile, A0Token *token) {
NSLog(@"SUCCESS %@", profile);
Expand All @@ -116,17 +115,13 @@ - (void)loginTouchID:(id)sender {
[self performSegueWithIdentifier:@"LoggedIn" sender:self];
}];
};
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
navController.modalPresentationStyle = UIModalPresentationFormSheet;
}
[self presentViewController:navController animated:YES completion:nil];
[lock presentTouchIDController:controller fromController:self];
}

- (void)loginSMS:(id)sender {
[self.keychain clearAll];
A0SMSLockViewController *controller = [[A0SMSLockViewController alloc] init];
controller.lock = [[A0LockApplication sharedInstance] lock];
A0Lock *lock = [[A0LockApplication sharedInstance] lock];
A0SMSLockViewController *controller = [lock newSMSViewController];
controller.closable = YES;
@weakify(self);
controller.auth0APIToken = ^{
Expand All @@ -144,10 +139,6 @@ - (void)loginSMS:(id)sender {
[self performSegueWithIdentifier:@"LoggedIn" sender:self];
}];
};
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
navController.modalPresentationStyle = UIModalPresentationFormSheet;
}
[self presentViewController:navController animated:YES completion:nil];
[lock presentSMSController:controller fromController:self];
}
@end
15 changes: 6 additions & 9 deletions Lock/Lock/A0LockApplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,17 @@ @implementation A0LockApplication
- (instancetype)init {
self = [super init];
if (self) {
_lock = [A0Lock new];
_lock = [A0Lock newLock];
A0TwitterAuthenticator *twitter = [A0TwitterAuthenticator newAuthenticatorWithKey:@""
andSecret:@""];
twitter.clientProvider = _lock;
A0FacebookAuthenticator *facebook = [A0FacebookAuthenticator newAuthenticatorWithDefaultPermissions];
facebook.clientProvider = _lock;
NSString *googlePlusClientId = [[NSBundle mainBundle] infoDictionary][@"GooglePlusClientId"];
A0GooglePlusAuthenticator *googleplus = [A0GooglePlusAuthenticator newAuthenticatorWithClientId:googlePlusClientId];
googleplus.clientProvider = _lock;
[[A0IdentityProviderAuthenticator sharedInstance] registerAuthenticationProviders:@[
twitter,
facebook,
googleplus,
]];
[_lock registerAuthenticators:@[
twitter,
facebook,
googleplus,
]];
}
return self;
}
Expand Down
4 changes: 3 additions & 1 deletion Lock/Lock/A0SettingsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import <SimpleKeychain/A0SimpleKeychain.h>
#import <TouchIDAuth/A0TouchIDAuthentication.h>
#import <Lock/Lock.h>
#import "A0LockApplication.h"

@interface A0SettingsViewController ()

Expand Down Expand Up @@ -63,7 +64,8 @@ - (void)viewDidLoad {

- (IBAction)clearKeychain:(id)sender {
[[A0SimpleKeychain keychainWithService:@"Auth0"] clearAll];
[[A0IdentityProviderAuthenticator sharedInstance] clearSessions];
A0IdentityProviderAuthenticator *authenticator = [[[A0LockApplication sharedInstance] lock] identityProviderAuthenticator];
[authenticator clearSessions];
}

- (IBAction)clearTouchID:(id)sender {
Expand Down
22 changes: 11 additions & 11 deletions Lock/Tests/A0IdentityProviderAuthenticatorSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ @interface A0IdentityProviderAuthenticator (TestAPI)

it(@"should fail with provider with no identifier", ^{
expect(^{
[authenticator registerAuthenticationProvider:mockProtocol(@protocol(A0AuthenticationProvider))];
[authenticator registerAuthenticationProvider:mock(A0BaseAuthenticator.class)];
}).to.raiseWithReason(NSInternalInconsistencyException, @"Provider must have a valid indentifier");
});

Expand All @@ -81,7 +81,7 @@ @interface A0IdentityProviderAuthenticator (TestAPI)
__block id<A0AuthenticationProvider> facebookProvider;

beforeEach(^{
facebookProvider = mockProtocol(@protocol(A0AuthenticationProvider));
facebookProvider = mock(A0BaseAuthenticator.class);
[given([facebookProvider identifier]) willReturn:kFBProviderId];
[authenticator registerAuthenticationProvider:facebookProvider];
});
Expand All @@ -95,9 +95,9 @@ @interface A0IdentityProviderAuthenticator (TestAPI)
__block id<A0AuthenticationProvider> twitterProvider;

beforeEach(^{
facebookProvider = mockProtocol(@protocol(A0AuthenticationProvider));
facebookProvider = mock(A0BaseAuthenticator.class);
[given([facebookProvider identifier]) willReturn:kFBProviderId];
twitterProvider = mockProtocol(@protocol(A0AuthenticationProvider));
twitterProvider = mock(A0BaseAuthenticator.class);
[given([twitterProvider identifier]) willReturn:kTwitterProviderId];

[authenticator registerAuthenticationProviders:@[facebookProvider, twitterProvider]];
Expand All @@ -117,9 +117,9 @@ @interface A0IdentityProviderAuthenticator (TestAPI)
__block id<A0AuthenticationProvider> twitterProvider;

beforeEach(^{
facebookProvider = mockProtocol(@protocol(A0AuthenticationProvider));
facebookProvider = mock(A0BaseAuthenticator.class);
[given([facebookProvider identifier]) willReturn:kFBProviderId];
twitterProvider = mockProtocol(@protocol(A0AuthenticationProvider));
twitterProvider = mock(A0BaseAuthenticator.class);
[given([twitterProvider identifier]) willReturn:kTwitterProviderId];
application = mock(A0Application.class);
facebookStrategy = mock(A0Strategy.class);
Expand Down Expand Up @@ -153,7 +153,7 @@ @interface A0IdentityProviderAuthenticator (TestAPI)
void(^successBlock)(A0UserProfile *, A0Token *) = ^(A0UserProfile *profile, A0Token *token) {};

beforeEach(^{
provider = mockProtocol(@protocol(A0AuthenticationProvider));
provider = mock(A0BaseAuthenticator.class);
[given([provider identifier]) willReturn:@"provider"];
strategy = mock(A0Strategy.class);
[given([strategy name]) willReturn:@"provider"];
Expand Down Expand Up @@ -234,8 +234,8 @@ @interface A0IdentityProviderAuthenticator (TestAPI)
NSURL *twitterURL = [NSURL URLWithString:@"twitter://handler"];

beforeEach(^{
facebook = mockProtocol(@protocol(A0AuthenticationProvider));
twitter = mockProtocol(@protocol(A0AuthenticationProvider));
facebook = mock(A0BaseAuthenticator.class);
twitter = mock(A0BaseAuthenticator.class);
[given([facebook handleURL:facebookURL sourceApplication:nil]) willReturnBool:YES];
[given([twitter handleURL:twitterURL sourceApplication:nil]) willReturnBool:YES];
authenticator.authenticators = [@{ @"facebook": facebook, @"twitter": twitter } mutableCopy];
Expand Down Expand Up @@ -302,8 +302,8 @@ @interface A0IdentityProviderAuthenticator (TestAPI)
__block id<A0AuthenticationProvider> twitter;

beforeEach(^{
facebook = mockProtocol(@protocol(A0AuthenticationProvider));
twitter = mockProtocol(@protocol(A0AuthenticationProvider));
facebook = mock(A0BaseAuthenticator.class);
twitter = mock(A0BaseAuthenticator.class);
authenticator.authenticators = [@{ @"facebook": facebook, @"twitter": twitter } mutableCopy];
});

Expand Down
10 changes: 6 additions & 4 deletions Pod/Classes/Core/A0APIClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ typedef void(^A0APIClientDelegationSuccess)(A0Token *tokenInfo);
/**
* Returns a shared instance of `A0APIClient`. This instance is initialised with clientId and tenant from Info plist file entries. These entries are `Auth0ClientId` and `Auth0Tenant`.
* It can also be instantiated with a custom domain instead of Auth0's.
* We recommend keeping yourself the `A0APIClient` instead instead of relying in this singleton, for this reason this method is deprecated.
* @deprecated 1.12.0
* We recommend keeping yourself the `A0APIClient` instead instead of relying in this singleton, for this reason this method is deprecated and we suggest using A0Lock class to obtain an instance of this class.
* @deprecated 1.12.0. We recommend creating an instance of A0Lock and call its method `-apiClient` to obtain an instance of this object.
* @return a shared `A0APIClient` instance.
*/
+ (instancetype)sharedClient __attribute__((deprecated));
Expand Down Expand Up @@ -346,11 +346,13 @@ typedef void(^A0APIClientDelegationSuccess)(A0Token *tokenInfo);
@interface A0APIClient (Deprecated)

/**
* Initialise the Client with Auth0's app client ID and tenant name
* Initialise the Client with Auth0's app client ID and tenant name.
*
* @param clientId app's client ID.
* @param tenant app's tenant name
*
* @deprecated 1.12.0
* @deprecated 1.12.0. Use domain & clientId to build and instance of just use A0Lock class
* @see A0Lock
* @return a new `A0APIClient` instance
*/
- (instancetype)initWithClientId:(NSString *)clientId andTenant:(NSString *)tenant;
Expand Down
31 changes: 31 additions & 0 deletions Pod/Classes/Core/A0AuthenticatorProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// A0AuthenticatorProvider.h
//
// Copyright (c) 2015 Auth0 (http://auth0.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <Foundation/Foundation.h>

@class A0IdentityProviderAuthenticator;

@protocol A0AuthenticatorProvider <NSObject>

- (A0IdentityProviderAuthenticator *)identityProviderAuthenticator;

@end
57 changes: 42 additions & 15 deletions Pod/Classes/Core/A0Lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@

#import <Foundation/Foundation.h>
#import "A0APIClientProvider.h"
#import "A0AuthenticatorProvider.h"

@class A0APIClient, A0UserAPIClient;
@class A0APIClient, A0UserAPIClient, A0IdentityProviderAuthenticator;

/**
* Main interface with Auth0 Lock for iOS.
*/
@interface A0Lock : NSObject<A0APIClientProvider>
@interface A0Lock : NSObject<A0APIClientProvider, A0AuthenticatorProvider>

/**
* Auth0 account's client identifier
Expand All @@ -44,19 +45,7 @@
@property (strong, readonly, nonatomic) NSURL *configurationURL;

/**
* Initialise a new instance with values from NSBundle
* The valid keys are the following:

* ClientId: "Auth0ClientId"
* Tenant: "Auth0Tenant"
* Domain: "Auth0Domain"
* Config Domain: "Auth0ConfigurationDomain"
*
* It can be any of these configurations:
* 1. Domain + Domain Config + Client Id
* 2. Domain + Client Id
* 3. Tenant + Client Id
* The order also determines the precende, so if (1) and (2) are found in the dictionary, the option (1) will be used instead of (2).
* Initialise a new instance with values from Info.plist
*
* @return an instance of A0Lock
*/
Expand Down Expand Up @@ -111,6 +100,17 @@
configurationDomain:(NSString *)configurationDomain;


/**
* Creates a new instance of Lock using information stored in your Info.plist.
* These are the the valid entries:
* - Auth0ClientId: Your app's client identifier in Auth0.
* - Auth0Domain: Your app's domain name or url in Auth0. e.g: samples.auth0.com or https://samples.auth0.com
* - Auth0ConfigurationDomain: Your app's configuration domain name or url where we get yout app configuration. This value is optional and will default to Auth0 CDN.
*
* @return a new instance
*/
+ (instancetype)newLock;

/**
* Auth0 Authentication API client.
*
Expand All @@ -127,4 +127,31 @@
*/
- (A0UserAPIClient *)newUserAPIClientWithIdToken:(NSString *)idToken;

/**
* Handle URL received from AppDelegate when app is called from a third party app at the end of an authentication flow.
*
*
* @param url url used by third party app to call the application
* @param sourceApplication caller name
*
* @return if we can handle the url or not.
*/
- (BOOL)handleURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication;

/**
* Register IdP authenticator that will be used for Social & Enterprise connections.
* By default all Social & Enterprise authentications are performed by using the web flow with Safari but you can plug
* your own authenticator for a connection. e.g.: you can register A0FacebookAuthenticator in order to login with FB native SDK.
*
* @param authenticators list of authenticators to register. Must be subclasses of A0BaseAuthenticator
* @see A0BaseAuthenticator
*/
- (void)registerAuthenticators:(NSArray *)authenticators;

/**
* Remove all stored sessions of any IdP in your application.
* If the user logged in using Safari, those sessions will not be cleaned.
*/
- (void)clearSessions;

@end
23 changes: 23 additions & 0 deletions Pod/Classes/Core/A0Lock.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "A0APIv1Router.h"
#import "A0APIClient.h"
#import "A0UserAPIClient.h"
#import "A0IdentityProviderAuthenticator.h"

#define kCDNConfigurationURL @"https://cdn.auth0.com"
#define kEUCDNConfigurationURL @"https://cdn.eu.auth0.com"
Expand Down Expand Up @@ -51,6 +52,7 @@ + (instancetype)URLWithAuth0Domain:(NSString *)domain {
@interface A0Lock ()
@property (strong, nonatomic) id<A0APIRouter> router;
@property (strong, nonatomic) A0APIClient *client;
@property (strong, nonatomic) A0IdentityProviderAuthenticator *authenticator;
@end

@implementation A0Lock
Expand Down Expand Up @@ -93,6 +95,7 @@ - (instancetype)initWithClientId:(NSString *)clientId domain:(NSString *)domain
A0LogDebug(@"Auth0 Lock initialised with clientId: (%@) domainURL: (%@) configurationURL: (%@)", clientId, domainURL, configurationURL);
_router = [[A0APIv1Router alloc] initWithClientId:clientId domainURL:domainURL configurationURL:configurationURL];
_client = [[A0APIClient alloc] initWithAPIRouter:_router];
_authenticator = [[A0IdentityProviderAuthenticator alloc] initWithLock:self];
}

return self;
Expand All @@ -102,6 +105,10 @@ - (A0APIClient *)apiClient {
return self.client;
}

- (A0IdentityProviderAuthenticator *)identityProviderAuthenticator {
return self.authenticator;
}

- (A0UserAPIClient *)newUserAPIClientWithIdToken:(NSString *)idToken {
return [[A0UserAPIClient alloc] initWithRouter:self.router idToken:idToken];
}
Expand All @@ -118,6 +125,22 @@ - (NSURL *)domainURL {
return [self.router endpointURL];
}

- (BOOL)handleURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication {
return [self.authenticator handleURL:url sourceApplication:sourceApplication];
}

- (void)registerAuthenticators:(NSArray *)authenticators {
[self.identityProviderAuthenticator registerAuthenticationProviders:authenticators];
}

- (void)clearSessions {
[self.identityProviderAuthenticator clearSessions];
}

+ (instancetype)newLock {
return [[A0Lock alloc] init];
}

+ (instancetype)newLockWithClientId:(NSString *)clientId domain:(NSString *)domain {
return [[A0Lock alloc] initWithClientId:clientId domain:domain];
}
Expand Down
Loading