fix: support getAuthenticatedUser() in OPDS (#2041)

This commit is contained in:
James Ward
2026-07-17 00:46:56 -04:00
committed by GitHub
parent 2cfe5d2da7
commit 108c5c10e4
5 changed files with 34 additions and 6 deletions
@@ -1,6 +1,7 @@
package org.booklore.config.security; package org.booklore.config.security;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.booklore.config.security.userdetails.OpdsUserDetails;
import org.booklore.model.dto.BookLoreUser; import org.booklore.model.dto.BookLoreUser;
import org.booklore.repository.ShelfRepository; import org.booklore.repository.ShelfRepository;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
@@ -14,8 +15,14 @@ public class SecurityUtil {
private BookLoreUser getCurrentUser() { private BookLoreUser getCurrentUser() {
var authentication = SecurityContextHolder.getContext().getAuthentication(); var authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof BookLoreUser user) { if (authentication != null) {
return user; if (authentication.getPrincipal() instanceof BookLoreUser user) {
return user;
}
if (authentication.getPrincipal() instanceof OpdsUserDetails opdsUser) {
return opdsUser.getUser();
}
} }
return null; return null;
} }
@@ -3,6 +3,7 @@ package org.booklore.config.security.service;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.booklore.model.dto.AccessTokenDto; import org.booklore.model.dto.AccessTokenDto;
import org.booklore.model.dto.OpdsUser;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.booklore.config.AppProperties; import org.booklore.config.AppProperties;
import org.booklore.config.security.JwtUtils; import org.booklore.config.security.JwtUtils;
@@ -85,13 +86,16 @@ public class AuthenticationService {
return null; return null;
} }
Object principal = authentication.getPrincipal(); Object principal = authentication.getPrincipal();
if (principal instanceof OpdsUserDetails opdsUser) {
principal = opdsUser.getUser();
}
if (principal instanceof BookLoreUser user) { if (principal instanceof BookLoreUser user) {
if (user.getId() != null && user.getId() != -1L) { if (user.getId() != null && user.getId() != -1L) {
defaultSettingInitializer.ensureDefaultSettings(user); defaultSettingInitializer.ensureDefaultSettings(user);
} }
return user; return user;
} }
throw new IllegalStateException("Authenticated principal is not of type BookLoreUser"); throw new IllegalStateException("Authenticated principal is not of expected type");
} }
public BookLoreUser getSystemUser() { public BookLoreUser getSystemUser() {
@@ -3,7 +3,10 @@ package org.booklore.config.security.service;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.booklore.config.security.userdetails.OpdsUserDetails; import org.booklore.config.security.userdetails.OpdsUserDetails;
import org.booklore.mapper.OpdsUserV2Mapper; import org.booklore.mapper.OpdsUserV2Mapper;
import org.booklore.mapper.custom.BookLoreUserTransformer;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.OpdsUserV2; import org.booklore.model.dto.OpdsUserV2;
import org.booklore.model.entity.BookLoreUserEntity;
import org.booklore.model.entity.OpdsUserV2Entity; import org.booklore.model.entity.OpdsUserV2Entity;
import org.booklore.repository.OpdsUserV2Repository; import org.booklore.repository.OpdsUserV2Repository;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
@@ -16,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor @RequiredArgsConstructor
public class OpdsUserDetailsService implements UserDetailsService { public class OpdsUserDetailsService implements UserDetailsService {
private final BookLoreUserTransformer bookLoreUserTransformer;
private final OpdsUserV2Repository opdsUserV2Repository; private final OpdsUserV2Repository opdsUserV2Repository;
private final OpdsUserV2Mapper opdsUserV2Mapper; private final OpdsUserV2Mapper opdsUserV2Mapper;
@@ -24,7 +28,11 @@ public class OpdsUserDetailsService implements UserDetailsService {
public OpdsUserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public OpdsUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
OpdsUserV2Entity userV2 = opdsUserV2Repository.findByUsername(username) OpdsUserV2Entity userV2 = opdsUserV2Repository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("Invalid credentials")); .orElseThrow(() -> new UsernameNotFoundException("Invalid credentials"));
BookLoreUser appUser = bookLoreUserTransformer.toDTO(userV2.getUser());
OpdsUserV2 mappedCredential = opdsUserV2Mapper.toDto(userV2); OpdsUserV2 mappedCredential = opdsUserV2Mapper.toDto(userV2);
return new OpdsUserDetails(mappedCredential); return new OpdsUserDetails(
appUser,
mappedCredential
);
} }
} }
@@ -1,5 +1,6 @@
package org.booklore.config.security.userdetails; package org.booklore.config.security.userdetails;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.OpdsUserV2; import org.booklore.model.dto.OpdsUserV2;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@@ -12,6 +13,7 @@ import java.util.List;
@Getter @Getter
@RequiredArgsConstructor @RequiredArgsConstructor
public class OpdsUserDetails implements UserDetails { public class OpdsUserDetails implements UserDetails {
private final BookLoreUser user;
private final OpdsUserV2 opdsUserV2; private final OpdsUserV2 opdsUserV2;
@@ -67,7 +67,11 @@ class OpdsBookServiceTest {
private OpdsUserDetails legacyUserDetails() { private OpdsUserDetails legacyUserDetails() {
OpdsUserV2 v2 = OpdsUserV2.builder().userId(999L).username("legacy").build(); OpdsUserV2 v2 = OpdsUserV2.builder().userId(999L).username("legacy").build();
return new OpdsUserDetails(v2); BookLoreUser user = BookLoreUser.builder().username("legacy").id(999L).build();
return new OpdsUserDetails(
user,
v2
);
} }
private OpdsUserDetails v2UserDetails(Long userId, boolean isAdmin, Set<Long> libraryIds) { private OpdsUserDetails v2UserDetails(Long userId, boolean isAdmin, Set<Long> libraryIds) {
@@ -88,7 +92,10 @@ class OpdsBookServiceTest {
} }
when(user.getAssignedLibraries()).thenReturn(libraries); when(user.getAssignedLibraries()).thenReturn(libraries);
return new OpdsUserDetails(v2); return new OpdsUserDetails(
user,
v2
);
} }
@Test @Test