Search in sources :

Example 1 with DefaultKeycloakSessionFactory

use of org.keycloak.services.DefaultKeycloakSessionFactory in project keycloak by keycloak.

the class PlainTextVaultProviderFactoryTest method shouldInitializeVaultCorrectly.

@Test
public void shouldInitializeVaultCorrectly() {
    // given
    VaultConfig config = new VaultConfig(Scenario.EXISTING.getAbsolutePathAsString());
    KeycloakSession session = new DefaultKeycloakSession(new DefaultKeycloakSessionFactory());
    FilesPlainTextVaultProviderFactory factory = new FilesPlainTextVaultProviderFactory() {

        @Override
        protected String getRealmName(KeycloakSession session) {
            return "test";
        }
    };
    // when
    factory.init(config);
    VaultProvider provider = factory.create(session);
    // then
    assertNotNull(provider);
}
Also used : DefaultKeycloakSessionFactory(org.keycloak.services.DefaultKeycloakSessionFactory) DefaultKeycloakSession(org.keycloak.services.DefaultKeycloakSession) KeycloakSession(org.keycloak.models.KeycloakSession) DefaultKeycloakSession(org.keycloak.services.DefaultKeycloakSession) Test(org.junit.Test)

Example 2 with DefaultKeycloakSessionFactory

use of org.keycloak.services.DefaultKeycloakSessionFactory in project keycloak by keycloak.

the class KeycloakModelTest method createKeycloakSessionFactory.

/**
 * Creates a fresh initialized {@link KeycloakSessionFactory}. The returned factory uses configuration
 * local to the thread that calls this method, allowing for per-thread customization. This in turn allows
 * testing of several parallel session factories which can be used to simulate several servers
 * running in parallel.
 * @return
 */
public static KeycloakSessionFactory createKeycloakSessionFactory() {
    int factoryIndex = FACTORY_COUNT.incrementAndGet();
    String threadName = Thread.currentThread().getName();
    CONFIG.reset();
    CONFIG.spi(ComponentFactorySpi.NAME).provider(DefaultComponentFactoryProviderFactory.PROVIDER_ID).config("cachingForced", "true");
    MODEL_PARAMETERS.forEach(m -> m.updateConfig(CONFIG));
    LOG.debugf("Creating factory %d in %s using the following configuration:\n    %s", factoryIndex, threadName, CONFIG);
    DefaultKeycloakSessionFactory res = new DefaultKeycloakSessionFactory() {

        @Override
        protected boolean isEnabled(ProviderFactory factory, Scope scope) {
            return super.isEnabled(factory, scope) && isFactoryAllowed(factory);
        }

        @Override
        protected Map<Class<? extends Provider>, Map<String, ProviderFactory>> loadFactories(ProviderManager pm) {
            spis.removeIf(s -> !isSpiAllowed(s));
            return super.loadFactories(pm);
        }

        private boolean isSpiAllowed(Spi s) {
            return MODEL_PARAMETERS.stream().anyMatch(p -> p.isSpiAllowed(s));
        }

        private boolean isFactoryAllowed(ProviderFactory factory) {
            return MODEL_PARAMETERS.stream().anyMatch(p -> p.isFactoryAllowed(factory));
        }

        @Override
        public String toString() {
            return "KeycloakSessionFactory " + factoryIndex + " (from " + threadName + " thread)";
        }
    };
    res.init();
    res.publish(new PostMigrationEvent());
    return res;
}
Also used : DefaultKeycloakSessionFactory(org.keycloak.services.DefaultKeycloakSessionFactory) Scope(org.keycloak.Config.Scope) DefaultExecutorsProviderFactory(org.keycloak.executors.DefaultExecutorsProviderFactory) ComponentFactoryProviderFactory(org.keycloak.component.ComponentFactoryProviderFactory) DefaultAuthorizationProviderFactory(org.keycloak.authorization.DefaultAuthorizationProviderFactory) DefaultComponentFactoryProviderFactory(org.keycloak.services.DefaultComponentFactoryProviderFactory) ProviderFactory(org.keycloak.provider.ProviderFactory) DeploymentStateProviderFactory(org.keycloak.models.DeploymentStateProviderFactory) ProviderManager(org.keycloak.provider.ProviderManager) BeforeClass(org.junit.BeforeClass) Map(java.util.Map) ComponentFactorySpi(org.keycloak.component.ComponentFactorySpi) ClientScopeSpi(org.keycloak.models.ClientScopeSpi) ExecutorsSpi(org.keycloak.executors.ExecutorsSpi) DBLockSpi(org.keycloak.models.dblock.DBLockSpi) ClusterSpi(org.keycloak.cluster.ClusterSpi) UserSpi(org.keycloak.models.UserSpi) Spi(org.keycloak.provider.Spi) UserLoginFailureSpi(org.keycloak.models.UserLoginFailureSpi) TimerSpi(org.keycloak.timer.TimerSpi) ClientSpi(org.keycloak.models.ClientSpi) StoreFactorySpi(org.keycloak.authorization.store.StoreFactorySpi) RealmSpi(org.keycloak.models.RealmSpi) GroupSpi(org.keycloak.models.GroupSpi) UserSessionSpi(org.keycloak.models.UserSessionSpi) DeploymentStateSpi(org.keycloak.models.DeploymentStateSpi) EventStoreSpi(org.keycloak.events.EventStoreSpi) AuthorizationSpi(org.keycloak.authorization.AuthorizationSpi) RoleSpi(org.keycloak.models.RoleSpi) Provider(org.keycloak.provider.Provider) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Example 3 with DefaultKeycloakSessionFactory

use of org.keycloak.services.DefaultKeycloakSessionFactory in project keycloak by keycloak.

the class DefaultHttpClientFactoryTest method createHttpClientProviderWithDisableTrustManager.

@Test
public void createHttpClientProviderWithDisableTrustManager() throws IOException {
    Map<String, String> values = new HashMap<>();
    values.put(DISABLE_TRUST_MANAGER_PROPERTY, "true");
    DefaultHttpClientFactory factory = new DefaultHttpClientFactory();
    factory.init(scope(values));
    KeycloakSession session = new DefaultKeycloakSession(new DefaultKeycloakSessionFactory());
    HttpClientProvider provider = factory.create(session);
    Optional<String> testURL = getTestURL();
    Assume.assumeTrue("Could not get test url for domain", testURL.isPresent());
    try (CloseableHttpClient httpClient = (CloseableHttpClient) provider.getHttpClient();
        CloseableHttpResponse response = httpClient.execute(new HttpGet(testURL.get()))) {
        assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusLine().getStatusCode());
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) DefaultKeycloakSessionFactory(org.keycloak.services.DefaultKeycloakSessionFactory) HashMap(java.util.HashMap) DefaultKeycloakSession(org.keycloak.services.DefaultKeycloakSession) KeycloakSession(org.keycloak.models.KeycloakSession) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) DefaultKeycloakSession(org.keycloak.services.DefaultKeycloakSession) Test(org.junit.Test)

Example 4 with DefaultKeycloakSessionFactory

use of org.keycloak.services.DefaultKeycloakSessionFactory in project keycloak by keycloak.

the class DefaultHttpClientFactoryTest method createHttpClientProviderWithUnvailableURL.

@Test(expected = SSLPeerUnverifiedException.class)
public void createHttpClientProviderWithUnvailableURL() throws IOException {
    DefaultHttpClientFactory factory = new DefaultHttpClientFactory();
    factory.init(scope(new HashMap<>()));
    KeycloakSession session = new DefaultKeycloakSession(new DefaultKeycloakSessionFactory());
    HttpClientProvider provider = factory.create(session);
    try (CloseableHttpClient httpClient = (CloseableHttpClient) provider.getHttpClient()) {
        Optional<String> testURL = getTestURL();
        Assume.assumeTrue("Could not get test url for domain", testURL.isPresent());
        httpClient.execute(new HttpGet(testURL.get()));
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) DefaultKeycloakSessionFactory(org.keycloak.services.DefaultKeycloakSessionFactory) HashMap(java.util.HashMap) DefaultKeycloakSession(org.keycloak.services.DefaultKeycloakSession) KeycloakSession(org.keycloak.models.KeycloakSession) HttpGet(org.apache.http.client.methods.HttpGet) DefaultKeycloakSession(org.keycloak.services.DefaultKeycloakSession) Test(org.junit.Test)

Example 5 with DefaultKeycloakSessionFactory

use of org.keycloak.services.DefaultKeycloakSessionFactory in project keycloak by keycloak.

the class KeycloakApplication method createSessionFactory.

public static KeycloakSessionFactory createSessionFactory() {
    DefaultKeycloakSessionFactory factory = new DefaultKeycloakSessionFactory();
    factory.init();
    return factory;
}
Also used : DefaultKeycloakSessionFactory(org.keycloak.services.DefaultKeycloakSessionFactory)

Aggregations

DefaultKeycloakSessionFactory (org.keycloak.services.DefaultKeycloakSessionFactory)6 Test (org.junit.Test)3 KeycloakSession (org.keycloak.models.KeycloakSession)3 DefaultKeycloakSession (org.keycloak.services.DefaultKeycloakSession)3 HashMap (java.util.HashMap)2 HttpGet (org.apache.http.client.methods.HttpGet)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 Map (java.util.Map)1 CustomChangeException (liquibase.exception.CustomChangeException)1 SetupException (liquibase.exception.SetupException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 BeforeClass (org.junit.BeforeClass)1 Scope (org.keycloak.Config.Scope)1 AuthorizationSpi (org.keycloak.authorization.AuthorizationSpi)1 DefaultAuthorizationProviderFactory (org.keycloak.authorization.DefaultAuthorizationProviderFactory)1 StoreFactorySpi (org.keycloak.authorization.store.StoreFactorySpi)1 ClusterSpi (org.keycloak.cluster.ClusterSpi)1 ComponentFactoryProviderFactory (org.keycloak.component.ComponentFactoryProviderFactory)1 ComponentFactorySpi (org.keycloak.component.ComponentFactorySpi)1 EventStoreSpi (org.keycloak.events.EventStoreSpi)1