Environment dependent provider

This commit is contained in:
Stian Thorgersen 2016-09-08 07:36:00 +02:00 committed by Bruno Oliveira
parent 76e1160b36
commit 36bb94afb8
3 changed files with 55 additions and 4 deletions

View file

@ -0,0 +1,33 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.provider;
/**
* Providers that are only supported in some environments can implement this interface to be able to determine if they
* should be available or not.
*
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public interface EnvironmentDependentProviderFactory {
/**
* @return <code>true</code> if the provider is supported and should be available, <code>false</code> otherwise
*/
boolean isSupported();
}

View file

@ -20,6 +20,7 @@ import org.keycloak.Config;
import org.keycloak.common.util.MultivaluedHashMap; import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.provider.EnvironmentDependentProviderFactory;
import org.keycloak.provider.Provider; import org.keycloak.provider.Provider;
import org.keycloak.provider.ProviderEvent; import org.keycloak.provider.ProviderEvent;
import org.keycloak.provider.ProviderEventListener; import org.keycloak.provider.ProviderEventListener;
@ -193,8 +194,7 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory, Pr
} }
Config.Scope scope = Config.scope(spi.getName(), provider); Config.Scope scope = Config.scope(spi.getName(), provider);
if (scope.getBoolean("enabled", true)) { if (isEnabled(factory, scope)) {
factory.init(scope); factory.init(scope);
if (spi.isInternal() && !isInternal(factory)) { if (spi.isInternal() && !isInternal(factory)) {
@ -208,7 +208,7 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory, Pr
} else { } else {
for (ProviderFactory factory : pm.load(spi)) { for (ProviderFactory factory : pm.load(spi)) {
Config.Scope scope = Config.scope(spi.getName(), factory.getId()); Config.Scope scope = Config.scope(spi.getName(), factory.getId());
if (scope.getBoolean("enabled", true)) { if (isEnabled(factory, scope)) {
factory.init(scope); factory.init(scope);
if (spi.isInternal() && !isInternal(factory)) { if (spi.isInternal() && !isInternal(factory)) {
@ -223,7 +223,16 @@ public class DefaultKeycloakSessionFactory implements KeycloakSessionFactory, Pr
} }
} }
return factoryMap; return factoryMap;
}
private boolean isEnabled(ProviderFactory factory, Config.Scope scope) {
if (!scope.getBoolean("enabled", true)) {
return false;
}
if (factory instanceof EnvironmentDependentProviderFactory) {
return ((EnvironmentDependentProviderFactory) factory).isSupported();
}
return true;
} }
protected void loadSPIs(ProviderManager pm, List<Spi> spiList) { protected void loadSPIs(ProviderManager pm, List<Spi> spiList) {

View file

@ -21,12 +21,15 @@ import org.keycloak.hash.PasswordHashProvider;
import org.keycloak.hash.PasswordHashProviderFactory; import org.keycloak.hash.PasswordHashProviderFactory;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.provider.EnvironmentDependentProviderFactory;
import java.io.File;
/** /**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a> * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
public class PlainTextPasswordProviderFactory implements PasswordHashProviderFactory { public class PlainTextPasswordProviderFactory implements PasswordHashProviderFactory, EnvironmentDependentProviderFactory {
@Override @Override
public PasswordHashProvider create(KeycloakSession session) { public PasswordHashProvider create(KeycloakSession session) {
return new PlainTextPasswordProvider(); return new PlainTextPasswordProvider();
@ -51,4 +54,10 @@ public class PlainTextPasswordProviderFactory implements PasswordHashProviderFac
public String getId() { public String getId() {
return "text"; return "text";
} }
// TODO REMOVE THIS
@Override
public boolean isSupported() {
return !new File("/tmp/disable-text-hash").exists();
}
} }