fix(gateway): let the public QR registration reach its backend calls
A visitor has no user token, so the gateway refused every call the public page makes. Open only the exact method and path pairs it needs, and replace the full user lookup with a yes/no activeAdminOf endpoint so no user record is exposed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
1a7c542f5e
commit
eff4899c0d
8 files changed
+329
-10
No files matched your search
@@ -19,6 +19,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -28,6 +29,9 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Component
|
||||
public class JWTAuthFilter implements GlobalFilter, Ordered {
|
||||
@@ -36,6 +40,21 @@ public class JWTAuthFilter implements GlobalFilter, Ordered {
|
||||
|
||||
private static final String WWW_AUTH_HEADER = "WWW-Authenticate";
|
||||
|
||||
/** Public QR registration page: mobile-api reads these for a visitor with no user token. Anchored, exact method and path. */
|
||||
private static final List<Map.Entry<HttpMethod, Pattern>> PUBLIC_REGISTRATION = List.of(
|
||||
publicPath(HttpMethod.GET, "/counter/registrationLink/findByToken/[A-Za-z0-9_-]{22}"),
|
||||
publicPath(HttpMethod.GET, "/counter/event/findByUuid/\\d+"),
|
||||
publicPath(HttpMethod.GET, "/counter/interest/findByCompany/\\d+"),
|
||||
publicPath(HttpMethod.GET, "/counter/eventSalesperson/findByEventUuid/\\d+"),
|
||||
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"));
|
||||
|
||||
private static Map.Entry<HttpMethod, Pattern> publicPath(HttpMethod method, String regex) {
|
||||
return Map.entry(method, Pattern.compile(regex));
|
||||
}
|
||||
|
||||
@Value("${auth.api-key.third-party-api-key:''}")
|
||||
private String thirdPartyApiKey;
|
||||
|
||||
@@ -136,6 +155,7 @@ public class JWTAuthFilter implements GlobalFilter, Ordered {
|
||||
// Co-Lite admin sign-in: lock check and its recording run before the user has a token
|
||||
|| (url.matches("/loginSecurity/isLocked/[^/]+") && !url.contains(".."))
|
||||
|| url.equals("/loginSecurity/record")
|
||||
|| PUBLIC_REGISTRATION.stream().anyMatch(p -> p.getKey().equals(exchange.getRequest().getMethod()) && p.getValue().matcher(url).matches())
|
||||
|| url.contains("/geoLocationHierarchy/findByFirstLevelLocation")
|
||||
|| url.contains("/geoLocationHierarchy/findAllByParentId")
|
||||
// public content - no JWT required
|
||||
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
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 QR registration page reads a few things and posts one registration without a user token; nothing else opens. */
|
||||
class JWTAuthFilterPublicRegistrationPathTest {
|
||||
|
||||
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 publicRegistrationPathsNeedNoUserToken() {
|
||||
for (String[] c : new String[][] {
|
||||
{"GET", "/counter/registrationLink/findByToken/" + TOKEN},
|
||||
{"GET", "/counter/event/findByUuid/3312726793093644288"},
|
||||
{"GET", "/counter/interest/findByCompany/5"},
|
||||
{"GET", "/counter/eventSalesperson/findByEventUuid/3312726793093644288"},
|
||||
{"GET", "/company/findById/2"},
|
||||
{"GET", "/company/2/privacyNotice"},
|
||||
{"GET", "/user/21/activeAdminOf/5"},
|
||||
{"POST", "/counter/registration/register"} }) {
|
||||
assertThat(passes(call(HttpMethod.valueOf(c[0]), c[1]))).as(c[0] + " " + c[1]).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everythingElseStillNeedsAToken() {
|
||||
for (String[] c : new String[][] {
|
||||
{"POST", "/company/1/privacyNotice"},
|
||||
{"GET", "/user/findById/1"},
|
||||
{"POST", "/counter/registrationLink/ensure"},
|
||||
{"GET", "/counter/registrationLink/findByToken/" + TOKEN + "/x"},
|
||||
{"GET", "/counter/registrationLink/findByToken/short"},
|
||||
{"GET", "/counter/registrationLink/findByToken/" + TOKEN + "a"},
|
||||
{"GET", "/counter/registrationLink/findByToken/../x/" + TOKEN},
|
||||
{"POST", "/counter/event/findByUuid/1"},
|
||||
{"GET", "/counter/event/findByUuid/1/x"},
|
||||
{"GET", "/counter/event/findByUuid/abc"},
|
||||
{"GET", "/x/company/findById/2"},
|
||||
{"GET", "/company/findById/2/x"},
|
||||
{"GET", "/user/1/activeAdminOf/2/x"},
|
||||
{"POST", "/user/1/activeAdminOf/2"},
|
||||
{"GET", "/counter/registration/register"},
|
||||
{"POST", "/counter/registration/register/x"} }) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user