Skip to content

Commit

Permalink
rename flag to setOAuth2Preferred. add isOAuth2Preferred method
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Dec 21, 2016
1 parent 09f9370 commit 3168830
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class AuthenticationAPIClient {
private final Gson gson;
private final com.auth0.android.request.internal.RequestFactory factory;
private final ErrorBuilder<AuthenticationException> authErrorBuilder;
private boolean useOAuth2;
private boolean oauth2Preferred;


/**
Expand Down Expand Up @@ -143,10 +143,17 @@ private AuthenticationAPIClient(Auth0 auth0, RequestFactory factory, OkHttpClien
* This setting affects the methods {@link AuthenticationAPIClient#login(String, String, String)}, {@link AuthenticationAPIClient#tokenInfo(String)}, {@link AuthenticationAPIClient#signUp(String, String, String)} and {@link AuthenticationAPIClient#signUp(String, String, String, String)}.
* Default is {@code false}.
*
* @param use if Lock will use the OAuth 2.0 API or the legacy one.
* @param preferred if Lock will use the OAuth 2.0 API or the legacy one.
*/
public void useOAuth2(boolean use) {
this.useOAuth2 = use;
public void setOAuth2Preferred(boolean preferred) {
this.oauth2Preferred = preferred;
}

/**
* Getter for the OAuth 2.0 preferred current value.
*/
public boolean isOAuth2Preferred() {
return oauth2Preferred;
}

/**
Expand Down Expand Up @@ -204,7 +211,7 @@ public void setUserAgent(String userAgent) {
*/
@SuppressWarnings("WeakerAccess")
public AuthenticationRequest login(@NonNull String usernameOrEmail, @NonNull String password, @NonNull String connection) {
if (useOAuth2) {
if (oauth2Preferred) {
AuthenticationRequest login = login(usernameOrEmail, password);
login.setRealm(connection);
return login;
Expand Down Expand Up @@ -421,7 +428,7 @@ public Request<UserProfile, AuthenticationException> userInfo(@NonNull String ac

/**
* Fetch the token information from Auth0.
* If {@link AuthenticationAPIClient#useOAuth2} is set to true, userInfo endpoint will be used instead.
* If {@link AuthenticationAPIClient#oauth2Preferred} is set to true, userInfo endpoint will be used instead.
* <p>
* Example usage:
* <pre><code>
Expand All @@ -442,7 +449,7 @@ public Request<UserProfile, AuthenticationException> userInfo(@NonNull String ac
@SuppressWarnings("WeakerAccess")
@Deprecated
public Request<UserProfile, AuthenticationException> tokenInfo(@NonNull String idToken) {
if (useOAuth2) {
if (oauth2Preferred) {
return userInfo(idToken);
}

Expand Down Expand Up @@ -521,7 +528,7 @@ public DatabaseConnectionRequest<DatabaseUser, AuthenticationException> createUs

/**
* Creates a user in a DB connection using <a href="https://auth0.com/docs/auth-api#!#post--dbconnections-signup">'/dbconnections/signup' endpoint</a>
* and then logs in using the /oauth/ro endpoint. If {@link AuthenticationAPIClient#useOAuth2} is set to true, the /oauth/token endpoint will be used instead.
* and then logs in using the /oauth/ro endpoint. If {@link AuthenticationAPIClient#oauth2Preferred} is set to true, the /oauth/token endpoint will be used instead.
* Example usage:
* <pre><code>
* client.signUp("{email}", "{password}", "{username}", "{database connection name}")
Expand All @@ -544,7 +551,7 @@ public DatabaseConnectionRequest<DatabaseUser, AuthenticationException> createUs
public SignUpRequest signUp(@NonNull String email, @NonNull String password, @NonNull String username, @NonNull String connection) {
final DatabaseConnectionRequest<DatabaseUser, AuthenticationException> createUserRequest = createUser(email, password, username, connection);
final AuthenticationRequest authenticationRequest;
if (useOAuth2) {
if (oauth2Preferred) {
authenticationRequest = login(email, password);
authenticationRequest.setRealm(connection);
} else {
Expand All @@ -556,7 +563,7 @@ public SignUpRequest signUp(@NonNull String email, @NonNull String password, @No

/**
* Creates a user in a DB connection using <a href="https://auth0.com/docs/auth-api#!#post--dbconnections-signup">'/dbconnections/signup' endpoint</a>
* and then logs in using the /oauth/ro endpoint. If {@link AuthenticationAPIClient#useOAuth2} is set to true, the /oauth/token endpoint will be used instead.
* and then logs in using the /oauth/ro endpoint. If {@link AuthenticationAPIClient#oauth2Preferred} is set to true, the /oauth/token endpoint will be used instead.
* Example usage:
* <pre><code>
* client.signUp("{email}", "{password}", "{database connection name}")
Expand All @@ -578,7 +585,7 @@ public SignUpRequest signUp(@NonNull String email, @NonNull String password, @No
public SignUpRequest signUp(@NonNull String email, @NonNull String password, @NonNull String connection) {
final DatabaseConnectionRequest<DatabaseUser, AuthenticationException> createUserRequest = createUser(email, password, connection);
final AuthenticationRequest authenticationRequest;
if (useOAuth2) {
if (oauth2Preferred) {
authenticationRequest = login(email, password);
authenticationRequest.setRealm(connection);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import java.util.Locale;
import java.util.Map;

import static android.R.id.list;
import static com.auth0.android.util.AuthenticationAPI.GENERIC_TOKEN;
import static com.auth0.android.util.AuthenticationAPI.ID_TOKEN;
import static com.auth0.android.util.AuthenticationAPI.REFRESH_TOKEN;
Expand Down Expand Up @@ -147,6 +148,31 @@ public void shouldNotSetTelemetryIfMissing() throws Exception {
verify(factory, never()).setClientInfo(any(String.class));
}

@SuppressWarnings("unchecked")
@Test
public void shouldPreferOAuth2() throws Exception {
AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
client.setOAuth2Preferred(true);

assertThat(client.isOAuth2Preferred(), is(true));
}

@SuppressWarnings("unchecked")
@Test
public void shouldNotPreferOAuth2() throws Exception {
AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
client.setOAuth2Preferred(false);

assertThat(client.isOAuth2Preferred(), is(false));
}

@SuppressWarnings("unchecked")
@Test
public void shouldNotPreferOAuth2ByDefault() throws Exception {
AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
assertThat(client.isOAuth2Preferred(), is(false));
}

@SuppressWarnings("unchecked")
@Test
public void shouldEnableHttpLogging() throws Exception {
Expand Down Expand Up @@ -283,7 +309,7 @@ public void shouldLoginWithUserAndPasswordUsingOAuthTokenEndpointIfOAuth2IsEnabl
final MockAuthenticationCallback<Credentials> callback = new MockAuthenticationCallback<>();

AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
client.useOAuth2(true);
client.setOAuth2Preferred(true);
client.login(SUPPORT_AUTH0_COM, "some-password", MY_CONNECTION)
.start(callback);
assertThat(callback, hasPayloadOfType(Credentials.class));
Expand Down Expand Up @@ -389,7 +415,7 @@ public void shouldFetchUserInfoIfOAuth2IsEnabled() throws Exception {
final MockAuthenticationCallback<UserProfile> callback = new MockAuthenticationCallback<>();

AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
client.useOAuth2(true);
client.setOAuth2Preferred(true);
client.tokenInfo("ACCESS_TOKEN")
.start(callback);

Expand Down Expand Up @@ -768,7 +794,7 @@ public void shouldSignUpUserUsingOAuthTokenEndpointIfOAuth2IsEnabled() throws Ex

final MockAuthenticationCallback<Credentials> callback = new MockAuthenticationCallback<>();
AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
client.useOAuth2(true);
client.setOAuth2Preferred(true);
client.signUp(SUPPORT_AUTH0_COM, PASSWORD, SUPPORT, MY_CONNECTION)
.start(callback);

Expand Down Expand Up @@ -899,7 +925,7 @@ public void shouldSignUpUserWithoutUsernameUsingOAuthTokenEndpointIfOAuth2IsEnab

final MockAuthenticationCallback<Credentials> callback = new MockAuthenticationCallback<>();
AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
client.useOAuth2(true);
client.setOAuth2Preferred(true);
client.signUp(SUPPORT_AUTH0_COM, PASSWORD, MY_CONNECTION)
.start(callback);

Expand Down

0 comments on commit 3168830

Please sign in to comment.