use of org.keycloak.representations.idm.ComponentRepresentation 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;
}
use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.
the class UserStorageTest method testMaxLifespan.
@Test
public void testMaxLifespan() {
ApiUtil.findUserByUsername(testRealmResource(), "thor");
// set eviction to 1 hour from now
ComponentRepresentation propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.MAX_LIFESPAN.name());
// 1 hour in milliseconds
propProviderRW.getConfig().putSingle(MAX_LIFESPAN, Long.toString(1 * 60 * 60 * 1000));
testRealmResource().components().component(propProviderRWId).update(propProviderRW);
// now
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
UserModel user = session.users().getUserByUsername(realm, "thor");
System.out.println("User class: " + user.getClass());
// should still be cached
Assert.assertTrue(user instanceof CachedUserModel);
});
// 1/2 hour in future
setTimeOffset(1 / 2 * 60 * 60);
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
UserModel user = session.users().getUserByUsername(realm, "thor");
System.out.println("User class: " + user.getClass());
// should still be cached
Assert.assertTrue(user instanceof CachedUserModel);
});
// 2 hours in future
setTimeOffset(2 * 60 * 60);
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
UserModel user = session.users().getUserByUsername(realm, "thor");
System.out.println("User class: " + user.getClass());
// should be evicted
Assert.assertFalse(user instanceof CachedUserModel);
});
}
use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.
the class UserStorageTest method testNoCache.
@Test
public void testNoCache() {
ApiUtil.findUserByUsername(testRealmResource(), "thor");
// set NO_CACHE policy
ComponentRepresentation propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.NO_CACHE.name());
testRealmResource().components().component(propProviderRWId).update(propProviderRW);
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
UserModel user = session.users().getUserByUsername(realm, "thor");
System.out.println("User class: " + user.getClass());
// should be evicted
Assert.assertFalse(user instanceof CachedUserModel);
});
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
UserModel thor2 = session.users().getUserByUsername(realm, "thor");
Assert.assertFalse(thor2 instanceof CachedUserModel);
});
propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.DEFAULT.name());
propProviderRW.getConfig().remove("evictionHour");
propProviderRW.getConfig().remove("evictionMinute");
propProviderRW.getConfig().remove("evictionDay");
testRealmResource().components().component(propProviderRWId).update(propProviderRW);
testingClient.server().run(session -> {
RealmModel realm = session.realms().getRealmByName("test");
UserModel thor = session.users().getUserByUsername(realm, "thor");
System.out.println("Foo");
});
}
use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.
the class UserStorageTest method setDailyEvictionTime.
private void setDailyEvictionTime(int hour, int minutes) {
if (hour < 0 || hour > 23) {
throw new IllegalArgumentException("hour == " + hour);
}
if (minutes < 0 || minutes > 59) {
throw new IllegalArgumentException("minutes == " + minutes);
}
ComponentRepresentation propProviderRW = testRealmResource().components().component(propProviderRWId).toRepresentation();
propProviderRW.getConfig().putSingle(CACHE_POLICY, CachePolicy.EVICT_DAILY.name());
propProviderRW.getConfig().putSingle(EVICTION_HOUR, String.valueOf(hour));
propProviderRW.getConfig().putSingle(EVICTION_MINUTE, String.valueOf(minutes));
testRealmResource().components().component(propProviderRWId).update(propProviderRW);
}
use of org.keycloak.representations.idm.ComponentRepresentation in project keycloak by keycloak.
the class UserStorageTest method newPropProviderRW.
protected ComponentRepresentation newPropProviderRW() {
ComponentRepresentation propProviderRW = new ComponentRepresentation();
propProviderRW.setName("user-props");
propProviderRW.setProviderId(UserPropertyFileStorageFactory.PROVIDER_ID);
propProviderRW.setProviderType(UserStorageProvider.class.getName());
propProviderRW.setConfig(new MultivaluedHashMap<>());
propProviderRW.getConfig().putSingle("priority", Integer.toString(2));
propProviderRW.getConfig().putSingle("propertyFile", CONFIG_DIR.getAbsolutePath() + File.separator + "user-password.properties");
propProviderRW.getConfig().putSingle("federatedStorage", "true");
return propProviderRW;
}
Aggregations