# Custom authenticator using scripts error

**URL:** <https://forum.keycloak.org/t/custom-authenticator-using-scripts-error/9599>\
**Category:** Miscellanaeous\
**Created:** [June 15, 2021, 2:19pm UTC](https://forum.keycloak.org/t/custom-authenticator-using-scripts-error/9599 "2021-06-15T14:19:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Visesh](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Visesh](https://forum.keycloak.org/u/Visesh)\
**Post date:** [June 15, 2021, 2:19pm UTC](https://forum.keycloak.org/t/custom-authenticator-using-scripts-error/9599/1 "2021-06-15T14:19:29Z")

</div>

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](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;

```

}

}
