From 316883066346f0ef4e08add932908f6481e02ba7 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Wed, 21 Dec 2016 18:34:09 -0300 Subject: [PATCH] rename flag to setOAuth2Preferred. add isOAuth2Preferred method --- .../AuthenticationAPIClient.java | 29 ++++++++++------ .../AuthenticationAPIClientTest.java | 34 ++++++++++++++++--- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.java b/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.java index fcc50f4c9..69a46324d 100755 --- a/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.java +++ b/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.java @@ -97,7 +97,7 @@ public class AuthenticationAPIClient { private final Gson gson; private final com.auth0.android.request.internal.RequestFactory factory; private final ErrorBuilder authErrorBuilder; - private boolean useOAuth2; + private boolean oauth2Preferred; /** @@ -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; } /** @@ -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; @@ -421,7 +428,7 @@ public Request 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. *

* Example usage: *


@@ -442,7 +449,7 @@ public Request userInfo(@NonNull String ac
     @SuppressWarnings("WeakerAccess")
     @Deprecated
     public Request tokenInfo(@NonNull String idToken) {
-        if (useOAuth2) {
+        if (oauth2Preferred) {
             return userInfo(idToken);
         }
 
@@ -521,7 +528,7 @@ public DatabaseConnectionRequest createUs
 
     /**
      * Creates a user in a DB connection using '/dbconnections/signup' endpoint
-     * 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:
      * 

      * client.signUp("{email}", "{password}", "{username}", "{database connection name}")
@@ -544,7 +551,7 @@ public DatabaseConnectionRequest createUs
     public SignUpRequest signUp(@NonNull String email, @NonNull String password, @NonNull String username, @NonNull String connection) {
         final DatabaseConnectionRequest createUserRequest = createUser(email, password, username, connection);
         final AuthenticationRequest authenticationRequest;
-        if (useOAuth2) {
+        if (oauth2Preferred) {
             authenticationRequest = login(email, password);
             authenticationRequest.setRealm(connection);
         } else {
@@ -556,7 +563,7 @@ public SignUpRequest signUp(@NonNull String email, @NonNull String password, @No
 
     /**
      * Creates a user in a DB connection using '/dbconnections/signup' endpoint
-     * 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:
      * 

      * client.signUp("{email}", "{password}", "{database connection name}")
@@ -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 createUserRequest = createUser(email, password, connection);
         final AuthenticationRequest authenticationRequest;
-        if (useOAuth2) {
+        if (oauth2Preferred) {
             authenticationRequest = login(email, password);
             authenticationRequest.setRealm(connection);
         } else {
diff --git a/auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.java b/auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.java
index 9b75dc5e9..6a4683022 100755
--- a/auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.java
+++ b/auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.java
@@ -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;
@@ -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 {
@@ -283,7 +309,7 @@ public void shouldLoginWithUserAndPasswordUsingOAuthTokenEndpointIfOAuth2IsEnabl
         final MockAuthenticationCallback 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));
@@ -389,7 +415,7 @@ public void shouldFetchUserInfoIfOAuth2IsEnabled() throws Exception {
         final MockAuthenticationCallback callback = new MockAuthenticationCallback<>();
 
         AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
-        client.useOAuth2(true);
+        client.setOAuth2Preferred(true);
         client.tokenInfo("ACCESS_TOKEN")
                 .start(callback);
 
@@ -768,7 +794,7 @@ public void shouldSignUpUserUsingOAuthTokenEndpointIfOAuth2IsEnabled() throws Ex
 
         final MockAuthenticationCallback 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);
 
@@ -899,7 +925,7 @@ public void shouldSignUpUserWithoutUsernameUsingOAuthTokenEndpointIfOAuth2IsEnab
 
         final MockAuthenticationCallback callback = new MockAuthenticationCallback<>();
         AuthenticationAPIClient client = new AuthenticationAPIClient(auth0);
-        client.useOAuth2(true);
+        client.setOAuth2Preferred(true);
         client.signUp(SUPPORT_AUTH0_COM, PASSWORD, MY_CONNECTION)
                 .start(callback);