Search in sources :

Example 16 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class BadRealmTest method testBadRealmName.

@Test
@ModelTest
public void testBadRealmName(KeycloakSession session) {
    RealmManager manager = new RealmManager(session);
    try {
        manager.createRealm(id, name + script);
        fail();
    } catch (ReservedCharValidator.ReservedCharException ex) {
    }
}
Also used : ReservedCharValidator(org.keycloak.utils.ReservedCharValidator) RealmManager(org.keycloak.services.managers.RealmManager) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Example 17 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class ImportTest method importWithoutRequestContext.

// KEYCLOAK-12921 NPE importing realm with no request context
@Test
public void importWithoutRequestContext() throws IOException {
    final String realmString = IOUtils.toString(getClass().getResourceAsStream("/model/realm-validation.json"), StandardCharsets.UTF_8);
    testingClient.server().run(session -> {
        RealmRepresentation testRealm = JsonSerialization.readValue(realmString, RealmRepresentation.class);
        AtomicReference<Throwable> err = new AtomicReference<>();
        // Need a new thread to not get context from thread processing request to run-on-server endpoint
        Thread t = new Thread(() -> {
            try {
                KeycloakSession ses = session.getKeycloakSessionFactory().create();
                ses.getContext().setRealm(session.getContext().getRealm());
                ses.getTransactionManager().begin();
                RealmModel realmModel = new RealmManager(ses).importRealm(testRealm);
                ses.getTransactionManager().commit();
                ses.close();
                ses = session.getKeycloakSessionFactory().create();
                ses.getTransactionManager().begin();
                session.realms().removeRealm(realmModel.getId());
                ses.getTransactionManager().commit();
                ses.close();
            } catch (Throwable th) {
                err.set(th);
            }
        });
        synchronized (t) {
            t.start();
            try {
                t.wait(10000);
            } catch (InterruptedException e) {
                throw new RunOnServerException(e);
            }
        }
        if (err.get() != null) {
            throw new RunOnServerException(err.get());
        }
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) KeycloakSession(org.keycloak.models.KeycloakSession) AtomicReference(java.util.concurrent.atomic.AtomicReference) RealmManager(org.keycloak.services.managers.RealmManager) RunOnServerException(org.keycloak.testsuite.runonserver.RunOnServerException) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 18 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class AuthenticationSessionProviderTest method testOnClientRemoved.

@Test
@ModelTest
public void testOnClientRemoved(KeycloakSession session) {
    AtomicReference<String> tab1ID = new AtomicReference<>();
    AtomicReference<String> tab2ID = new AtomicReference<>();
    AtomicReference<String> authSessionID = new AtomicReference<>();
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesRealmRemoved1) -> {
        KeycloakSession currentSession = sesRealmRemoved1;
        RealmModel realm = currentSession.realms().getRealm("test");
        authSessionID.set(currentSession.authenticationSessions().createRootAuthenticationSession(realm).getId());
        AuthenticationSessionModel authSession1 = currentSession.authenticationSessions().getRootAuthenticationSession(realm, authSessionID.get()).createAuthenticationSession(realm.getClientByClientId("test-app"));
        AuthenticationSessionModel authSession2 = currentSession.authenticationSessions().getRootAuthenticationSession(realm, authSessionID.get()).createAuthenticationSession(realm.getClientByClientId("third-party"));
        tab1ID.set(authSession1.getTabId());
        tab2ID.set(authSession2.getTabId());
        authSession1.setAuthNote("foo", "bar");
        authSession2.setAuthNote("foo", "baz");
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesRealmRemoved1) -> {
        KeycloakSession currentSession = sesRealmRemoved1;
        RealmModel realm = currentSession.realms().getRealm("test");
        RootAuthenticationSessionModel rootAuthSession = currentSession.authenticationSessions().getRootAuthenticationSession(realm, authSessionID.get());
        assertThat(rootAuthSession.getAuthenticationSessions().size(), is(2));
        assertThat(rootAuthSession.getAuthenticationSession(realm.getClientByClientId("test-app"), tab1ID.get()).getAuthNote("foo"), is("bar"));
        assertThat(rootAuthSession.getAuthenticationSession(realm.getClientByClientId("third-party"), tab2ID.get()).getAuthNote("foo"), is("baz"));
        new ClientManager(new RealmManager(currentSession)).removeClient(realm, realm.getClientByClientId("third-party"));
    });
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesRealmRemoved1) -> {
        KeycloakSession currentSession = sesRealmRemoved1;
        RealmModel realm = currentSession.realms().getRealm("test");
        RootAuthenticationSessionModel rootAuthSession = currentSession.authenticationSessions().getRootAuthenticationSession(realm, authSessionID.get());
        assertThat(rootAuthSession.getAuthenticationSession(realm.getClientByClientId("test-app"), tab1ID.get()).getAuthNote("foo"), is("bar"));
        assertThat(rootAuthSession.getAuthenticationSession(realm.getClientByClientId("third-party"), tab2ID.get()), nullValue());
        // Revert client
        realm.addClient("third-party");
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) AuthenticationSessionModel(org.keycloak.sessions.AuthenticationSessionModel) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) KeycloakSession(org.keycloak.models.KeycloakSession) RootAuthenticationSessionModel(org.keycloak.sessions.RootAuthenticationSessionModel) ClientManager(org.keycloak.services.managers.ClientManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) RealmManager(org.keycloak.services.managers.RealmManager) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) ModelTest(org.keycloak.testsuite.arquillian.annotation.ModelTest) Test(org.junit.Test) AbstractTestRealmKeycloakTest(org.keycloak.testsuite.AbstractTestRealmKeycloakTest)

Example 19 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class RealmsResource method init.

private RealmModel init(String realmName) {
    RealmManager realmManager = new RealmManager(session);
    RealmModel realm = realmManager.getRealmByName(realmName);
    if (realm == null) {
        throw new NotFoundException("Realm does not exist");
    }
    session.getContext().setRealm(realm);
    return realm;
}
Also used : RealmModel(org.keycloak.models.RealmModel) NotFoundException(javax.ws.rs.NotFoundException) RealmManager(org.keycloak.services.managers.RealmManager)

Example 20 with RealmManager

use of org.keycloak.services.managers.RealmManager in project keycloak by keycloak.

the class UserStorageFailureTest method addProvidersBeforeTest.

@Before
public void addProvidersBeforeTest() {
    ComponentRepresentation memProvider = new ComponentRepresentation();
    memProvider.setName("failure");
    memProvider.setProviderId(FailableHardcodedStorageProviderFactory.PROVIDER_ID);
    memProvider.setProviderType(UserStorageProvider.class.getName());
    memProvider.setConfig(new MultivaluedHashMap<>());
    memProvider.getConfig().putSingle("priority", Integer.toString(0));
    failureProviderId = addComponent(memProvider);
    if (initialized)
        return;
    final String authServerRoot = OAuthClient.AUTH_SERVER_ROOT;
    testingClient.server().run(session -> {
        RealmManager manager = new RealmManager(session);
        RealmModel appRealm = manager.getRealmByName(AuthRealm.TEST);
        ClientModel offlineClient = appRealm.addClient("offline-client");
        offlineClient.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
        offlineClient.setEnabled(true);
        offlineClient.setDirectAccessGrantsEnabled(true);
        offlineClient.setSecret("secret");
        HashSet<String> redirects = new HashSet<>();
        redirects.add(authServerRoot + "/offline-client");
        offlineClient.setRedirectUris(redirects);
        offlineClient.setServiceAccountsEnabled(true);
        offlineClient.setFullScopeAllowed(true);
        UserModel serviceAccount = manager.getSession().users().addUser(appRealm, ServiceAccountConstants.SERVICE_ACCOUNT_USER_PREFIX + offlineClient.getClientId());
        serviceAccount.setEnabled(true);
        RoleModel role = appRealm.getRole("offline_access");
        Assert.assertNotNull(role);
        serviceAccount.grantRole(role);
        serviceAccount.setServiceAccountClientLink(offlineClient.getClientId());
        UserModel localUser = manager.getSession().userLocalStorage().addUser(appRealm, LOCAL_USER);
        localUser.setEnabled(true);
    });
    initialized = true;
}
Also used : ComponentRepresentation(org.keycloak.representations.idm.ComponentRepresentation) RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) CachedUserModel(org.keycloak.models.cache.CachedUserModel) ClientModel(org.keycloak.models.ClientModel) UserStorageProvider(org.keycloak.storage.UserStorageProvider) RoleModel(org.keycloak.models.RoleModel) RealmManager(org.keycloak.services.managers.RealmManager) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

RealmManager (org.keycloak.services.managers.RealmManager)47 RealmModel (org.keycloak.models.RealmModel)34 Test (org.junit.Test)19 UserModel (org.keycloak.models.UserModel)17 KeycloakSession (org.keycloak.models.KeycloakSession)16 ClientModel (org.keycloak.models.ClientModel)13 ClientManager (org.keycloak.services.managers.ClientManager)11 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)8 Path (javax.ws.rs.Path)6 UserSessionModel (org.keycloak.models.UserSessionModel)6 UserManager (org.keycloak.models.UserManager)5 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)4 LinkedList (java.util.LinkedList)3 GET (javax.ws.rs.GET)3 NotFoundException (javax.ws.rs.NotFoundException)3 Produces (javax.ws.rs.Produces)3 UserConsentModel (org.keycloak.models.UserConsentModel)3 ClientPolicyException (org.keycloak.services.clientpolicy.ClientPolicyException)3