use of org.keycloak.models.KeycloakSession in project keycloak by keycloak.
the class AuthenticationSessionProviderTest method setAccessCodeLifespan.
// If parameter is -1, then the parameter won't change.
private void setAccessCodeLifespan(KeycloakSession session, int lifespan, int lifespanUserAction, int lifespanLogin) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionLifespan) -> {
KeycloakSession currentSession = sessionLifespan;
RealmModel realm = currentSession.realms().getRealm("test");
if (lifespan != -1)
realm.setAccessCodeLifespan(lifespan);
if (lifespanUserAction != -1)
realm.setAccessCodeLifespanUserAction(lifespanUserAction);
if (lifespanLogin != -1)
realm.setAccessCodeLifespanLogin(lifespanLogin);
});
}
use of org.keycloak.models.KeycloakSession in project keycloak by keycloak.
the class AuthenticationSessionProviderTest method testExpiredOffset.
private void testExpiredOffset(KeycloakSession session, int offset, boolean isSessionNull, String authSessionID) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionExp) -> {
KeycloakSession currentSession = sessionExp;
RealmModel realm = currentSession.realms().getRealm("test");
Time.setOffset(offset);
currentSession.authenticationSessions().removeExpired(realm);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionExpVerify) -> {
KeycloakSession currentSession = sessionExpVerify;
RealmModel realm = currentSession.realms().getRealm("test");
if (isSessionNull)
assertThat(currentSession.authenticationSessions().getRootAuthenticationSession(realm, authSessionID), nullValue());
else
assertThat(currentSession.authenticationSessions().getRootAuthenticationSession(realm, authSessionID), notNullValue());
});
}
use of org.keycloak.models.KeycloakSession in project keycloak by keycloak.
the class CompositeRolesModelTest method testNoClientID.
@Test
@ModelTest
public void testNoClientID(KeycloakSession session) {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Unknown client specification in scope mappings: some-client");
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession session1) -> {
try {
// RealmManager manager = new RealmManager(session1);
RealmRepresentation rep = loadJson(getClass().getResourceAsStream("/model/testrealm-noclient-id.json"), RealmRepresentation.class);
rep.setId("TestNoClientID");
// manager.importRealm(rep);
adminClient.realms().create(rep);
} catch (RuntimeException e) {
}
});
}
use of org.keycloak.models.KeycloakSession in project keycloak by keycloak.
the class UserModelTest method persistUser.
@Test
@ModelTest
public void persistUser(KeycloakSession session) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sesPersistUser) -> {
KeycloakSession currentSession = sesPersistUser;
RealmModel realm = currentSession.realms().getRealmByName("original");
UserModel user = currentSession.users().addUser(realm, "user");
user.setFirstName("first-name");
user.setLastName("last-name");
user.setEmail("email");
assertNotNull(user.getCreatedTimestamp());
// test that timestamp is current with 10s tollerance
Assert.assertTrue((System.currentTimeMillis() - user.getCreatedTimestamp()) < 10000);
user.addRequiredAction(RequiredAction.CONFIGURE_TOTP);
user.addRequiredAction(RequiredAction.UPDATE_PASSWORD);
RealmModel searchRealm = currentSession.realms().getRealm(realm.getId());
UserModel persisted = currentSession.users().getUserByUsername(searchRealm, "user");
assertUserModel(user, persisted);
searchRealm = currentSession.realms().getRealm(realm.getId());
UserModel persisted2 = currentSession.users().getUserById(searchRealm, user.getId());
assertUserModel(user, persisted2);
Map<String, String> attributes = new HashMap<>();
attributes.put(UserModel.LAST_NAME, "last-name");
List<UserModel> search = currentSession.users().searchForUserStream(realm, attributes).collect(Collectors.toList());
Assert.assertThat(search, hasSize(1));
Assert.assertThat(search.get(0).getUsername(), equalTo("user"));
attributes.clear();
attributes.put(UserModel.EMAIL, "email");
search = currentSession.users().searchForUserStream(realm, attributes).collect(Collectors.toList());
Assert.assertThat(search, hasSize(1));
Assert.assertThat(search.get(0).getUsername(), equalTo("user"));
attributes.clear();
attributes.put(UserModel.LAST_NAME, "last-name");
attributes.put(UserModel.EMAIL, "email");
search = currentSession.users().searchForUserStream(realm, attributes).collect(Collectors.toList());
Assert.assertThat(search, hasSize(1));
Assert.assertThat(search.get(0).getUsername(), equalTo("user"));
});
}
use of org.keycloak.models.KeycloakSession in project keycloak by keycloak.
the class UserSessionProviderTest method testRemoveUserSessionsByRealm.
@Test
@ModelTest
public void testRemoveUserSessionsByRealm(KeycloakSession session) {
RealmModel realm = session.realms().getRealmByName("test");
createSessions(session);
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession kcSession) -> {
kcSession.sessions().removeUserSessions(realm);
});
assertEquals(0, session.sessions().getUserSessionsStream(realm, session.users().getUserByUsername(realm, "user1")).count());
assertEquals(0, session.sessions().getUserSessionsStream(realm, session.users().getUserByUsername(realm, "user2")).count());
}
Aggregations