Skip to content

Commit

Permalink
feat. log in with us/pw using the /oauth/token endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Nov 25, 2016
1 parent a85608d commit 1bfc38d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* <pre><code>
Expand Down Expand Up @@ -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:
* <pre><code>
* client.login("{username or email}", "{password}")
* .start(new BaseCallback<Credentials>() {
* {@literal}Override
* public void onSuccess(Credentials payload) { }
*
* {@literal}Override
* public void onFailure(AuthenticationException error) { }
* });
* </code></pre>
*
* @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<String, Object> 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 <a href="https://auth0.com/docs/auth-api#!#post--oauth-access_token">'\oauth\access_token' endpoint</a>
* The default scope used is 'openid'.
Expand Down Expand Up @@ -852,6 +881,20 @@ public TokenRequest token(@NonNull String authorizationCode, @NonNull String red
return new TokenRequest(request);
}

private AuthenticationRequest loginWithToken(Map<String, Object> parameters) {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(OAUTH_PATH)
.addPathSegment(TOKEN_PATH)
.build();

final Map<String, Object> requestParameters = ParameterBuilder.newBuilder()
.setClientId(getClientId())
.addAll(parameters)
.asDictionary();
return factory.authenticationPOST(url, client, gson)
.addAuthenticationParameters(requestParameters);
}

private AuthenticationRequest loginWithResourceOwner(Map<String, Object> parameters) {
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
.addPathSegment(OAUTH_PATH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public void shouldLoginWithUserAndPasswordUsingOAuthTokenEndpoint() throws Excep
mockAPI.willReturnSuccessfulLogin();
final MockAuthenticationCallback<Credentials> callback = new MockAuthenticationCallback<>();

client.loginWithToken(SUPPORT_AUTH0_COM, "some-password")
client.login(SUPPORT_AUTH0_COM, "some-password")
.start(callback);
assertThat(callback, hasPayloadOfType(Credentials.class));

Expand All @@ -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")));
}
Expand All @@ -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()));

Expand All @@ -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")));
}
Expand Down

0 comments on commit 1bfc38d

Please sign in to comment.