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;
import lombok.AllArgsConstructor;
import org.booklore.config.security.userdetails.OpdsUserDetails;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.repository.ShelfRepository;
import org.springframework.security.core.context.SecurityContextHolder;
@@ -14,8 +15,14 @@ public class SecurityUtil {
private BookLoreUser getCurrentUser() {
var authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof BookLoreUser user) {
return user;
if (authentication != null) {
if (authentication.getPrincipal() instanceof BookLoreUser user) {
return user;
}
if (authentication.getPrincipal() instanceof OpdsUserDetails opdsUser) {
return opdsUser.getUser();
}
}
return null;
}
@@ -3,6 +3,7 @@ package org.booklore.config.security.service;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.booklore.model.dto.AccessTokenDto;
import org.booklore.model.dto.OpdsUser;
import org.springframework.context.annotation.Lazy;
import org.booklore.config.AppProperties;
import org.booklore.config.security.JwtUtils;
@@ -85,13 +86,16 @@ public class AuthenticationService {
return null;
}
Object principal = authentication.getPrincipal();
if (principal instanceof OpdsUserDetails opdsUser) {
principal = opdsUser.getUser();
}
if (principal instanceof BookLoreUser user) {
if (user.getId() != null && user.getId() != -1L) {
defaultSettingInitializer.ensureDefaultSettings(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() {
@@ -3,7 +3,10 @@ package org.booklore.config.security.service;
import lombok.RequiredArgsConstructor;
import org.booklore.config.security.userdetails.OpdsUserDetails;
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.entity.BookLoreUserEntity;
import org.booklore.model.entity.OpdsUserV2Entity;
import org.booklore.repository.OpdsUserV2Repository;
import org.springframework.security.core.userdetails.UserDetailsService;
@@ -16,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
public class OpdsUserDetailsService implements UserDetailsService {
private final BookLoreUserTransformer bookLoreUserTransformer;
private final OpdsUserV2Repository opdsUserV2Repository;
private final OpdsUserV2Mapper opdsUserV2Mapper;
@@ -24,7 +28,11 @@ public class OpdsUserDetailsService implements UserDetailsService {
public OpdsUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
OpdsUserV2Entity userV2 = opdsUserV2Repository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("Invalid credentials"));
BookLoreUser appUser = bookLoreUserTransformer.toDTO(userV2.getUser());
OpdsUserV2 mappedCredential = opdsUserV2Mapper.toDto(userV2);
return new OpdsUserDetails(mappedCredential);
return new OpdsUserDetails(
appUser,
mappedCredential
);
}
}
@@ -1,5 +1,6 @@
package org.booklore.config.security.userdetails;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.OpdsUserV2;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@@ -12,6 +13,7 @@ import java.util.List;
@Getter
@RequiredArgsConstructor
public class OpdsUserDetails implements UserDetails {
private final BookLoreUser user;
private final OpdsUserV2 opdsUserV2;