Custom authenticator using scripts error

19:47:14,113 WARN [org.keycloak.events] (default task-6) type=LOGIN_ERROR, realmId=Test, clientId=account, userId=null, ipAddress=127.0.0.1, error=invalid_user_credentials, auth_method=openid-connect, auth_type=code, redirect_uri=http://localhost:8080/auth/realms/test/account/login-redirect, code_id=495d36c9-6ac1-4683-a4c3-3e5d0157cedb, authSessionParentId=495d36c9-6ac1-4683-a4c3-3e5d0157cedb, authSessionTabId=C6evwfv2L-8

My scipt code:

AuthenticationFlowError = Java.type(“org.keycloak.authentication.AuthenticationFlowError”);
ClientSessionCode = Java.type(“org.keycloak.services.managers.ClientSessionCode”);
Urls = Java.type(“org.keycloak.services.Urls”);
OAuth2Constants = Java.type(“org.keycloak.OAuth2Constants”);
Response = Java.type(“javax.ws.rs.core.Response”);

/**

  • Redirect to Identification provider
  • @param context {@see org.keycloak.authentication.AuthenticationFlowContext}
  • @param providerId : the alias of the provider to use */

function redirect_to_idp(context, providerId) {
var identityProviders = context.getRealm().getIdentityProviders();
var identityProvidersLen = identityProviders.length;
for (var i = 0; i < identityProvidersLen; i++) {
identityProvider = identityProviders[i];
if (identityProvider.isEnabled() && providerId.equals(identityProvider.getAlias())) {
var accessCode = new ClientSessionCode(context.getSession(), context.getRealm(), context.getAuthenticationSession()).getOrGenerateCode();
var clientId = context.getAuthenticationSession().getClient().getClientId();
var tabId = context.getAuthenticationSession().getTabId();
var location = Urls.identityProviderAuthnRequest(context.getUriInfo().getBaseUri(), providerId, context.getRealm().getName(), accessCode, clientId, tabId);
if (context.getAuthenticationSession().getClientNote(OAuth2Constants.DISPLAY) !== null)
{
location = UriBuilder.fromUri(location).queryParam(OAuth2Constants.DISPLAY, context.getAuthenticationSession().getClientNote(OAuth2Constants.DISPLAY)).build();
}
var response = Response.seeOther(location).build();
LOG.info(“Redirecting to %s” + providerId);
context.forceChallenge(response);
return;
}
}
function authenticate(context) {

 var username = user ? user.username : "anonymous";
 if (username.endsWith("gmail.com")) {
     redirect_to_idp(context, "google");
     return;
 }
 context.success();
 return;

}

}