Провайдер JavaScript#

Keycloak позволяет администраторам при помощи JavaScript скриптов добавлять новую функциональность:

  • Аутентификатор.

  • JavaScript Policy.

  • OpenID Connect Protocol сопоставление.

В данном поле будут доступны следующие поля: 'user' - текущий UserModel; 'realm' - текущий RealmModel; 'userSession' - текущая пользовательская сессия; 'keycloakSession' - текущая KeycloakSession; 'authenticationSession' - текущая AuthenticationSessionModel; 'httpRequest' - текущая org.jboss.resteasy.spi.HttpRequest ; 'script' - доступ к метаданным о скрипте; 'LOG' - org.jboss.logging.Logger, заданный в классе ScriptBasedAuthenticator.

Пример 1. Создание аутентификатора:

AuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");

function authenticate(context) {

  LOG.info(script.name + " --> trace auth for: " + user.username);

  if (   user.username === "tester"
      && user.getAttribute("someAttribute")
      && user.getAttribute("someAttribute").contains("someValue")) {

      context.failure(AuthenticationFlowError.INVALID_USER);
      return;
  }

  context.success();
}

Пример 2. Создание аутентификатора:

AuthenticationFlowError = Java.type("org.keycloak.authentication.AuthenticationFlowError");

function authenticate(context) {

    if (authenticationSession.getRealm().getName() != "${realm}") {
        context.failure(AuthenticationFlowError.INVALID_CLIENT_SESSION);
        return;
    }

    if (authenticationSession.getClient().getClientId() != "${clientId}") {
        context.failure(AuthenticationFlowError.UNKNOWN_CLIENT);
        return;
    }

    if (authenticationSession.getProtocol() != "${authMethod}") {
        context.failure(AuthenticationFlowError.INVALID_CLIENT_SESSION);
        return;
    }

    context.success();
}

Пример 3. Создание сопоставления

'hello_' + user.username

Для того чтобы данная функциональность стала доступной, необходимо включить scripts и upload_scripts, следуя инструкциям в руководстве по установке (см. главу "Дополнительная функциональность").