|
| 1 | +package com.instipod.keycloakauthenticators; |
| 2 | + |
| 3 | +import org.keycloak.Config.Scope; |
| 4 | +import org.keycloak.authentication.authenticators.conditional.ConditionalAuthenticator; |
| 5 | +import org.keycloak.models.AuthenticationExecutionModel.Requirement; |
| 6 | +import org.keycloak.models.KeycloakSessionFactory; |
| 7 | +import org.keycloak.provider.ProviderConfigProperty; |
| 8 | +import org.keycloak.provider.ProviderConfigurationBuilder; |
| 9 | + |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class ConditionalAttributeAuthenticatorFactory implements org.keycloak.authentication.authenticators.conditional.ConditionalAuthenticatorFactory { |
| 14 | + public static final String PROVIDER_ID = "conditional-attribute"; |
| 15 | + protected static final String CONDITIONAL_ATTRIBUTE_NAME = "condAttributeName"; |
| 16 | + protected static final String CONDITIONAL_ATTRIBUTE_VALUE = "condAttributeValue"; |
| 17 | + protected static final String CONDITIONAL_NOT = "condNot"; |
| 18 | + |
| 19 | + private static List<ProviderConfigProperty> commonConfig; |
| 20 | + |
| 21 | + static { |
| 22 | + commonConfig = Collections.unmodifiableList(ProviderConfigurationBuilder.create() |
| 23 | + .property().name(CONDITIONAL_ATTRIBUTE_NAME).label("User Attribute Name").helpText("Attribute name to check (supports variables)").type(ProviderConfigProperty.STRING_TYPE).add() |
| 24 | + .property().name(CONDITIONAL_ATTRIBUTE_VALUE).label("User Attribute Value").helpText("Attribute value (supports variables)").type(ProviderConfigProperty.STRING_TYPE).add() |
| 25 | + .property().name(CONDITIONAL_NOT).label("Not").helpText("If we should match on NOT having this attribute").type(ProviderConfigProperty.BOOLEAN_TYPE).add() |
| 26 | + .build() |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void init(Scope config) { |
| 32 | + // no-op |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void postInit(KeycloakSessionFactory factory) { |
| 37 | + // no-op |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void close() { |
| 42 | + // no-op |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getId() { |
| 47 | + return PROVIDER_ID; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getDisplayType() { |
| 52 | + return "Condition - Attribute"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getReferenceCategory() { |
| 57 | + return "condition"; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean isConfigurable() { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + private static final Requirement[] REQUIREMENT_CHOICES = { |
| 66 | + Requirement.REQUIRED, Requirement.DISABLED |
| 67 | + }; |
| 68 | + |
| 69 | + @Override |
| 70 | + public Requirement[] getRequirementChoices() { |
| 71 | + return REQUIREMENT_CHOICES; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean isUserSetupAllowed() { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String getHelpText() { |
| 81 | + return "Flow is executed only if user has specified attribute."; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public List<ProviderConfigProperty> getConfigProperties() { |
| 86 | + return commonConfig; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public ConditionalAuthenticator getSingleton() { |
| 91 | + return ConditionalAttributeAuthenticator.SINGLETON; |
| 92 | + } |
| 93 | +} |
0 commit comments