Skip to content

Commit 636709b

Browse files
committed
mcp-server: backfill tests for api key filter
Signed-off-by: Daniel Garnier-Moiroux <[email protected]>
1 parent 2c4f0ef commit 636709b

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

mcp-server-security/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@
107107
<artifactId>assertj-core</artifactId>
108108
<scope>test</scope>
109109
</dependency>
110+
<dependency>
111+
<groupId>org.mockito</groupId>
112+
<artifactId>mockito-core</artifactId>
113+
<scope>test</scope>
114+
</dependency>
110115
<dependency>
111116
<groupId>org.springframework</groupId>
112117
<artifactId>spring-test</artifactId>

mcp-server-security/src/main/java/org/springaicommunity/mcp/security/server/apikey/web/ApiKeyAuthenticationFilter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.http.HttpStatus;
2727
import org.springframework.security.authentication.AuthenticationManager;
2828
import org.springframework.security.core.Authentication;
29+
import org.springframework.security.web.authentication.AuthenticationConverter;
2930
import org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler;
3031
import org.springframework.security.web.authentication.AuthenticationFilter;
3132
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
@@ -39,7 +40,11 @@ public class ApiKeyAuthenticationFilter extends AuthenticationFilter {
3940
public static final String DEFAULT_API_KEY_HEADER = "X-API-Key";
4041

4142
public ApiKeyAuthenticationFilter(AuthenticationManager authenticationManager) {
42-
super(authenticationManager, new ApiKeyAuthenticationConverter(DEFAULT_API_KEY_HEADER));
43+
this(authenticationManager, new ApiKeyAuthenticationConverter(DEFAULT_API_KEY_HEADER));
44+
}
45+
46+
public ApiKeyAuthenticationFilter(AuthenticationManager authenticationManager, AuthenticationConverter authenticationConverter) {
47+
super(authenticationManager, authenticationConverter);
4348

4449
setSuccessHandler(new PassthroughSuccessHandler());
4550
setFailureHandler(
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2025-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springaicommunity.mcp.security.server.apikey.web;
18+
19+
import java.io.IOException;
20+
21+
import jakarta.servlet.FilterChain;
22+
import jakarta.servlet.ServletException;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.springaicommunity.mcp.security.server.apikey.authentication.ApiKeyAuthenticationToken;
26+
import org.springaicommunity.mcp.security.server.apikey.memory.ApiKeyEntityImpl;
27+
28+
import org.springframework.mock.web.MockHttpServletRequest;
29+
import org.springframework.mock.web.MockHttpServletResponse;
30+
import org.springframework.security.authentication.AuthenticationManager;
31+
import org.springframework.security.core.context.SecurityContextHolder;
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.mockito.ArgumentMatchers.any;
34+
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.verify;
36+
import static org.mockito.Mockito.verifyNoInteractions;
37+
import static org.mockito.Mockito.when;
38+
39+
class ApiKeyAuthenticationFilterTests {
40+
41+
private final FilterChain chain = mock(FilterChain.class);
42+
43+
private final AuthenticationManager authenticationManager = mock(AuthenticationManager.class);
44+
45+
private final MockHttpServletRequest request = new MockHttpServletRequest();
46+
47+
private final MockHttpServletResponse response = new MockHttpServletResponse();
48+
49+
private final ApiKeyEntityImpl apiKeyEntity = ApiKeyEntityImpl.builder()
50+
.id("api01")
51+
.secret("ignored")
52+
.name("test api key")
53+
.build();
54+
55+
private final ApiKeyAuthenticationToken authenticated = ApiKeyAuthenticationToken.authenticated(this.apiKeyEntity,
56+
null);
57+
58+
@BeforeEach
59+
void setUp() {
60+
when(authenticationManager.authenticate(any())).thenReturn(this.authenticated);
61+
SecurityContextHolder.clearContext();
62+
}
63+
64+
@Test
65+
void whenApiKeyPresentThenAuthenticates() throws ServletException, IOException {
66+
var filter = new ApiKeyAuthenticationFilter(this.authenticationManager);
67+
this.request.addHeader("X-API-key", "api01.secret");
68+
69+
filter.doFilter(this.request, this.response, this.chain);
70+
71+
verify(this.chain).doFilter(this.request, this.response);
72+
assertThat(SecurityContextHolder.getContext().getAuthentication()).isEqualTo(this.authenticated);
73+
}
74+
75+
@Test
76+
void whenApiKeyMalformedThenUnauthorized() throws ServletException, IOException {
77+
var filter = new ApiKeyAuthenticationFilter(this.authenticationManager);
78+
this.request.addHeader("X-API-key", "malformed");
79+
80+
filter.doFilter(this.request, this.response, this.chain);
81+
82+
verifyNoInteractions(this.chain);
83+
assertThat(this.response.getStatus()).isEqualTo(401);
84+
}
85+
86+
@Test
87+
void whenApiKeyMissingThenPassthrough() throws ServletException, IOException {
88+
var filter = new ApiKeyAuthenticationFilter(this.authenticationManager);
89+
90+
filter.doFilter(this.request, this.response, this.chain);
91+
92+
verify(this.chain).doFilter(this.request, this.response);
93+
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
94+
}
95+
96+
@Test
97+
void whenCustomConverterThenUses() throws ServletException, IOException {
98+
var converter = new ApiKeyAuthenticationConverter("x-test-header");
99+
var filter = new ApiKeyAuthenticationFilter(this.authenticationManager, converter);
100+
this.request.addHeader("x-test-header", "api01.secret");
101+
102+
filter.doFilter(this.request, this.response, this.chain);
103+
104+
verify(this.chain).doFilter(this.request, this.response);
105+
assertThat(SecurityContextHolder.getContext().getAuthentication()).isEqualTo(this.authenticated);
106+
}
107+
108+
}

0 commit comments

Comments
 (0)