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
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
path: ~/.m2/repository
key: maven-${{ hashFiles('pom.xml') }}
restore-keys: maven-
- name: Check code formatting with Spotless
run: ./mvnw -B spotless:check
- name: Check POMs against the Maven Central rules
# Same PomChecker rules JReleaser applies during the release, run on every
# PR so a POM that Maven Central would reject fails here instead of after
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.springframework.boot.security.oauth2.server.resource.autoconfigure.OAuth2ResourceServerAutoConfiguration;

/**
* A zero-config consumer application with the <em>full</em> reactor on the classpath
* ({@code spring-services-all}). It declares its own entity/repository ({@link Order}) and
* <strong>no</strong> {@code @Import}, {@code @EntityScan}, or {@code @EnableJpaRepositories} — every
* library module self-activates through its own auto-configuration.
* A zero-config consumer application with the <em>full</em> reactor on the classpath ({@code
* spring-services-all}). It declares its own entity/repository ({@link Order}) and
* <strong>no</strong> {@code @Import}, {@code @EntityScan}, or {@code @EnableJpaRepositories} —
* every library module self-activates through its own auto-configuration.
*/
@SpringBootApplication(exclude = OAuth2ResourceServerAutoConfiguration.class)
public class AggregateApp {}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
/**
* Aggregate / zero-config starter integration test (spec 013 gate 1, relocated here per spec 014).
*
* <p>Boots {@link AggregateApp} — a zero-config consumer with the full reactor
* ({@code spring-services-all}) on the classpath — and asserts that:
* <p>Boots {@link AggregateApp} — a zero-config consumer with the full reactor ({@code
* spring-services-all}) on the classpath — and asserts that:
*
* <ul>
* <li>the application's own {@link Order}/{@link OrderRepository} resolve (default entity scan
* intact);
* <li>the core library persistence resolves ({@link UserRepository}, System User bootstrapped);
* <li>representative beans from multiple optional feature modules are present — {@link SlackService}
* (slack), {@link EmailService} (email), and {@link McpProperties} (mcp) — proving every module
* self-activated by classpath presence without any {@code @Import};
* <li>the storage module, which deliberately does <em>not</em> self-activate by classpath presence,
* registers no {@link ObjectStore}.
* <li>representative beans from multiple optional feature modules are present — {@link
* SlackService} (slack), {@link EmailService} (email), and {@link McpProperties} (mcp) —
* proving every module self-activated by classpath presence without any {@code @Import};
* <li>the storage module, which deliberately does <em>not</em> self-activate by classpath
* presence, registers no {@link ObjectStore}.
* </ul>
*/
@SpringBootTest(classes = AggregateApp.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import org.testcontainers.postgresql.PostgreSQLContainer;

/**
* Test infrastructure for the aggregate context: a PostgreSQL Testcontainer and a stub
* {@link JwtDecoder} required by the core JWT security chain (the aggregate test does not exercise
* real JWT validation).
* Test infrastructure for the aggregate context: a PostgreSQL Testcontainer and a stub {@link
* JwtDecoder} required by the core JWT security chain (the aggregate test does not exercise real
* JWT validation).
*/
@TestConfiguration(proxyBeanMethods = false)
public class AggregateTestConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
/**
* Auto-configuration for the optional ALTCHA bot protection.
*
* <p>Registered through this module's
* {@code META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports}, but gated on
* {@code openelements.altcha.enabled=true} rather than on classpath presence: the module cannot work
* without a signing secret, and self-activation would fail the start-up of every application that
* consumes {@code spring-services-all} without configuring one.
* <p>Registered through this module's {@code
* META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports}, but gated on
* {@code openelements.altcha.enabled=true} rather than on classpath presence: the module cannot
* work without a signing secret, and self-activation would fail the start-up of every application
* that consumes {@code spring-services-all} without configuring one.
*
* <p>Every bean is guarded by {@link ConditionalOnMissingBean}, so an application replaces a part — most
* likely the {@link ReplayStore} when it runs several instances — by declaring its own bean. The beans
* live on the auto-configuration itself, as in {@code StorageAutoConfiguration}, because
* <p>Every bean is guarded by {@link ConditionalOnMissingBean}, so an application replaces a part —
* most likely the {@link ReplayStore} when it runs several instances — by declaring its own bean.
* The beans live on the auto-configuration itself, as in {@code StorageAutoConfiguration}, because
* {@code @ConditionalOnMissingBean} is only reliable while auto-configurations are being processed.
*
* <p>Runs after the auto-configurations that enable Spring Security, so that the module's filter
Expand All @@ -52,89 +52,92 @@
@EnableConfigurationProperties(AltchaProperties.class)
public class AltchaAutoConfiguration {

/** Creates the auto-configuration. */
public AltchaAutoConfiguration() {
}
/** Creates the auto-configuration. */
public AltchaAutoConfiguration() {}

/**
* Remembers used challenges on the heap; correct for a single instance only.
*
* @return the store
*/
@Bean
@ConditionalOnMissingBean
public ReplayStore altchaReplayStore() {
return new InMemoryReplayStore();
}

/**
* Creates and verifies challenges.
*
* @param properties the configuration
* @param replayStore the store that remembers used challenges
* @return the verifier
*/
@Bean
@ConditionalOnMissingBean
public AltchaVerifier altchaVerifier(
final AltchaProperties properties, final ReplayStore replayStore) {
return new AltchaVerifier(properties, replayStore);
}

/**
* The API for the application's endpoints.
*
* @param verifier the verifier
* @return the service
*/
@Bean
@ConditionalOnMissingBean
public AltchaService altchaService(final AltchaVerifier verifier) {
return new AltchaService(verifier);
}

/** The web part: the challenge endpoint, its security filter chain and the error response. */
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class Web {

/**
* Remembers used challenges on the heap; correct for a single instance only.
*
* @return the store
*/
@Bean
@ConditionalOnMissingBean
public ReplayStore altchaReplayStore() {
return new InMemoryReplayStore();
AltchaChallengeController altchaChallengeController(final AltchaService service) {
return new AltchaChallengeController(service);
}

/**
* Creates and verifies challenges.
*
* @param properties the configuration
* @param replayStore the store that remembers used challenges
* @return the verifier
* The error response. The {@link ProblemFactory} is passed as a provider rather than injected:
* it does not exist while {@code openelements.problem-details.enabled=false}, and that is
* exactly the case in which this handler must answer with the module's previous body.
*/
@Bean
@ConditionalOnMissingBean
public AltchaVerifier altchaVerifier(final AltchaProperties properties, final ReplayStore replayStore) {
return new AltchaVerifier(properties, replayStore);
AltchaExceptionHandler altchaExceptionHandler(
final ObjectProvider<ProblemFactory> problemFactory) {
return new AltchaExceptionHandler(problemFactory);
}

/**
* The API for the application's endpoints.
*
* @param verifier the verifier
* @return the service
* Permits anonymous {@code GET} on the challenge path and nothing else on it. Ordered ahead of
* the default JWT chain, which has no matcher and would otherwise demand a token from the
* visitor of a public form. The chain matches only this one path, so it cannot open anything
* else.
*/
@Bean
@ConditionalOnMissingBean
public AltchaService altchaService(final AltchaVerifier verifier) {
return new AltchaService(verifier);
}

/** The web part: the challenge endpoint, its security filter chain and the error response. */
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
static class Web {

@Bean
@ConditionalOnMissingBean
AltchaChallengeController altchaChallengeController(final AltchaService service) {
return new AltchaChallengeController(service);
}

/**
* The error response. The {@link ProblemFactory} is passed as a provider rather than injected:
* it does not exist while {@code openelements.problem-details.enabled=false}, and that is
* exactly the case in which this handler must answer with the module's previous body.
*/
@Bean
@ConditionalOnMissingBean
AltchaExceptionHandler altchaExceptionHandler(
final ObjectProvider<ProblemFactory> problemFactory) {
return new AltchaExceptionHandler(problemFactory);
}

/**
* Permits anonymous {@code GET} on the challenge path and nothing else on it. Ordered ahead of
* the default JWT chain, which has no matcher and would otherwise demand a token from the
* visitor of a public form. The chain matches only this one path, so it cannot open anything
* else.
*/
@Bean
@Order(0)
@ConditionalOnClass(HttpSecurity.class)
@ConditionalOnBean(HttpSecurity.class)
@ConditionalOnMissingBean(name = "altchaSecurityFilterChain")
SecurityFilterChain altchaSecurityFilterChain(final HttpSecurity http, final AltchaProperties properties)
throws Exception {
http.securityMatcher(properties.challengePath())
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.GET, properties.challengePath()).permitAll()
.anyRequest().denyAll())
.sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.csrf(csrf -> csrf.disable());
return http.build();
}
@Order(0)
@ConditionalOnClass(HttpSecurity.class)
@ConditionalOnBean(HttpSecurity.class)
@ConditionalOnMissingBean(name = "altchaSecurityFilterChain")
SecurityFilterChain altchaSecurityFilterChain(
final HttpSecurity http, final AltchaProperties properties) throws Exception {
http.securityMatcher(properties.challengePath())
.authorizeHttpRequests(
auth ->
auth.requestMatchers(HttpMethod.GET, properties.challengePath())
.permitAll()
.anyRequest()
.denyAll())
.sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.csrf(csrf -> csrf.disable());
return http.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Null-marked package (JSpecify): every type is non-null unless annotated {@link org.jspecify.annotations.Nullable}.
* Null-marked package (JSpecify): every type is non-null unless annotated {@link
* org.jspecify.annotations.Nullable}.
*/
@NullMarked
package com.openelements.spring.base.altcha;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,29 @@
@RestController
public class AltchaChallengeController {

private final AltchaService service;
private final AltchaService service;

/**
* Creates the controller.
*
* @param service the service that creates the challenges
*/
public AltchaChallengeController(final AltchaService service) {
this.service = Objects.requireNonNull(service, "service must not be null");
}
/**
* Creates the controller.
*
* @param service the service that creates the challenges
*/
public AltchaChallengeController(final AltchaService service) {
this.service = Objects.requireNonNull(service, "service must not be null");
}

/**
* Creates a challenge. It is never cached, since each one can be used once.
*
* @return the challenge as JSON
*/
@GetMapping(value = "${openelements.altcha.challenge-path:" + AltchaProperties.DEFAULT_CHALLENGE_PATH + "}",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> challenge() {
return ResponseEntity.ok().header(HttpHeaders.CACHE_CONTROL, "no-store").body(service.createChallenge());
}
/**
* Creates a challenge. It is never cached, since each one can be used once.
*
* @return the challenge as JSON
*/
@GetMapping(
value =
"${openelements.altcha.challenge-path:" + AltchaProperties.DEFAULT_CHALLENGE_PATH + "}",
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> challenge() {
return ResponseEntity.ok()
.header(HttpHeaders.CACHE_CONTROL, "no-store")
.body(service.createChallenge());
}
}
Loading