Skip to content

Commit

Permalink
Adding some compatibility with old non-encrypted database.
Browse files Browse the repository at this point in the history
  • Loading branch information
softwaremagico committed May 9, 2024
1 parent 371a29c commit 2e8af68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -67,11 +69,21 @@ public Optional<IAuthenticatedUser> findByUsername(String username) {
guest.setRoles(Collections.singleton(GUEST_ROLE));
return Optional.of(guest);
}
final Optional<AuthenticatedUser> 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> 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> authenticatedUser = authenticatedUserRepository
.findByUsername(username);
if (authenticatedUser.isPresent()) {
return Optional.of(authenticatedUser.get());
}
}
final Optional<Participant> participant = participantProvider.findByTokenUsername(username);
if (participant.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
@Transactional
public interface AuthenticatedUserRepository extends JpaRepository<AuthenticatedUser, Integer> {

Optional<AuthenticatedUser> findByUsername(String username);
Optional<AuthenticatedUser> findByUsernameHash(String username);
}

0 comments on commit 2e8af68

Please sign in to comment.