|
|
|
@ -19,6 +19,7 @@ import org.keycloak.TokenVerifier;
|
|
|
|
|
import org.keycloak.admin.client.Keycloak;
|
|
|
|
|
import org.keycloak.admin.client.KeycloakBuilder;
|
|
|
|
|
import org.keycloak.admin.client.resource.ClientResource;
|
|
|
|
|
import org.keycloak.admin.client.resource.GroupResource;
|
|
|
|
|
import org.keycloak.admin.client.resource.PolicyResource;
|
|
|
|
|
import org.keycloak.admin.client.resource.RealmResource;
|
|
|
|
|
import org.keycloak.admin.client.resource.ResourceResource;
|
|
|
|
@ -31,6 +32,7 @@ import org.keycloak.jose.jwk.JWK;
|
|
|
|
|
import org.keycloak.jose.jwk.JWKParser;
|
|
|
|
|
import org.keycloak.representations.JsonWebToken;
|
|
|
|
|
import org.keycloak.representations.idm.ClientRepresentation;
|
|
|
|
|
import org.keycloak.representations.idm.GroupRepresentation;
|
|
|
|
|
import org.keycloak.representations.idm.RoleRepresentation;
|
|
|
|
|
import org.keycloak.representations.idm.UserRepresentation;
|
|
|
|
|
import org.keycloak.representations.idm.authorization.DecisionStrategy;
|
|
|
|
@ -83,7 +85,8 @@ public class KeycloakHelper {
|
|
|
|
|
.password(password).clientId(encodedClientId).resteasyClient(resteasyClient).build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Keycloak newKeycloak(String realm, String clientId, String clientSecret) throws UnsupportedEncodingException {
|
|
|
|
|
public Keycloak newKeycloak(String realm, String clientId, String clientSecret)
|
|
|
|
|
throws UnsupportedEncodingException {
|
|
|
|
|
String encodedClientId = URLEncoder.encode(clientId, "UTF-8");
|
|
|
|
|
return KeycloakBuilder.builder().serverUrl(serverUrl).realm(realm).grantType(OAuth2Constants.CLIENT_CREDENTIALS)
|
|
|
|
|
.clientId(encodedClientId).clientSecret(clientSecret)
|
|
|
|
@ -98,26 +101,6 @@ public class KeycloakHelper {
|
|
|
|
|
return JWKParser.create(JWKSUtils.getKeyForUse(jsonWebKeySet, JWK.Use.SIG)).toPublicKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Realm is too complex to configure it in depth with this helper. Please do it with the Web UI
|
|
|
|
|
// public RealmResource addRealm(Keycloak keycloak, String realm, String displayName, String displayNameHtml,
|
|
|
|
|
// boolean enabled) throws KeycloakResourceCreationException {
|
|
|
|
|
// if (keycloak.realm(realm) != null) {
|
|
|
|
|
// throw new KeycloakResourceCreationException("Realm already present on server: " + realm, null);
|
|
|
|
|
// }
|
|
|
|
|
// RealmRepresentation newRealmRepresentation = new RealmRepresentation();
|
|
|
|
|
// newRealmRepresentation.setRealm(realm);
|
|
|
|
|
// newRealmRepresentation.setId(realm);
|
|
|
|
|
// newRealmRepresentation.setDisplayName(displayName);
|
|
|
|
|
// newRealmRepresentation.setDisplayNameHtml(displayNameHtml);
|
|
|
|
|
// newRealmRepresentation.setEnabled(enabled);
|
|
|
|
|
// try {
|
|
|
|
|
// keycloak.realms().create(newRealmRepresentation);
|
|
|
|
|
// return keycloak.realms().realm(realm);
|
|
|
|
|
// } catch (ClientErrorException e) {
|
|
|
|
|
// throw new KeycloakResourceCreationException("While creating new realm: " + realm, null);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public UserResource findUser(RealmResource realmResource, String username) {
|
|
|
|
|
List<UserRepresentation> results = realmResource.users().search(username);
|
|
|
|
|
return results.size() > 0 ? realmResource.users().get(results.get(0).getId()) : null;
|
|
|
|
@ -169,7 +152,6 @@ public class KeycloakHelper {
|
|
|
|
|
return realm.clients().get(realm.clients().findByClientId(encodedClientId).get(0).getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ClientResource addPublicClient(RealmResource realm, String clientId, String name, String description,
|
|
|
|
|
String rootUrl, String loginTheme) throws KeycloakResourceCreationException, UnsupportedEncodingException {
|
|
|
|
|
|
|
|
|
@ -223,6 +205,24 @@ public class KeycloakHelper {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GroupResource findGroupByPath(RealmResource realm, String groupPath) throws UnsupportedEncodingException {
|
|
|
|
|
GroupRepresentation group = realm.getGroupByPath(groupPath);
|
|
|
|
|
if (group != null) {
|
|
|
|
|
return realm.groups().group(group.getId());
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void mapGroupToCLientRole(GroupResource group, ClientResource client, String roleName) {
|
|
|
|
|
mapGroupToCLientRole(group, client, client.roles().get(roleName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void mapGroupToCLientRole(GroupResource group, ClientResource client, RoleResource role) {
|
|
|
|
|
group.roles().clientLevel(client.toRepresentation().getId())
|
|
|
|
|
.add(Collections.singletonList(role.toRepresentation()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RoleResource addRole(ClientResource clientResource, boolean clientRole, String id, String name,
|
|
|
|
|
String description, String containerId) {
|
|
|
|
|
|
|
|
|
|