From 7137dfc48fec34bf192885ba9be19617d24fdbdb Mon Sep 17 00:00:00 2001 From: ngqile Date: Tue, 1 Dec 2026 16:49:32 +0800 Subject: [PATCH] feat: gateway opens invite and check-in paths Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM --- .../solvway/gateway/filter/JWTAuthFilter.java | 8 +- .../filter/JWTAuthFilterInvitePathTest.java | 97 +++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterInvitePathTest.java diff --git a/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java b/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java index 0944b3d..a485a77 100644 --- a/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java +++ b/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java @@ -49,7 +49,13 @@ public class JWTAuthFilter implements GlobalFilter, Ordered { publicPath(HttpMethod.GET, "/company/findById/\\d+"), publicPath(HttpMethod.GET, "/company/\\d+/privacyNotice"), publicPath(HttpMethod.GET, "/user/\\d+/activeAdminOf/\\d+"), - publicPath(HttpMethod.POST, "/counter/registration/register")); + publicPath(HttpMethod.POST, "/counter/registration/register"), + // Public invite page and door check-in: exact method and path, token shape fixed at 22 url-safe chars + publicPath(HttpMethod.GET, "/counter/eventGuest/findByToken/[A-Za-z0-9_-]{22}"), + publicPath(HttpMethod.POST, "/counter/eventGuest/register"), + publicPath(HttpMethod.GET, "/counter/checkin/findEvent/[A-Za-z0-9_-]{22}"), + publicPath(HttpMethod.POST, "/counter/checkin/lookup"), + publicPath(HttpMethod.POST, "/counter/checkin/byGuest")); private static Map.Entry publicPath(HttpMethod method, String regex) { return Map.entry(method, Pattern.compile(regex)); diff --git a/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterInvitePathTest.java b/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterInvitePathTest.java new file mode 100644 index 0000000..901f860 --- /dev/null +++ b/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterInvitePathTest.java @@ -0,0 +1,97 @@ +package com.solvway.gateway.filter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.solvway.gateway.model.AuthAppKey; +import com.solvway.gateway.model.AuthAppKeyMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.mock.web.server.MockServerWebExchange; +import org.springframework.test.util.ReflectionTestUtils; +import reactor.core.publisher.Mono; + +import java.net.InetSocketAddress; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.assertj.core.api.Assertions.assertThat; + +/** The public invite and check-in pages open five method+path pairs without a user token; nothing else opens. */ +class JWTAuthFilterInvitePathTest { + + private static final String KEY = "gateway-test-key"; + private static final String TOKEN = "2lvFUDqc0zSu5X-6qvZE1g"; + + private JWTAuthFilter filter; + + @BeforeEach + void setUp() throws Exception { + AuthAppKey key = new ObjectMapper().readValue( + "{\"key\":\"" + KEY + "\",\"type\":\"BO\",\"ipList\":[\"0.0.0.0/0\"],\"allowList\":[\"/\"]}", + AuthAppKey.class); + AuthAppKeyMap map = new AuthAppKeyMap(); + map.add(KEY, key); + filter = new JWTAuthFilter(); + ReflectionTestUtils.setField(filter, "authAppKeyMap", map); + } + + private MockServerWebExchange call(HttpMethod method, String path) { + return MockServerWebExchange.from(MockServerHttpRequest.method(method, path) + .header("api-key", KEY) + .remoteAddress(new InetSocketAddress("127.0.0.1", 40000))); + } + + private boolean passes(MockServerWebExchange exchange) { + AtomicBoolean called = new AtomicBoolean(false); + GatewayFilterChain chain = ex -> { + called.set(true); + return Mono.empty(); + }; + filter.filter(exchange, chain).block(); + return called.get(); + } + + @Test + void invitePathsNeedNoUserToken() { + for (String[] c : new String[][] { + {"GET", "/counter/eventGuest/findByToken/" + TOKEN}, + {"POST", "/counter/eventGuest/register"}, + {"GET", "/counter/checkin/findEvent/" + TOKEN}, + {"POST", "/counter/checkin/lookup"}, + {"POST", "/counter/checkin/byGuest"} }) { + assertThat(passes(call(HttpMethod.valueOf(c[0]), c[1]))).as(c[0] + " " + c[1]).isTrue(); + } + } + + @Test + void everythingElseStillNeedsAToken() { + for (String[] c : new String[][] { + {"POST", "/counter/event/attendance"}, + {"POST", "/counter/eventGuest/list"}, + {"POST", "/counter/eventGuest/add"}, + {"POST", "/counter/eventGuest/remove"}, + {"POST", "/counter/eventGuest/markSent"}, + {"POST", "/counter/checkin/ensureToken"}, + {"POST", "/counter/eventGuest/findByToken/" + TOKEN}, + {"GET", "/counter/checkin/findEvent/" + TOKEN + "x"}, + {"POST", "/counter/checkin/findEvent/" + TOKEN}, + {"GET", "/counter/eventGuest/register"}, + {"GET", "/counter/checkin/lookup"}, + {"GET", "/counter/checkin/byGuest"}, + {"POST", "/counter/checkin/lookup/x"}, + {"GET", "/counter/eventGuest/findByToken/" + TOKEN + "/x"}, + {"GET", "/counter/checkin/findEvent/" + TOKEN + "/x"}, + {"GET", "/counter/eventGuest/findByToken/" + TOKEN.substring(1)}, + {"GET", "/counter/eventGuest/findByToken/" + TOKEN + "a"}, + {"GET", "/counter/eventGuest/findByToken/" + TOKEN.substring(1) + "."}, + {"GET", "/counter/eventGuest/findByToken/" + TOKEN.substring(1) + "%2F"}, + {"GET", "/counter/checkin/findEvent/" + TOKEN.substring(1)}, + {"GET", "/counter/checkin/findEvent/" + TOKEN.substring(1) + "."} }) { + MockServerWebExchange ex = call(HttpMethod.valueOf(c[0]), c[1]); + assertThat(passes(ex)).as(c[0] + " " + c[1]).isFalse(); + assertThat(ex.getResponse().getStatusCode()).as(c[0] + " " + c[1]).isEqualTo(HttpStatus.UNAUTHORIZED); + } + } +}