|
| 1 | +package com.hawk.keycloak; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import org.keycloak.models.*; |
| 5 | +import org.keycloak.models.utils.KeycloakModelUtils; |
| 6 | +import org.keycloak.protocol.oidc.mappers.GroupMembershipMapper; |
| 7 | +import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper; |
| 8 | +import org.keycloak.protocol.oidc.mappers.UserClientRoleMappingMapper; |
| 9 | +import org.keycloak.protocol.oidc.mappers.UserRealmRoleMappingMapper; |
| 10 | + |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +@RequiredArgsConstructor |
| 14 | +public class ClientScopeRegistration { |
| 15 | + public final static String CLIENT_SCOPE_NAME = "hawk-client"; |
| 16 | + |
| 17 | + final private KeycloakSessionFactory sessionFactory; |
| 18 | + |
| 19 | + public void register() { |
| 20 | + KeycloakModelUtils.runJobInTransaction(sessionFactory, (KeycloakSession session) -> { |
| 21 | + Stream<RealmModel> realms = session.realms().getRealmsStream(); |
| 22 | + realms.forEach(this::registerInRealm); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + public void registerInRealm(RealmModel realm) { |
| 27 | + |
| 28 | + ClientScopeModel scope = realm.getClientScopesStream() |
| 29 | + .filter(s -> s.getName().equals(CLIENT_SCOPE_NAME)) |
| 30 | + .findFirst() |
| 31 | + .orElse(null); |
| 32 | + |
| 33 | + if (scope != null) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // Create the scope |
| 38 | + scope = realm.addClientScope(CLIENT_SCOPE_NAME); |
| 39 | + scope.setDescription("Required claims for the hawk-client library"); |
| 40 | + scope.setProtocol("openid-connect"); |
| 41 | + scope.setIncludeInTokenScope(false); |
| 42 | + scope.setDisplayOnConsentScreen(false); |
| 43 | + |
| 44 | + // Realm Roles |
| 45 | + ProtocolMapperModel realmRoleMapper = UserRealmRoleMappingMapper.create( |
| 46 | + null, |
| 47 | + "realm roles", |
| 48 | + "hawk.roles.realm", |
| 49 | + false, |
| 50 | + false, |
| 51 | + false, |
| 52 | + true |
| 53 | + ); |
| 54 | + realmRoleMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_USERINFO, "true"); |
| 55 | + scope.addProtocolMapper(realmRoleMapper); |
| 56 | + |
| 57 | + // Client Roles |
| 58 | + ProtocolMapperModel clientRoleMapper = UserClientRoleMappingMapper.create( |
| 59 | + null, |
| 60 | + null, |
| 61 | + "client roles", |
| 62 | + "hawk.roles.client.${client_id}", |
| 63 | + false, |
| 64 | + false, |
| 65 | + false, |
| 66 | + true |
| 67 | + ); |
| 68 | + clientRoleMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_USERINFO, "true"); |
| 69 | + scope.addProtocolMapper(clientRoleMapper); |
| 70 | + |
| 71 | + // Groups |
| 72 | + ProtocolMapperModel groupMapper = GroupMembershipMapper.create( |
| 73 | + "groups", |
| 74 | + "hawk.groups", |
| 75 | + false, |
| 76 | + null, |
| 77 | + false, |
| 78 | + false, |
| 79 | + false |
| 80 | + ); |
| 81 | + groupMapper.getConfig().put(OIDCAttributeMapperHelper.INCLUDE_IN_USERINFO, "true"); |
| 82 | + scope.addProtocolMapper(groupMapper); |
| 83 | + } |
| 84 | +} |
0 commit comments