|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Muhammad Lukman Nasaruddin <lukman.nasaruddin@gmail.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
1 | 16 | package my.unifi.eset.keycloak.piidataencryption.ldap; |
2 | 17 |
|
3 | | -import jakarta.persistence.EntityManager; |
4 | | -import my.unifi.eset.keycloak.piidataencryption.utils.LogicUtils; |
| 18 | +import java.util.Map; |
| 19 | +import org.keycloak.Config; |
5 | 20 | import org.keycloak.component.ComponentModel; |
6 | | -import org.keycloak.connections.jpa.JpaConnectionProvider; |
7 | 21 | import org.keycloak.models.KeycloakSession; |
8 | | -import org.keycloak.models.KeycloakSessionFactory; |
| 22 | +import org.keycloak.storage.ldap.LDAPIdentityStoreRegistry; |
| 23 | +import org.keycloak.storage.ldap.LDAPStorageProvider; |
9 | 24 | import org.keycloak.storage.ldap.LDAPStorageProviderFactory; |
10 | | -import org.keycloak.storage.ldap.idm.query.internal.LDAPQuery; |
11 | | -import org.keycloak.storage.user.SynchronizationResult; |
| 25 | +import org.keycloak.storage.ldap.idm.store.ldap.LDAPIdentityStore; |
| 26 | +import org.keycloak.storage.ldap.mappers.LDAPConfigDecorator; |
12 | 27 |
|
13 | 28 | public class EncryptedLDAPStorageProviderFactory extends LDAPStorageProviderFactory { |
14 | 29 |
|
| 30 | + LDAPIdentityStoreRegistry ldapStoreRegistryOverride; |
| 31 | + |
15 | 32 | @Override |
16 | 33 | public int order() { |
17 | 34 | return 1000; |
18 | 35 | } |
19 | 36 |
|
20 | 37 | @Override |
21 | | - protected SynchronizationResult syncImpl(KeycloakSessionFactory sessionFactory, LDAPQuery userQuery, String realmId, ComponentModel fedModel) { |
22 | | - SynchronizationResult result = super.syncImpl(sessionFactory, userQuery, realmId, fedModel); |
23 | | - KeycloakSession session = sessionFactory.create(); |
24 | | - EntityManager em = session.getProvider(JpaConnectionProvider.class).getEntityManager(); |
25 | | - LogicUtils.encryptExistingUserEntities(session, em, session.realms().getRealm(realmId)); |
26 | | - em.flush(); |
27 | | - return result; |
| 38 | + public void init(Config.Scope config) { |
| 39 | + super.init(config); |
| 40 | + this.ldapStoreRegistryOverride = new LDAPIdentityStoreRegistry(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void close() { |
| 45 | + super.close(); |
| 46 | + this.ldapStoreRegistryOverride = null; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public LDAPStorageProvider create(KeycloakSession session, ComponentModel model) { |
| 51 | + Map<ComponentModel, LDAPConfigDecorator> configDecorators = getLDAPConfigDecorators(session, model); |
| 52 | + LDAPIdentityStore ldapIdentityStore = this.ldapStoreRegistryOverride.getLdapStore(session, model, configDecorators); |
| 53 | + return new EncryptedLDAPStorageProvider(this, session, model, ldapIdentityStore); |
28 | 54 | } |
29 | 55 |
|
30 | 56 | } |
0 commit comments