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) {
}
}
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());
}
});
}
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");
});
}
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;
}
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;
}
Aggregations