From 7622152b0bebca465195d551bf0fb5ea0550fc88 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Thu, 24 Nov 2016 14:10:07 -0300 Subject: [PATCH] feat. log in with us/pw using the /oauth/token endpoint --- .../AuthenticationAPIClient.java | 45 ++++++++++++++++++- .../AuthenticationAPIClientTest.java | 6 ++- 2 files changed, 48 insertions(+), 3 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 45a04598d..0c02515c5 100755 --- a/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.java +++ b/auth0/src/main/java/com/auth0/android/authentication/AuthenticationAPIClient.java @@ -148,7 +148,7 @@ public void setUserAgent(String userAgent) { } /** - * Log in a user with email/username and password using a DB connection. + * Log in a user with email/username and password using a DB connection and the /oauth/ro endpoint. * The default scope used is 'openid'. * Example usage: *

@@ -178,6 +178,35 @@ public AuthenticationRequest login(@NonNull String usernameOrEmail, @NonNull Str
         return loginWithResourceOwner(requestParameters);
     }
 
+    /**
+     * Log in a user with email/username and password using the /oauth/token endpoint.
+     * Example usage:
+     * 

+     * client.login("{username or email}", "{password}")
+     *      .start(new BaseCallback() {
+     *          {@literal}Override
+     *          public void onSuccess(Credentials payload) { }
+     *
+     *          {@literal}Override
+     *          public void onFailure(AuthenticationException error) { }
+     *      });
+     * 
+ * + * @param usernameOrEmail of the user + * @param password of the user + * @return a request to configure and start that will yield {@link Credentials} + */ + @SuppressWarnings("WeakerAccess") + public AuthenticationRequest login(@NonNull String usernameOrEmail, @NonNull String password) { + Map requestParameters = ParameterBuilder.newBuilder() + .set(USERNAME_KEY, usernameOrEmail) + .set(PASSWORD_KEY, password) + .setGrantType(GRANT_TYPE_PASSWORD) + .asDictionary(); + + return loginWithToken(requestParameters); + } + /** * Log in a user with a OAuth 'access_token' of a Identity Provider like Facebook or Twitter using '\oauth\access_token' endpoint * The default scope used is 'openid'. @@ -817,6 +846,20 @@ public TokenRequest token(@NonNull String authorizationCode, @NonNull String red return new TokenRequest(request); } + private AuthenticationRequest loginWithToken(Map parameters) { + HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder() + .addPathSegment(OAUTH_PATH) + .addPathSegment(TOKEN_PATH) + .build(); + + final Map requestParameters = ParameterBuilder.newBuilder() + .setClientId(getClientId()) + .addAll(parameters) + .asDictionary(); + return factory.authenticationPOST(url, client, gson) + .addAuthenticationParameters(requestParameters); + } + private AuthenticationRequest loginWithResourceOwner(Map parameters) { HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder() .addPathSegment(OAUTH_PATH) 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 66dbfff6b..880d75c30 100755 --- a/auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.java +++ b/auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.java @@ -204,7 +204,7 @@ public void shouldLoginWithUserAndPasswordUsingOAuthTokenEndpoint() throws Excep mockAPI.willReturnSuccessfulLogin(); final MockAuthenticationCallback callback = new MockAuthenticationCallback<>(); - client.loginWithToken(SUPPORT_AUTH0_COM, "some-password") + client.login(SUPPORT_AUTH0_COM, "some-password") .start(callback); assertThat(callback, hasPayloadOfType(Credentials.class)); @@ -215,6 +215,7 @@ public void shouldLoginWithUserAndPasswordUsingOAuthTokenEndpoint() throws Excep assertThat(body, hasEntry("grant_type", "password")); assertThat(body, hasEntry("username", SUPPORT_AUTH0_COM)); assertThat(body, hasEntry("password", "some-password")); + assertThat(body, not(hasKey("connection"))); assertThat(body, not(hasKey("scope"))); assertThat(body, not(hasKey("audience"))); } @@ -224,7 +225,7 @@ public void shouldLoginWithUserAndPasswordSyncUsingOAuthTokenEndpoint() throws E mockAPI.willReturnSuccessfulLogin(); final Credentials credentials = client - .loginWithToken(SUPPORT_AUTH0_COM, "some-password") + .login(SUPPORT_AUTH0_COM, "some-password") .execute(); assertThat(credentials, is(notNullValue())); @@ -235,6 +236,7 @@ public void shouldLoginWithUserAndPasswordSyncUsingOAuthTokenEndpoint() throws E assertThat(body, hasEntry("grant_type", "password")); assertThat(body, hasEntry("username", SUPPORT_AUTH0_COM)); assertThat(body, hasEntry("password", "some-password")); + assertThat(body, not(hasKey("connection"))); assertThat(body, not(hasKey("scope"))); assertThat(body, not(hasKey("audience"))); }