Search in sources :

Example 1 with ClientResource

use of org.keycloak.admin.client.resource.ClientResource in project openremote by openremote.

the class ManagerKeycloakIdentityProvider method createClientApplication.

public void createClientApplication(ClientRequestInfo clientRequestInfo, String realm) {
    ClientsResource clientsResource = getRealms(clientRequestInfo).realm(realm).clients();
    ClientRepresentation client = createClientApplication(realm, KEYCLOAK_CLIENT_ID, "OpenRemote", devMode);
    clientsResource.create(client);
    client = clientsResource.findByClientId(client.getClientId()).get(0);
    ClientResource clientResource = clientsResource.get(client.getId());
    addDefaultRoles(clientResource.roles());
}
Also used : ClientsResource(org.keycloak.admin.client.resource.ClientsResource) ClientResource(org.keycloak.admin.client.resource.ClientResource)

Example 2 with ClientResource

use of org.keycloak.admin.client.resource.ClientResource in project openremote by openremote.

the class KeycloakInitSetup method onStart.

@Override
public void onStart() throws Exception {
    super.onStart();
    // Configure the master realm
    RealmRepresentation masterRealm = masterRealmResource.toRepresentation();
    masterRealm.setDisplayName("Master");
    // Set SMTP server, theme, timeouts, etc.
    keycloakProvider.configureRealm(masterRealm, emailConfig);
    masterRealmResource.update(masterRealm);
    // Create our client application with its default roles
    keycloakProvider.createClientApplication(new ClientRequestInfo(null, accessToken), masterRealm.getRealm());
    // Get the client application ID so we can assign roles to users at the client
    // level (we can only check realm _or_ client application roles in @RolesAllowed!)
    String clientObjectId = getClientObjectId(masterClientsResource);
    ClientResource clientResource = masterClientsResource.get(clientObjectId);
    RolesResource rolesResource = clientResource.roles();
    // Give admin all roles on application client level
    RoleRepresentation readRole = rolesResource.get(ClientRole.READ.getValue()).toRepresentation();
    RoleRepresentation writeRole = rolesResource.get(ClientRole.WRITE.getValue()).toRepresentation();
    masterUsersResource.search(MASTER_REALM_ADMIN_USER, null, null, null, null, null).stream().map(userRepresentation -> masterUsersResource.get(userRepresentation.getId())).forEach(adminUser -> {
        adminUser.roles().clientLevel(clientObjectId).add(Arrays.asList(readRole, writeRole));
        LOG.info("Assigned all application roles to 'admin' user");
        UserRepresentation adminRep = adminUser.toRepresentation();
        adminRep.setFirstName("System");
        adminRep.setLastName("Administrator");
        adminUser.update(adminRep);
    });
}
Also used : RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) Container(org.openremote.container.Container) ClientRequestInfo(org.openremote.container.web.ClientRequestInfo) Arrays(java.util.Arrays) MASTER_REALM_ADMIN_USER(org.openremote.model.Constants.MASTER_REALM_ADMIN_USER) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) ClientRole(org.openremote.model.security.ClientRole) AbstractKeycloakSetup(org.openremote.manager.setup.AbstractKeycloakSetup) RolesResource(org.keycloak.admin.client.resource.RolesResource) Logger(java.util.logging.Logger) ClientResource(org.keycloak.admin.client.resource.ClientResource) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ClientRequestInfo(org.openremote.container.web.ClientRequestInfo) ClientResource(org.keycloak.admin.client.resource.ClientResource) RolesResource(org.keycloak.admin.client.resource.RolesResource) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Aggregations

ClientResource (org.keycloak.admin.client.resource.ClientResource)2 Arrays (java.util.Arrays)1 Logger (java.util.logging.Logger)1 ClientsResource (org.keycloak.admin.client.resource.ClientsResource)1 RolesResource (org.keycloak.admin.client.resource.RolesResource)1 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)1 RoleRepresentation (org.keycloak.representations.idm.RoleRepresentation)1 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)1 Container (org.openremote.container.Container)1 ClientRequestInfo (org.openremote.container.web.ClientRequestInfo)1 AbstractKeycloakSetup (org.openremote.manager.setup.AbstractKeycloakSetup)1 MASTER_REALM_ADMIN_USER (org.openremote.model.Constants.MASTER_REALM_ADMIN_USER)1 ClientRole (org.openremote.model.security.ClientRole)1