+
Skip to content

Change error to 400 for unknown user #40939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.keycloak.OAuth2Constants;
import org.keycloak.OAuthErrorException;
import org.keycloak.authentication.AuthenticationFlowException;
import org.keycloak.authentication.AuthenticationProcessor;
import org.keycloak.events.Details;
import org.keycloak.events.Errors;
Expand Down Expand Up @@ -111,7 +112,16 @@ public Response process(Context context) {
.setSession(session)
.setUriInfo(session.getContext().getUri())
.setRequest(request);
Response challenge = processor.authenticateOnly();
Response challenge = null;
try {
challenge = processor.authenticateOnly();
} catch (AuthenticationFlowException afe) {
new AuthenticationSessionManager(session).removeAuthenticationSession(realm, authSession, false);
String errorMessage = "Cannot find user (Unknown user)";
event.detail(Details.REASON, errorMessage);
event.error(Errors.USER_NOT_FOUND);
throw new CorsErrorResponseException(cors, afe.getError().name(), errorMessage, Response.Status.BAD_REQUEST);
}
if (challenge != null) {
// Remove authentication session as "Resource Owner Password Credentials Grant" is single-request scoped authentication
new AuthenticationSessionManager(session).removeAuthenticationSession(realm, authSession, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.keycloak.OAuthErrorException;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.authentication.AuthenticationFlowError;
import org.keycloak.authentication.authenticators.client.ClientIdAndSecretAuthenticator;
import org.keycloak.common.Profile;
import org.keycloak.connections.infinispan.InfinispanConnectionProvider;
Expand Down Expand Up @@ -775,6 +776,27 @@ public void grantAccessTokenNoRefreshToken() throws Exception {
assertNull(response.getRefreshToken());
}

@Test
public void grantAccessTokenServiceAccountUserOfOtherClient() throws Exception {
ClientManager.realm(adminClient.realm("test")).clientId("resource-owner").setServiceAccountsEnabled(true);
oauth.client("resource-owner-refresh", "secret");
AccessTokenResponse response = oauth.doPasswordGrantRequest("service-account-resource-owner", "password");

assertEquals(400, response.getStatusCode());
assertEquals(AuthenticationFlowError.UNKNOWN_USER.name(), response.getError());
assertEquals("Cannot find user (Unknown user)", response.getErrorDescription());
events.expectLogin()
.client("resource-owner-refresh")
.user((String) null)
.session((String) null)
.detail(Details.REASON, "Cannot find user (Unknown user)")
.error(Errors.USER_NOT_FOUND)
.clearDetails()
.assertEvent();

ClientManager.realm(adminClient.realm("test")).clientId("resource-owner").setServiceAccountsEnabled(false);
}

private int getAuthenticationSessionsCount() {
return testingClient.testing().cache(InfinispanConnectionProvider.AUTHENTICATION_SESSIONS_CACHE_NAME).size();
}
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载