From 2e8af68f769d8ad50c79a0507e8a5b3e399005e7 Mon Sep 17 00:00:00 2001 From: Software Magico Date: Thu, 9 May 2024 13:59:05 +0200 Subject: [PATCH] Adding some compatibility with old non-encrypted database. --- .../providers/AuthenticatedUserProvider.java | 22 ++++++++++++++----- .../AuthenticatedUserRepository.java | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/AuthenticatedUserProvider.java b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/AuthenticatedUserProvider.java index ca4d067d6..008a26d22 100644 --- a/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/AuthenticatedUserProvider.java +++ b/backend/kendo-tournament-core/src/main/java/com/softwaremagico/kt/core/providers/AuthenticatedUserProvider.java @@ -38,6 +38,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.softwaremagico.kt.persistence.encryption.KeyProperty.getDatabaseEncryptionKey; + @Repository public class AuthenticatedUserProvider { @@ -67,11 +69,21 @@ public Optional findByUsername(String username) { guest.setRoles(Collections.singleton(GUEST_ROLE)); return Optional.of(guest); } - final Optional authenticatedUser = authenticatedUserRepository - .findByUsernameHash(username); - if (authenticatedUser.isPresent()) { - authenticatedUser.get().setUsernameHash(authenticatedUser.get().getUsername()); - return Optional.of(authenticatedUser.get()); + if (getDatabaseEncryptionKey() != null) { + //Username is encrypted, use hash + final Optional authenticatedUser = authenticatedUserRepository + .findByUsernameHash(username); + if (authenticatedUser.isPresent()) { + authenticatedUser.get().setUsernameHash(authenticatedUser.get().getUsername()); + return Optional.of(authenticatedUser.get()); + } + } else { + //Username is not encrypted, use username for compatibility with old databases. + final Optional authenticatedUser = authenticatedUserRepository + .findByUsername(username); + if (authenticatedUser.isPresent()) { + return Optional.of(authenticatedUser.get()); + } } final Optional participant = participantProvider.findByTokenUsername(username); if (participant.isPresent()) { diff --git a/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/repositories/AuthenticatedUserRepository.java b/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/repositories/AuthenticatedUserRepository.java index 439434ac3..dc075bd5e 100644 --- a/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/repositories/AuthenticatedUserRepository.java +++ b/backend/kendo-tournament-persistence/src/main/java/com/softwaremagico/kt/persistence/repositories/AuthenticatedUserRepository.java @@ -32,5 +32,6 @@ @Transactional public interface AuthenticatedUserRepository extends JpaRepository { + Optional findByUsername(String username); Optional findByUsernameHash(String username); }