use of org.keycloak.models.ClientScopeModel in project keycloak by keycloak.
the class RealmAdminResource method removeDefaultDefaultClientScope.
@DELETE
@NoCache
@Path("default-default-client-scopes/{clientScopeId}")
public void removeDefaultDefaultClientScope(@PathParam("clientScopeId") String clientScopeId) {
auth.clients().requireManageClientScopes();
ClientScopeModel clientScope = realm.getClientScopeById(clientScopeId);
if (clientScope == null) {
throw new NotFoundException("Client scope not found");
}
realm.removeDefaultClientScope(clientScope);
adminEvent.operation(OperationType.DELETE).resource(ResourceType.CLIENT_SCOPE).resourcePath(session.getContext().getUri()).success();
}
use of org.keycloak.models.ClientScopeModel in project keycloak by keycloak.
the class JpaRealmProvider method removeClientScope.
@Override
public boolean removeClientScope(RealmModel realm, String id) {
if (id == null)
return false;
ClientScopeModel clientScope = getClientScopeById(realm, id);
if (clientScope == null)
return false;
session.users().preRemove(clientScope);
realm.removeDefaultClientScope(clientScope);
ClientScopeEntity clientScopeEntity = em.find(ClientScopeEntity.class, id, LockModeType.PESSIMISTIC_WRITE);
em.createNamedQuery("deleteClientScopeClientMappingByClientScope").setParameter("clientScopeId", clientScope.getId()).executeUpdate();
em.createNamedQuery("deleteClientScopeRoleMappingByClientScope").setParameter("clientScope", clientScopeEntity).executeUpdate();
em.remove(clientScopeEntity);
session.getKeycloakSessionFactory().publish(new ClientScopeModel.ClientScopeRemovedEvent() {
@Override
public KeycloakSession getKeycloakSession() {
return session;
}
@Override
public ClientScopeModel getClientScope() {
return clientScope;
}
});
em.flush();
return true;
}
use of org.keycloak.models.ClientScopeModel in project keycloak by keycloak.
the class ClientModelTest method testDefaultDefaultClientScopes.
@Test
@ModelTest
public void testDefaultDefaultClientScopes(KeycloakSession session) {
AtomicReference<ClientScopeModel> scope1Atomic = new AtomicReference<>();
AtomicReference<ClientScopeModel> scope2Atomic = new AtomicReference<>();
AtomicReference<ClientScopeModel> scope3Atomic = new AtomicReference<>();
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope1) -> {
currentSession = sessionDefaultClientScope1;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
ClientScopeModel scope1 = realm.addClientScope("scope1");
scope1.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
scope1Atomic.set(scope1);
ClientScopeModel scope2 = realm.addClientScope("scope2");
scope2.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
scope2Atomic.set(scope2);
ClientScopeModel scope3 = realm.addClientScope("scope3");
scope3.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
scope3Atomic.set(scope3);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope2) -> {
currentSession = sessionDefaultClientScope2;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
ClientScopeModel scope1 = scope1Atomic.get();
ClientScopeModel scope2 = scope2Atomic.get();
ClientScopeModel scope3 = scope3Atomic.get();
scope1 = realm.getClientScopeById(scope1.getId());
scope2 = realm.getClientScopeById(scope2.getId());
scope3 = realm.getClientScopeById(scope3.getId());
realm.addDefaultClientScope(scope1, true);
realm.addDefaultClientScope(scope2, false);
realm.addDefaultClientScope(scope3, false);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope3) -> {
currentSession = sessionDefaultClientScope3;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.addClient("foo");
client.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope4) -> {
currentSession = sessionDefaultClientScope4;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.getClientByClientId("foo");
ClientScopeModel scope1 = scope1Atomic.get();
ClientScopeModel scope2 = scope2Atomic.get();
Map<String, ClientScopeModel> clientScopes1 = client.getClientScopes(true);
assertThat("Client Scope contains 'scope1':", clientScopes1.containsKey("scope1"), is(true));
assertThat("Client Scope contains 'scope2':", clientScopes1.containsKey("scope2"), is(false));
assertThat("Client Scope contains 'scope3':", clientScopes1.containsKey("scope3"), is(false));
Map<String, ClientScopeModel> clientScopes2 = client.getClientScopes(false);
assertThat("Client Scope contains 'scope1':", clientScopes2.containsKey("scope1"), is(false));
assertThat("Client Scope contains 'scope2':", clientScopes2.containsKey("scope2"), is(true));
assertThat("Client Scope contains 'scope3':", clientScopes2.containsKey("scope3"), is(true));
currentSession.clients().removeClient(realm, client.getId());
// Remove some realm default client scopes
realm.removeDefaultClientScope(scope1);
realm.removeDefaultClientScope(scope2);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope5) -> {
currentSession = sessionDefaultClientScope5;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.addClient("foo2");
client.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionDefaultClientScope5) -> {
currentSession = sessionDefaultClientScope5;
RealmModel realm = currentSession.realms().getRealmByName(realmName);
client = realm.getClientByClientId("foo2");
Map<String, ClientScopeModel> clientScopes1 = client.getClientScopes(true);
assertThat("Client Scope contains 'scope1':", clientScopes1.containsKey("scope1"), is(false));
assertThat("Client Scope contains 'scope2':", clientScopes1.containsKey("scope2"), is(false));
assertThat("Client Scope contains 'scope3':", clientScopes1.containsKey("scope3"), is(false));
Map<String, ClientScopeModel> clientScopes2 = client.getClientScopes(false);
assertThat("Client Scope contains 'scope1':", clientScopes2.containsKey("scope1"), is(false));
assertThat("Client Scope contains 'scope2':", clientScopes2.containsKey("scope2"), is(false));
assertThat("Client Scope contains 'scope3':", clientScopes2.containsKey("scope3"), is(true));
currentSession.clients().removeClient(realm, client.getId());
realm.removeClientScope(scope1Atomic.get().getId());
realm.removeClientScope(scope2Atomic.get().getId());
realm.removeDefaultClientScope(scope3Atomic.get());
realm.removeClientScope(scope3Atomic.get().getId());
});
}
use of org.keycloak.models.ClientScopeModel in project keycloak by keycloak.
the class UserConsentModelTest method updateWithClientScopeRemovalTest.
@Test
@ModelTest
public void updateWithClientScopeRemovalTest(KeycloakSession session) {
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession removalTestSession1) -> {
KeycloakSession currentSession = removalTestSession1;
RealmModel realm = currentSession.realms().getRealm("original");
ClientModel fooClient = realm.getClientByClientId("foo-client");
UserModel john = currentSession.users().getUserByUsername(realm, "john");
UserConsentModel johnConsent = currentSession.users().getConsentByClient(realm, john.getId(), fooClient.getId());
Assert.assertEquals(1, johnConsent.getGrantedClientScopes().size());
// Remove foo protocol mapper from johnConsent
ClientScopeModel fooScope = KeycloakModelUtils.getClientScopeByName(realm, "foo");
johnConsent.getGrantedClientScopes().remove(fooScope);
currentSession.users().updateConsent(realm, john.getId(), johnConsent);
});
KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession removalTestSession2) -> {
KeycloakSession currentSession = removalTestSession2;
RealmModel realm = currentSession.realms().getRealm("original");
ClientModel fooClient = realm.getClientByClientId("foo-client");
UserModel john = currentSession.users().getUserByUsername(realm, "john");
UserConsentModel johnConsent = currentSession.users().getConsentByClient(realm, john.getId(), fooClient.getId());
Assert.assertEquals(johnConsent.getGrantedClientScopes().size(), 0);
Assert.assertTrue("Created date should be less than last updated date", johnConsent.getCreatedDate() < johnConsent.getLastUpdatedDate());
});
}
use of org.keycloak.models.ClientScopeModel in project keycloak by keycloak.
the class JpaUserFederatedStorageProvider method updateGrantedConsentEntity.
// Update roles and protocolMappers to given consentEntity from the consentModel
private void updateGrantedConsentEntity(FederatedUserConsentEntity consentEntity, UserConsentModel consentModel) {
Collection<FederatedUserConsentClientScopeEntity> grantedClientScopeEntities = consentEntity.getGrantedClientScopes();
Collection<FederatedUserConsentClientScopeEntity> scopesToRemove = new HashSet<>(grantedClientScopeEntities);
for (ClientScopeModel clientScope : consentModel.getGrantedClientScopes()) {
FederatedUserConsentClientScopeEntity grantedClientScopeEntity = new FederatedUserConsentClientScopeEntity();
grantedClientScopeEntity.setUserConsent(consentEntity);
grantedClientScopeEntity.setScopeId(clientScope.getId());
// Check if it's already there
if (!grantedClientScopeEntities.contains(grantedClientScopeEntity)) {
em.persist(grantedClientScopeEntity);
em.flush();
grantedClientScopeEntities.add(grantedClientScopeEntity);
} else {
scopesToRemove.remove(grantedClientScopeEntity);
}
}
// Those mappers were no longer on consentModel and will be removed
for (FederatedUserConsentClientScopeEntity toRemove : scopesToRemove) {
grantedClientScopeEntities.remove(toRemove);
em.remove(toRemove);
}
consentEntity.setLastUpdatedDate(Time.currentTimeMillis());
em.flush();
}
Aggregations