refactor(komga): stop using opds service for book access checks (#1479)
This commit is contained in:
@@ -8,14 +8,17 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.booklore.config.JacksonConfig;
|
import org.booklore.config.JacksonConfig;
|
||||||
import org.booklore.config.security.service.AuthenticationService;
|
import org.booklore.config.security.service.AuthenticationService;
|
||||||
import org.booklore.config.security.userdetails.OpdsUserDetails;
|
import org.booklore.config.security.userdetails.OpdsUserDetails;
|
||||||
|
import org.booklore.exception.ApiError;
|
||||||
import org.booklore.mapper.komga.KomgaMapper;
|
import org.booklore.mapper.komga.KomgaMapper;
|
||||||
|
import org.booklore.model.dto.BookLoreUser;
|
||||||
import org.booklore.model.dto.komga.KomgaBookDto;
|
import org.booklore.model.dto.komga.KomgaBookDto;
|
||||||
import org.booklore.model.dto.komga.KomgaLibraryDto;
|
import org.booklore.model.dto.komga.KomgaLibraryDto;
|
||||||
import org.booklore.model.dto.komga.KomgaPageableDto;
|
import org.booklore.model.dto.komga.KomgaPageableDto;
|
||||||
import org.booklore.model.dto.komga.KomgaSeriesDto;
|
import org.booklore.model.dto.komga.KomgaSeriesDto;
|
||||||
|
import org.booklore.model.entity.BookEntity;
|
||||||
|
import org.booklore.model.entity.BookLoreUserEntity;
|
||||||
import org.booklore.service.book.BookService;
|
import org.booklore.service.book.BookService;
|
||||||
import org.booklore.service.komga.KomgaService;
|
import org.booklore.service.komga.KomgaService;
|
||||||
import org.booklore.service.opds.OpdsBookService;
|
|
||||||
import org.booklore.service.opds.OpdsUserV2Service;
|
import org.booklore.service.opds.OpdsUserV2Service;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
@@ -39,7 +42,6 @@ public class KomgaController {
|
|||||||
|
|
||||||
private final KomgaService komgaService;
|
private final KomgaService komgaService;
|
||||||
private final BookService bookService;
|
private final BookService bookService;
|
||||||
private final OpdsBookService opdsBookService;
|
|
||||||
private final AuthenticationService authenticationService;
|
private final AuthenticationService authenticationService;
|
||||||
private final OpdsUserV2Service opdsUserV2Service;
|
private final OpdsUserV2Service opdsUserV2Service;
|
||||||
private final KomgaMapper komgaMapper;
|
private final KomgaMapper komgaMapper;
|
||||||
@@ -58,6 +60,18 @@ public class KomgaController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateBookContentAccess(Long bookId) {
|
||||||
|
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
throw ApiError.FORBIDDEN.createException("Authentication required");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!komgaService.validateBookContentAccess(user, bookId)) {
|
||||||
|
throw ApiError.BOOK_NOT_FOUND.createException(bookId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ==================== Libraries ====================
|
// ==================== Libraries ====================
|
||||||
|
|
||||||
@Operation(summary = "List all libraries")
|
@Operation(summary = "List all libraries")
|
||||||
@@ -152,7 +166,8 @@ public class KomgaController {
|
|||||||
@Parameter(description = "Book ID") @PathVariable Long bookId,
|
@Parameter(description = "Book ID") @PathVariable Long bookId,
|
||||||
@Parameter(description = "Page number") @PathVariable Integer pageNumber,
|
@Parameter(description = "Page number") @PathVariable Integer pageNumber,
|
||||||
@Parameter(description = "Convert image format (e.g., 'png')") @RequestParam(required = false) String convert) {
|
@Parameter(description = "Convert image format (e.g., 'png')") @RequestParam(required = false) String convert) {
|
||||||
opdsBookService.validateBookContentAccess(bookId, getOpdsUserId());
|
validateBookContentAccess(bookId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
boolean convertToPng = "png".equalsIgnoreCase(convert);
|
boolean convertToPng = "png".equalsIgnoreCase(convert);
|
||||||
Resource pageImage = komgaService.getBookPageImage(bookId, pageNumber, convertToPng);
|
Resource pageImage = komgaService.getBookPageImage(bookId, pageNumber, convertToPng);
|
||||||
@@ -172,7 +187,8 @@ public class KomgaController {
|
|||||||
@GetMapping("/v1/books/{bookId}/file")
|
@GetMapping("/v1/books/{bookId}/file")
|
||||||
public ResponseEntity<Resource> downloadBook(
|
public ResponseEntity<Resource> downloadBook(
|
||||||
@Parameter(description = "Book ID") @PathVariable Long bookId) {
|
@Parameter(description = "Book ID") @PathVariable Long bookId) {
|
||||||
opdsBookService.validateBookContentAccess(bookId, getOpdsUserId());
|
validateBookContentAccess(bookId);
|
||||||
|
|
||||||
return bookService.downloadBook(bookId);
|
return bookService.downloadBook(bookId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,7 +196,8 @@ public class KomgaController {
|
|||||||
@GetMapping("/v1/books/{bookId}/thumbnail")
|
@GetMapping("/v1/books/{bookId}/thumbnail")
|
||||||
public ResponseEntity<Resource> getBookThumbnail(
|
public ResponseEntity<Resource> getBookThumbnail(
|
||||||
@Parameter(description = "Book ID") @PathVariable Long bookId) {
|
@Parameter(description = "Book ID") @PathVariable Long bookId) {
|
||||||
opdsBookService.validateBookContentAccess(bookId, getOpdsUserId());
|
validateBookContentAccess(bookId);
|
||||||
|
|
||||||
Resource coverImage = bookService.getBookThumbnail(bookId);
|
Resource coverImage = bookService.getBookThumbnail(bookId);
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header("Content-Type", "image/jpeg")
|
.header("Content-Type", "image/jpeg")
|
||||||
@@ -216,11 +233,4 @@ public class KomgaController {
|
|||||||
@Parameter(description = "Return all collections without paging") @RequestParam(defaultValue = "false") boolean unpaged) {
|
@Parameter(description = "Return all collections without paging") @RequestParam(defaultValue = "false") boolean unpaged) {
|
||||||
return writeJson(komgaService.getCollections(page, size, unpaged));
|
return writeJson(komgaService.getCollections(page, size, unpaged));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Long getOpdsUserId() {
|
|
||||||
OpdsUserDetails details = authenticationService.getOpdsUser();
|
|
||||||
return details != null && details.getOpdsUserV2() != null
|
|
||||||
? details.getOpdsUserV2().getUserId()
|
|
||||||
: null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,9 @@ package org.booklore.service.komga;
|
|||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.booklore.exception.ApiError;
|
||||||
import org.booklore.mapper.komga.KomgaMapper;
|
import org.booklore.mapper.komga.KomgaMapper;
|
||||||
|
import org.booklore.model.dto.BookLoreUser;
|
||||||
import org.booklore.model.dto.MagicShelf;
|
import org.booklore.model.dto.MagicShelf;
|
||||||
import org.booklore.model.dto.komga.*;
|
import org.booklore.model.dto.komga.*;
|
||||||
import org.booklore.model.entity.BookEntity;
|
import org.booklore.model.entity.BookEntity;
|
||||||
@@ -15,6 +17,7 @@ import org.booklore.service.MagicShelfService;
|
|||||||
import org.booklore.service.appsettings.AppSettingService;
|
import org.booklore.service.appsettings.AppSettingService;
|
||||||
import org.booklore.service.reader.CbxReaderService;
|
import org.booklore.service.reader.CbxReaderService;
|
||||||
import org.booklore.service.reader.PdfReaderService;
|
import org.booklore.service.reader.PdfReaderService;
|
||||||
|
import org.booklore.service.restriction.ContentRestrictionService;
|
||||||
import org.springframework.core.io.ByteArrayResource;
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
@@ -45,6 +48,40 @@ public class KomgaService {
|
|||||||
private final CbxReaderService cbxReaderService;
|
private final CbxReaderService cbxReaderService;
|
||||||
private final PdfReaderService pdfReaderService;
|
private final PdfReaderService pdfReaderService;
|
||||||
private final AppSettingService appSettingService;
|
private final AppSettingService appSettingService;
|
||||||
|
private final ContentRestrictionService contentRestrictionService;
|
||||||
|
|
||||||
|
|
||||||
|
public boolean validateBookContentAccess(BookLoreUser user, Long bookId) {
|
||||||
|
if (user == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.getPermissions() != null && user.getPermissions().isAdmin()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
BookEntity book = bookRepository.findById(bookId)
|
||||||
|
.orElse(null);
|
||||||
|
|
||||||
|
if (book == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean hasLibraryAccess = user.getAssignedLibraries() != null && user.getAssignedLibraries().stream()
|
||||||
|
.anyMatch(library -> library.getId().equals(book.getLibrary().getId()));
|
||||||
|
|
||||||
|
if (!hasLibraryAccess) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<BookEntity> filtered = contentRestrictionService.applyRestrictions(List.of(book), user.getId());
|
||||||
|
if (filtered.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<KomgaLibraryDto> getAllLibraries() {
|
public List<KomgaLibraryDto> getAllLibraries() {
|
||||||
return libraryRepository.findAll().stream()
|
return libraryRepository.findAll().stream()
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package org.booklore.service.komga;
|
package org.booklore.service.komga;
|
||||||
|
|
||||||
import org.booklore.mapper.komga.KomgaMapper;
|
import org.booklore.mapper.komga.KomgaMapper;
|
||||||
|
import org.booklore.model.dto.BookLoreUser;
|
||||||
|
import org.booklore.model.dto.Library;
|
||||||
import org.booklore.model.dto.komga.KomgaBookDto;
|
import org.booklore.model.dto.komga.KomgaBookDto;
|
||||||
import org.booklore.model.dto.komga.KomgaPageDto;
|
import org.booklore.model.dto.komga.KomgaPageDto;
|
||||||
import org.booklore.model.dto.komga.KomgaPageableDto;
|
import org.booklore.model.dto.komga.KomgaPageableDto;
|
||||||
@@ -16,6 +18,7 @@ import org.booklore.repository.LibraryRepository;
|
|||||||
import org.booklore.service.MagicShelfService;
|
import org.booklore.service.MagicShelfService;
|
||||||
import org.booklore.service.appsettings.AppSettingService;
|
import org.booklore.service.appsettings.AppSettingService;
|
||||||
import org.booklore.service.reader.CbxReaderService;
|
import org.booklore.service.reader.CbxReaderService;
|
||||||
|
import org.booklore.service.restriction.ContentRestrictionService;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
@@ -53,6 +56,9 @@ class KomgaServiceTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private AppSettingService appSettingService;
|
private AppSettingService appSettingService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ContentRestrictionService contentRestrictionService;
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private KomgaService komgaService;
|
private KomgaService komgaService;
|
||||||
|
|
||||||
@@ -244,4 +250,125 @@ class KomgaServiceTest {
|
|||||||
verify(bookRepository, never()).findAllWithMetadataByLibraryId(anyLong());
|
verify(bookRepository, never()).findAllWithMetadataByLibraryId(anyLong());
|
||||||
verify(bookRepository, never()).findAllWithMetadata();
|
verify(bookRepository, never()).findAllWithMetadata();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateBookContentAccess_acceptsBookCorrectWithAccess() {
|
||||||
|
BookEntity bookEntity = getBookEntity();
|
||||||
|
BookLoreUser user = getBookloreUser(false, List.of(library));
|
||||||
|
|
||||||
|
when(bookRepository.findById(1L)).thenReturn(Optional.of(bookEntity));
|
||||||
|
when(
|
||||||
|
contentRestrictionService.applyRestrictions(List.of(bookEntity), user.getId())
|
||||||
|
).thenReturn(List.of(bookEntity));
|
||||||
|
|
||||||
|
boolean actual = komgaService.validateBookContentAccess(user, 1L);
|
||||||
|
|
||||||
|
assertThat(actual).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateBookContentAccess_ignoresNoPermissionUser() {
|
||||||
|
BookEntity bookEntity = getBookEntity();
|
||||||
|
BookLoreUser user = BookLoreUser.builder()
|
||||||
|
.id(1L)
|
||||||
|
.assignedLibraries(List.of(Library.builder().id(1L).build()))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(bookRepository.findById(1L)).thenReturn(Optional.of(bookEntity));
|
||||||
|
when(
|
||||||
|
contentRestrictionService.applyRestrictions(List.of(bookEntity), user.getId())
|
||||||
|
).thenReturn(List.of(bookEntity));
|
||||||
|
|
||||||
|
boolean actual = komgaService.validateBookContentAccess(user, 1L);
|
||||||
|
|
||||||
|
assertThat(actual).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateBookContentAccess_rejectsNullLibraryUser() {
|
||||||
|
BookEntity bookEntity = getBookEntity();
|
||||||
|
BookLoreUser user = BookLoreUser.builder()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(bookRepository.findById(1L)).thenReturn(Optional.of(bookEntity));
|
||||||
|
|
||||||
|
boolean actual = komgaService.validateBookContentAccess(user, 1L);
|
||||||
|
|
||||||
|
assertThat(actual).isFalse();
|
||||||
|
|
||||||
|
verifyNoInteractions(contentRestrictionService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateBookContentAccess_rejectsOnLibraryAccess() {
|
||||||
|
LibraryEntity otherLibrary = LibraryEntity.builder().id(2L).build();
|
||||||
|
|
||||||
|
BookEntity bookEntity = getBookEntity();
|
||||||
|
BookLoreUser user = getBookloreUser(false, List.of(otherLibrary));
|
||||||
|
|
||||||
|
when(bookRepository.findById(1L)).thenReturn(Optional.of(bookEntity));
|
||||||
|
|
||||||
|
boolean actual = komgaService.validateBookContentAccess(user, 1L);
|
||||||
|
|
||||||
|
assertThat(actual).isFalse();
|
||||||
|
|
||||||
|
verifyNoInteractions(contentRestrictionService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateBookContentAccess_rejectsOnContentRestrictions() {
|
||||||
|
BookEntity bookEntity = getBookEntity();
|
||||||
|
BookLoreUser user = getBookloreUser(false, List.of(library));
|
||||||
|
|
||||||
|
when(bookRepository.findById(1L)).thenReturn(Optional.of(bookEntity));
|
||||||
|
when(
|
||||||
|
contentRestrictionService.applyRestrictions(List.of(bookEntity), user.getId())
|
||||||
|
).thenReturn(List.of());
|
||||||
|
|
||||||
|
boolean actual = komgaService.validateBookContentAccess(user, 1L);
|
||||||
|
|
||||||
|
assertThat(actual).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateBookContentAccess_rejectsOnMissingBook() {
|
||||||
|
BookLoreUser user = getBookloreUser(false, List.of());
|
||||||
|
|
||||||
|
boolean actual = komgaService.validateBookContentAccess(user, 1L);
|
||||||
|
|
||||||
|
assertThat(actual).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void validateBookContentAccess_adminAlwaysHasAccess() {
|
||||||
|
BookLoreUser user = getBookloreUser(true, List.of());
|
||||||
|
|
||||||
|
boolean actual = komgaService.validateBookContentAccess(user, 1L);
|
||||||
|
|
||||||
|
assertThat(actual).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
BookEntity getBookEntity() {
|
||||||
|
return BookEntity.builder()
|
||||||
|
.id(1L)
|
||||||
|
.library(library)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
BookLoreUser getBookloreUser(boolean isAdmin, List<LibraryEntity> libraries) {
|
||||||
|
BookLoreUser.UserPermissions perms = new BookLoreUser.UserPermissions();
|
||||||
|
|
||||||
|
perms.setAdmin(isAdmin);
|
||||||
|
|
||||||
|
return BookLoreUser.builder()
|
||||||
|
.id(1L)
|
||||||
|
.permissions(perms)
|
||||||
|
.assignedLibraries(
|
||||||
|
libraries
|
||||||
|
.stream()
|
||||||
|
.map(l -> Library.builder().id(l.getId()).build())
|
||||||
|
.toList()
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user