13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
17
16
package my .unifi .eset .keycloak .piidataencryption .utils ;
18
17
19
18
import jakarta .persistence .EntityManager ;
@@ -97,12 +96,16 @@ public static boolean shouldEncryptAttribute(KeycloakSession ks, String realmId,
97
96
if (attributeName .startsWith ("pii-" )) {
98
97
return true ;
99
98
}
100
- UserProfileProvider upp = ks .getProvider (UserProfileProvider .class );
101
- if (upp instanceof DeclarativeUserProfileProvider dup ) {
102
- UPAttribute upa = dup .getConfiguration ().getAttribute (attributeName );
103
- if (upa != null && upa .getValidations ().containsKey (PiiDataEncryptionValidatorProvider .ID )) {
104
- return Boolean .parseBoolean (String .valueOf (upa .getValidations ().get (PiiDataEncryptionValidatorProvider .ID ).getOrDefault ("enable" , false )));
99
+ try {
100
+ UserProfileProvider upp = ks .getProvider (UserProfileProvider .class );
101
+ if (upp instanceof DeclarativeUserProfileProvider dup ) {
102
+ UPAttribute upa = dup .getConfiguration ().getAttribute (attributeName );
103
+ if (upa != null && upa .getValidations ().containsKey (PiiDataEncryptionValidatorProvider .ID )) {
104
+ return Boolean .parseBoolean (String .valueOf (upa .getValidations ().get (PiiDataEncryptionValidatorProvider .ID ).getOrDefault ("enable" , false )));
105
+ }
105
106
}
107
+ } catch (Exception ex ) {
108
+ return false ;
106
109
}
107
110
return false ;
108
111
}
@@ -190,6 +193,7 @@ public static EncryptedUserAttributeEntity getEncryptedUserAttributeEntity(Entit
190
193
*/
191
194
public static void encryptExistingUserEntities (KeycloakSession ks , EntityManager em , RealmModel realm ) {
192
195
List <UserEntity > realmUsers = em .createQuery ("SELECT u FROM UserEntity u WHERE u.realmId = :realmId" , UserEntity .class ).setParameter ("realmId" , realm .getId ()).getResultList ();
196
+ logger .debugf ("Event: REALM_USERS_ENCRYPTION, Realm: %s, Total Users: %s" , realm .getId (), realmUsers .size ());
193
197
for (UserEntity user : realmUsers ) {
194
198
if (user .getServiceAccountClientLink () != null ) {
195
199
continue ; // skip service accounts
@@ -214,9 +218,8 @@ public static void encryptUserEntity(KeycloakSession ks, EntityManager em, UserE
214
218
return ;
215
219
}
216
220
EncryptedUserEntity eue = LogicUtils .getEncryptedUserEntity (em , ue , true );
217
- if (ue . getUsername (). length () == 40 && ue .getUsername (). matches ( "^[0-9a-fA-F]+$" )) {
221
+ if (isHash ( ue .getUsername ())) {
218
222
// somehow the value is already hashed so we skip it to avoid double hash/encrypt
219
- // we only need to check email because email has a specific string format
220
223
return ;
221
224
}
222
225
eue .setUsername (EncryptionUtils .encryptValue (ue .getUsername ()));
@@ -249,7 +252,7 @@ public static void encryptUserEntity(KeycloakSession ks, EntityManager em, UserE
249
252
public static void encryptUserAttributeEntity (KeycloakSession ks , EntityManager em , UserAttributeEntity uae ) {
250
253
if (shouldEncryptAttribute (ks , uae )) {
251
254
String value = uae .getValue ();
252
- if (value . length () == 40 && value . matches ( "^[0-9a-fA-F]+$" )) {
255
+ if (isHash ( value )) {
253
256
// somehow the value is already hashed so we skip it to avoid double hash/encrypt
254
257
return ;
255
258
}
@@ -365,6 +368,10 @@ public static String hash(String raw) {
365
368
return raw != null ? DigestUtils .sha1Hex (raw .trim ().toLowerCase ()) : null ;
366
369
}
367
370
371
+ public static boolean isHash (String value ) {
372
+ return value .length () == 40 && value .matches ("^[0-9a-fA-F]+$" );
373
+ }
374
+
368
375
// Makes this class un-instantiatable
369
376
private LogicUtils () {
370
377
}
0 commit comments