|
| 1 | +package com.github.thomasdarimont.keycloak.custom.auth.confirmcookie; |
| 2 | + |
| 3 | +import com.google.auto.service.AutoService; |
| 4 | +import jakarta.ws.rs.core.Response; |
| 5 | +import lombok.extern.jbosslog.JBossLog; |
| 6 | +import org.keycloak.Config; |
| 7 | +import org.keycloak.authentication.AuthenticationFlowContext; |
| 8 | +import org.keycloak.authentication.Authenticator; |
| 9 | +import org.keycloak.authentication.AuthenticatorFactory; |
| 10 | +import org.keycloak.authentication.authenticators.browser.CookieAuthenticator; |
| 11 | +import org.keycloak.models.AuthenticationExecutionModel; |
| 12 | +import org.keycloak.models.KeycloakSession; |
| 13 | +import org.keycloak.models.KeycloakSessionFactory; |
| 14 | +import org.keycloak.models.RealmModel; |
| 15 | +import org.keycloak.models.UserModel; |
| 16 | +import org.keycloak.provider.ProviderConfigProperty; |
| 17 | +import org.keycloak.provider.ProviderConfigurationBuilder; |
| 18 | +import org.keycloak.provider.ServerInfoAwareProviderFactory; |
| 19 | +import org.keycloak.services.managers.AuthenticationManager; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +@JBossLog |
| 25 | +public class ConfirmCookieAuthenticator extends CookieAuthenticator { |
| 26 | + |
| 27 | + static final ConfirmCookieAuthenticator INSTANCE = new ConfirmCookieAuthenticator(); |
| 28 | + |
| 29 | + @Override |
| 30 | + public void authenticate(AuthenticationFlowContext context) { |
| 31 | + AuthenticationManager.AuthResult authResult = AuthenticationManager.authenticateIdentityCookie(context.getSession(), |
| 32 | + context.getRealm(), true); |
| 33 | + if (authResult == null) { |
| 34 | + context.attempted(); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + Response response = context.form() // |
| 39 | + .createForm("login-confirm-cookie-form.ftl"); |
| 40 | + context.challenge(response); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void action(AuthenticationFlowContext context) { |
| 45 | + super.authenticate(context); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean requiresUser() { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public boolean configuredFor(KeycloakSession session, RealmModel realm, UserModel user) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void setRequiredActions(KeycloakSession session, RealmModel realm, UserModel user) { |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void close() { |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + @AutoService(AuthenticatorFactory.class) |
| 69 | + public static class Factory implements AuthenticatorFactory, ServerInfoAwareProviderFactory { |
| 70 | + |
| 71 | + @Override |
| 72 | + public String getId() { |
| 73 | + return "acme-auth-confirm-cookie"; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Authenticator create(KeycloakSession session) { |
| 78 | + return INSTANCE; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getDisplayType() { |
| 83 | + return "Acme: Confirm Cookie Authenticator"; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public String getHelpText() { |
| 88 | + return "Shows a form asking to confirm cookie"; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String getReferenceCategory() { |
| 93 | + return "hello"; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean isConfigurable() { |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public List<ProviderConfigProperty> getConfigProperties() { |
| 103 | + |
| 104 | + List<ProviderConfigProperty> properties = ProviderConfigurationBuilder.create() // |
| 105 | + .property().name("message").label("Message") |
| 106 | + .helpText("Message text").type(ProviderConfigProperty.STRING_TYPE) |
| 107 | + .defaultValue("hello").add() |
| 108 | + |
| 109 | + .build(); |
| 110 | + return properties; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public AuthenticationExecutionModel.Requirement[] getRequirementChoices() { |
| 115 | + return REQUIREMENT_CHOICES; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean isUserSetupAllowed() { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void postInit(KeycloakSessionFactory factory) { |
| 125 | + // called after factory is found |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void init(Config.Scope config) { |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + @Override |
| 134 | + public void close() { |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public Map<String, String> getOperationalInfo() { |
| 140 | + return Map.of("info", "infoValue"); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments