Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 64 additions & 42 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.8</version>
<version>4.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.openpodcastapi</groupId>
Expand Down Expand Up @@ -36,7 +36,7 @@
<properties>
<java.version>25</java.version>
<org.mapstruct.version>1.6.3</org.mapstruct.version>
<org.mapstruct.extensions.spring.version>1.1.3</org.mapstruct.extensions.spring.version>
<org.mapstruct.extensions.spring.version>2.0.0</org.mapstruct.extensions.spring.version>
<com.fasterxml.uuid.version>5.1.1</com.fasterxml.uuid.version>
</properties>
<dependencies>
Expand All @@ -54,23 +54,30 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>io.github.wimdeblauwe</groupId>
<artifactId>htmx-spring-boot-thymeleaf</artifactId>
<version>4.0.1</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>3.4.0</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-flyway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand All @@ -84,14 +91,42 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-restdocs</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand All @@ -108,6 +143,11 @@
<artifactId>mapstruct-spring-annotations</artifactId>
<version>${org.mapstruct.extensions.spring.version}</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
Expand All @@ -118,36 +158,14 @@
<artifactId>dotenv-java</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
<version>11.17.1</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
Expand All @@ -169,6 +187,10 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security-oauth2-resource-server</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -202,7 +224,7 @@
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>2.2.6</version>
<version>3.2.0</version>
<executions>
<execution>
<id>generate-docs</id>
Expand All @@ -226,7 +248,7 @@
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-asciidoctor</artifactId>
<version>3.0.5</version>
<version>4.0.0</version>
</dependency>
</dependencies>
</plugin>
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/openpodcastapi/opa/OpenPodcastAPI.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package org.openpodcastapi.opa;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication()
@SpringBootApplication
@EnableScheduling
public class OpenPodcastAPI {

static void main(String[] args) {
SpringApplication.run(OpenPodcastAPI.class, args);
}

@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openpodcastapi.opa.advice;

import jakarta.persistence.EntityNotFoundException;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.openpodcastapi.opa.exceptions.ValidationErrorResponse;
Expand All @@ -21,25 +22,25 @@
public class GlobalExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<String> handleEntityNotFoundException(EntityNotFoundException error) {
public ResponseEntity<@NonNull String> handleEntityNotFoundException(EntityNotFoundException error) {
log.debug("{}", error.getMessage());
return ResponseEntity.notFound().build();
}

@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<String> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
public ResponseEntity<@NonNull String> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}

@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException e) {
public ResponseEntity<@NonNull String> handleIllegalArgumentException(IllegalArgumentException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ValidationErrorResponse> handleValidationException(MethodArgumentNotValidException ex) {
public ResponseEntity<@NonNull ValidationErrorResponse> handleValidationException(MethodArgumentNotValidException ex) {
List<ValidationErrorResponse.FieldError> errors = ex.getBindingResult().getFieldErrors().stream()
.map(fe -> new ValidationErrorResponse.FieldError(fe.getField(), fe.getDefaultMessage()))
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.openpodcastapi.opa.auth;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.server.resource.web.authentication.BearerTokenAuthenticationConverter;
import org.springframework.security.web.authentication.AuthenticationConverter;
import org.springframework.stereotype.Component;

@Component
public class ApiBearerTokenAuthenticationConverter implements AuthenticationConverter {

private final BearerTokenAuthenticationConverter delegate =
new BearerTokenAuthenticationConverter();

@Override
public Authentication convert(HttpServletRequest request) {

String path = request.getRequestURI();

// Don't authenticate the auth endpoints
if (path.startsWith("/api/auth/")) {
return null;
}

// If the request has no Bearer token, return null
final var header = request.getHeader("Authorization");
if (header == null || !header.startsWith("Bearer ")) {
return null;
}

// Task Spring Boot with handling the request
return delegate.convert(request);
}
}
34 changes: 34 additions & 0 deletions src/main/java/org/openpodcastapi/opa/auth/ApiSecurityHandlers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.openpodcastapi.opa.auth;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

@Component
public class ApiSecurityHandlers {
/// Returns an unauthorized response for unauthenticate API queries
@Bean
public AuthenticationEntryPoint apiAuthenticationEntryPoint() {
return (_, response, authException) -> {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentType("application/json");
response.getWriter().write("""
{"error": "unauthorized", "message": "%s"}
""".formatted(authException.getMessage()));
};
}

/// Returns a forbidden response for API queries
@Bean
public AccessDeniedHandler apiAccessDeniedHandler() {
return (_, response, exception) -> {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.setContentType("application/json");
response.getWriter().write("""
{"error": "forbidden", "message": "%s"}
""".formatted(exception.getMessage()));
};
}
}
10 changes: 0 additions & 10 deletions src/main/java/org/openpodcastapi/opa/auth/AuthDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,4 @@ public record RefreshTokenResponse(
@JsonProperty(value = "expiresIn", required = true) @NotNull String expiresIn
) {
}

/// Displays an auth error
///
/// @param error the error message
/// @param message an additional description of the error
public record ErrorMessageDTO(
@JsonProperty(value = "error", required = true) @NotNull String error,
@JsonProperty(value = "message", required = true) @NotNull String message
) {
}
}

This file was deleted.

This file was deleted.

Loading