Skip to content

Commit

Permalink
Merge pull request #333 from auth0/refactor-provider-resolver
Browse files Browse the repository at this point in the history
Refactor AuthProviderResolver [Breaking Change]
  • Loading branch information
hzalaz authored Sep 19, 2016
2 parents fdf8d75 + ae4eaaf commit 3552d05
Show file tree
Hide file tree
Showing 22 changed files with 264 additions and 223 deletions.
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies {
compile 'com.android.support:design:24.2.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup:otto:1.3.8'
compile 'com.auth0.android:auth0:1.0.0-rc.1'
compile 'com.auth0.android:auth0:1.0.0'
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'org.robolectric:robolectric:3.0'
Expand Down
13 changes: 7 additions & 6 deletions lib/src/main/java/com/auth0/android/lock/Lock.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@
import com.auth0.android.lock.LockCallback.LockEvent;
import com.auth0.android.lock.internal.Options;
import com.auth0.android.lock.internal.Theme;
import com.auth0.android.lock.provider.AuthProviderResolver;
import com.auth0.android.lock.provider.ProviderResolverManager;
import com.auth0.android.lock.provider.AuthResolver;
import com.auth0.android.lock.utils.CustomField;
import com.auth0.android.lock.utils.LockException;
import com.auth0.android.provider.AuthHandler;
import com.auth0.android.util.Telemetry;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -401,13 +402,13 @@ public Builder loginAfterSignUp(boolean login) {
}

/**
* Uses the given AuthProviderResolver to ask for Native IdentityProviders.
* Uses the given AuthHandlers to query for AuthProviders on a new authentication request.
*
* @param resolver the AuthProviderResolver to use
* @param handlers that Lock will query for AuthProviders.
* @return the current builder instance
*/
public Builder withProviderResolver(@NonNull AuthProviderResolver resolver) {
ProviderResolverManager.set(resolver);
public Builder withAuthHandlers(@NonNull AuthHandler... handlers) {
AuthResolver.setAuthHandlers(Arrays.asList(handlers));
return this;
}

Expand Down
11 changes: 6 additions & 5 deletions lib/src/main/java/com/auth0/android/lock/LockActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
import com.auth0.android.lock.events.SocialConnectionEvent;
import com.auth0.android.lock.internal.Configuration;
import com.auth0.android.lock.internal.Options;
import com.auth0.android.lock.provider.ProviderResolverManager;
import com.auth0.android.lock.internal.json.ApplicationFetcher;
import com.auth0.android.lock.internal.json.Connection;
import com.auth0.android.lock.provider.AuthResolver;
import com.auth0.android.lock.views.ClassicLockView;
import com.auth0.android.provider.AuthCallback;
import com.auth0.android.provider.AuthProvider;
Expand Down Expand Up @@ -220,9 +220,9 @@ public void run() {
}
};

private void fetchProviderAndBeginAuthentication(String connectionName) {
private void fetchProviderAndBeginAuthentication(@Nullable String strategyName, @NonNull String connectionName) {
Log.v(TAG, "Looking for a provider to use with the connection " + connectionName);
currentProvider = ProviderResolverManager.get().onAuthProviderRequest(this, authProviderCallback, connectionName);
currentProvider = AuthResolver.providerFor(strategyName, connectionName);
if (currentProvider != null) {
currentProvider.start(this, authProviderCallback, PERMISSION_REQUEST_CODE, CUSTOM_AUTH_REQUEST_CODE);
return;
Expand Down Expand Up @@ -289,7 +289,7 @@ public void onFetchApplicationRequest(FetchApplicationEvent event) {
@SuppressWarnings("unused")
@Subscribe
public void onSocialAuthenticationRequest(SocialConnectionEvent event) {
fetchProviderAndBeginAuthentication(event.getConnectionName());
fetchProviderAndBeginAuthentication(event.getStrategyName(), event.getConnectionName());
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -355,6 +355,7 @@ public void onDatabaseAuthenticationRequest(DatabaseChangePasswordEvent event) {
@SuppressWarnings("unused")
@Subscribe
public void onEnterpriseAuthenticationRequest(EnterpriseLoginEvent event) {
//noinspection ConstantConditions
if (event.getConnectionName() == null) {
Log.w(TAG, "There is no matching enterprise connection to authenticate with");
handler.post(new Runnable() {
Expand Down Expand Up @@ -382,7 +383,7 @@ public void run() {
}

Log.d(TAG, "Using the /authorize endpoint for this Enterprise Login Request");
fetchProviderAndBeginAuthentication(event.getConnectionName());
fetchProviderAndBeginAuthentication(event.getStrategyName(), event.getConnectionName());
}

//Callbacks
Expand Down
15 changes: 8 additions & 7 deletions lib/src/main/java/com/auth0/android/lock/PasswordlessLock.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
import com.auth0.android.lock.LockCallback.LockEvent;
import com.auth0.android.lock.internal.Options;
import com.auth0.android.lock.internal.Theme;
import com.auth0.android.lock.provider.AuthProviderResolver;
import com.auth0.android.lock.provider.ProviderResolverManager;
import com.auth0.android.lock.provider.AuthResolver;
import com.auth0.android.lock.utils.LockException;
import com.auth0.android.provider.AuthHandler;
import com.auth0.android.util.Telemetry;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -129,7 +130,7 @@ public Intent newIntent(Activity activity) {
public void onDestroy(Activity activity) {
LocalBroadcastManager.getInstance(activity).unregisterReceiver(this.receiver);
}

private void initialize(Activity activity) {
IntentFilter filter = new IntentFilter();
filter.addAction(Constants.AUTHENTICATION_ACTION);
Expand Down Expand Up @@ -323,13 +324,13 @@ public Builder onlyUseConnections(@NonNull List<String> connections) {
}

/**
* Uses the given AuthProviderResolver to ask for Native IdentityProviders.
* Uses the given AuthHandlers to query for AuthProviders on a new authentication request.
*
* @param resolver the AuthProviderResolver to use
* @param handlers that Lock will query for AuthProviders.
* @return the current builder instance
*/
public Builder withProviderResolver(@NonNull AuthProviderResolver resolver) {
ProviderResolverManager.set(resolver);
public Builder withAuthHandlers(@NonNull AuthHandler... handlers) {
AuthResolver.setAuthHandlers(Arrays.asList(handlers));
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import com.auth0.android.authentication.AuthenticationAPIClient;
import com.auth0.android.authentication.AuthenticationException;
import com.auth0.android.lock.adapters.Country;
import com.auth0.android.lock.internal.PasswordlessMode;
import com.auth0.android.lock.errors.AuthenticationError;
import com.auth0.android.lock.errors.LoginErrorMessageBuilder;
import com.auth0.android.lock.events.CountryCodeChangeEvent;
Expand All @@ -60,9 +59,10 @@
import com.auth0.android.lock.events.SocialConnectionEvent;
import com.auth0.android.lock.internal.Configuration;
import com.auth0.android.lock.internal.Options;
import com.auth0.android.lock.internal.PasswordlessMode;
import com.auth0.android.lock.internal.json.ApplicationFetcher;
import com.auth0.android.lock.internal.json.Connection;
import com.auth0.android.lock.provider.ProviderResolverManager;
import com.auth0.android.lock.provider.AuthResolver;
import com.auth0.android.lock.views.PasswordlessLockView;
import com.auth0.android.provider.AuthCallback;
import com.auth0.android.provider.AuthProvider;
Expand Down Expand Up @@ -445,7 +445,7 @@ public void onSocialAuthenticationRequest(SocialConnectionEvent event) {
lastPasswordlessEmailOrNumber = null;
lastPasswordlessCountry = null;
Log.v(TAG, "Looking for a provider to use with the connection " + event.getConnectionName());
currentProvider = ProviderResolverManager.get().onAuthProviderRequest(this, authProviderCallback, event.getConnectionName());
currentProvider = AuthResolver.providerFor(event.getStrategyName(), event.getConnectionName());
if (currentProvider != null) {
currentProvider.start(this, authProviderCallback, PERMISSION_REQUEST_CODE, CUSTOM_AUTH_REQUEST_CODE);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,39 @@

package com.auth0.android.lock.events;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class EnterpriseLoginEvent {

private final String strategyName;
private final String connectionName;
private final String username;
private final String password;
private final boolean useRO;

public EnterpriseLoginEvent(@Nullable String connectionName, String username, String password) {
public EnterpriseLoginEvent(@Nullable String strategyName, @NonNull String connectionName, String username, String password) {
this.strategyName = strategyName;
this.connectionName = connectionName;
this.username = username;
this.password = password;
this.useRO = true;
}

public EnterpriseLoginEvent(@Nullable String connectionName) {
public EnterpriseLoginEvent(@Nullable String strategyName, @NonNull String connectionName) {
this.strategyName = strategyName;
this.connectionName = connectionName;
this.username = "";
this.password = "";
this.useRO = false;
}

@Nullable
public String getStrategyName() {
return strategyName;
}

@NonNull
public String getConnectionName() {
return connectionName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@
import android.support.annotation.NonNull;

public class SocialConnectionEvent {
private final String strategyName;
private final String connectionName;

public SocialConnectionEvent(@NonNull String connectionName) {
public SocialConnectionEvent(@NonNull String strategyName, @NonNull String connectionName) {
this.strategyName = strategyName;
this.connectionName = connectionName;
}

@NonNull
public String getStrategyName() {
return strategyName;
}

@NonNull
public String getConnectionName() {
return connectionName;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.auth0.android.lock.provider;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.auth0.android.provider.AuthHandler;
import com.auth0.android.provider.AuthProvider;

import java.util.ArrayList;
import java.util.List;


/**
* Holds instances of AuthHandlers that can be used to query for AuthProviders given
* a strategy and connection name.
* When no AuthProvider is matched, it will return null
*/
public final class AuthResolver {
private static List<AuthHandler> authHandlers;

private AuthResolver() {
}

/**
* Sets the AuthHandler list to use on this instance.
*
* @param handlers the list of AuthHandlers to use.
*/
public static void setAuthHandlers(@NonNull List<AuthHandler> handlers) {
authHandlers = new ArrayList<>(handlers);
}

/**
* Get an AuthProvider that can handle a given strategy and connection name, or null if there are no
* providers to handle them.
*
* @param strategy to handle
* @param connection to handle
* @return an AuthProvider to handle the authentication or null if no providers are available.
*/
@Nullable
public static AuthProvider providerFor(@Nullable String strategy, @NonNull String connection) {
if (authHandlers == null) {
return null;
}

AuthProvider provider = null;
for (AuthHandler p : authHandlers) {
provider = p.providerFor(strategy, connection);
if (provider != null) {
break;
}
}
return provider;
}
}
Loading

0 comments on commit 3552d05

Please sign in to comment.