Skip to content

Commit

Permalink
Merge pull request #89 from auth0/chore-improve-idp-support
Browse files Browse the repository at this point in the history
Add extra method to call social auth with strategy name.
  • Loading branch information
hzalaz committed Mar 14, 2015
2 parents 1f67207 + 140f7bc commit b3efa90
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
17 changes: 17 additions & 0 deletions Lock/Tests/A0IdentityProviderAuthenticatorSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ @interface A0IdentityProviderAuthenticator (TestAPI)

});

context(@"authenticate with known strategy name", ^{

void(^failureBlock)(NSError *) = ^(NSError *error) {};
beforeEach(^{
[authenticator authenticateForStrategyName:strategy.name parameters:nil success:successBlock failure:failureBlock];
});

it(@"should call the correct provider", ^{
[MKTVerify(provider) authenticateWithParameters:nil success:successBlock failure:failureBlock];
});

it(@"should tell it can authenticate", ^{
expect([authenticator canAuthenticateStrategy:strategy]).to.beTruthy();
});

});

context(@"authenticate with unknown strategy", ^{

__block A0Strategy *unknown;
Expand Down
15 changes: 15 additions & 0 deletions Pod/Classes/Provider/A0IdentityProviderAuthenticator.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@
success:(void(^)(A0UserProfile *profile, A0Token *token))success
failure:(void(^)(NSError *error))failure;

/**
* Authenticates a user using an identity provider specified by `A0Strategy`'s name and the registered method (Safari or Native).
* For the connection name it will use the first one by default.
* You can override the default connection name setting in parameters the key `connection` with the name of the connection that should be used instead.
*
* @param strategy object that represent an authentication strategy with an identity provider.
* @param parameters authentication parameters for Auth0 API.
* @param success block called on successful authentication with user's token info and profile
* @param failure block called on error with the reason as a parameter
*/
- (void)authenticateForStrategyName:(NSString *)strategyName
parameters:(A0AuthParameters *)parameters
success:(void(^)(A0UserProfile *profile, A0Token *token))success
failure:(void(^)(NSError *error))failure;

/**
* Checks if the given startegy has a registered authenticator (either Native or Safari).
*
Expand Down
21 changes: 14 additions & 7 deletions Pod/Classes/Provider/A0IdentityProviderAuthenticator.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,28 @@ - (BOOL)canAuthenticateStrategy:(A0Strategy *)strategy {
return authenticator != nil;
}

- (void)authenticateForStrategy:(A0Strategy *)strategy
parameters:(A0AuthParameters *)parameters
success:(void(^)(A0UserProfile *profile, A0Token *token))success
failure:(void(^)(NSError *error))failure {
id<A0AuthenticationProvider> authenticator = self.authenticators[strategy.name];
- (void)authenticateForStrategyName:(NSString *)strategyName
parameters:(A0AuthParameters *)parameters
success:(void (^)(A0UserProfile *, A0Token *))success
failure:(void (^)(NSError *))failure {
id<A0AuthenticationProvider> authenticator = self.authenticators[strategyName];
if (authenticator) {
[authenticator authenticateWithParameters:parameters success:success failure:failure];
} else {
A0LogWarn(@"No known provider for strategy %@", strategy.name);
A0LogWarn(@"No known provider for strategy %@", strategyName);
if (failure) {
failure([A0Errors unkownProviderForStrategy:strategy.name]);
failure([A0Errors unkownProviderForStrategy:strategyName]);
}
}
}

- (void)authenticateForStrategy:(A0Strategy *)strategy
parameters:(A0AuthParameters *)parameters
success:(void(^)(A0UserProfile *profile, A0Token *token))success
failure:(void(^)(NSError *error))failure {
[self authenticateForStrategyName:strategy.name parameters:parameters success:success failure:failure];
}

- (BOOL)handleURL:(NSURL *)url sourceApplication:(NSString *)application {
__block BOOL handled = NO;
[self.authenticators enumerateKeysAndObjectsUsingBlock:^(NSString *key, id<A0AuthenticationProvider> authenticator, BOOL *stop) {
Expand Down

0 comments on commit b3efa90

Please sign in to comment.