|
| 1 | +package org.springaicommunity.mcp.security.server.oauth2.authentication; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.mockito.ArgumentMatchers.eq; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.mockito.ArgumentCaptor; |
| 12 | +import org.springaicommunity.mcp.security.server.oauth2.metadata.ResourceIdentifier; |
| 13 | +import org.springframework.http.HttpHeaders; |
| 14 | +import org.springframework.security.core.AuthenticationException; |
| 15 | + |
| 16 | +import jakarta.servlet.http.HttpServletRequest; |
| 17 | +import jakarta.servlet.http.HttpServletResponse; |
| 18 | + |
| 19 | +class BearerResourceMetadataTokenAuthenticationEntryPointTest { |
| 20 | + |
| 21 | + private HttpServletRequest request; |
| 22 | + |
| 23 | + private HttpServletResponse response; |
| 24 | + |
| 25 | + private ResourceIdentifier resourceIdentifier; |
| 26 | + |
| 27 | + private BearerResourceMetadataTokenAuthenticationEntryPoint entryPoint; |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + void setUp() { |
| 31 | + request = mock(HttpServletRequest.class); |
| 32 | + response = mock(HttpServletResponse.class); |
| 33 | + resourceIdentifier = mock(ResourceIdentifier.class); |
| 34 | + when(resourceIdentifier.getPath()).thenReturn("/mcp"); |
| 35 | + entryPoint = new BearerResourceMetadataTokenAuthenticationEntryPoint(resourceIdentifier); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void commence_ShouldAddCustomContextPath() throws Exception { |
| 40 | + when(request.getContextPath()).thenReturn("/foo"); |
| 41 | + when(request.getScheme()).thenReturn("https"); |
| 42 | + when(request.getServerName()).thenReturn("my.host.com"); |
| 43 | + when(request.getServerPort()).thenReturn(443); |
| 44 | + when(request.getRequestURI()).thenReturn("/foo/some/endpoint"); |
| 45 | + when(request.getQueryString()).thenReturn(null); |
| 46 | + |
| 47 | + when(response.getHeader(HttpHeaders.WWW_AUTHENTICATE)).thenReturn("Bearer"); |
| 48 | + |
| 49 | + AuthenticationException authException = mock(AuthenticationException.class); |
| 50 | + |
| 51 | + entryPoint.commence(request, response, authException); |
| 52 | + |
| 53 | + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 54 | + verify(response).setHeader(eq(HttpHeaders.WWW_AUTHENTICATE), captor.capture()); |
| 55 | + |
| 56 | + String headerValue = captor.getValue(); |
| 57 | + assertThat(headerValue) |
| 58 | + .contains("Bearer resource_metadata=https://my.host.com/foo/.well-known/oauth-protected-resource/mcp"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void commence_ShouldAddDefaultContextPath() throws Exception { |
| 63 | + when(request.getContextPath()).thenReturn("/"); |
| 64 | + when(request.getScheme()).thenReturn("https"); |
| 65 | + when(request.getServerName()).thenReturn("my.host.com"); |
| 66 | + when(request.getServerPort()).thenReturn(443); |
| 67 | + when(request.getRequestURI()).thenReturn("/some/endpoint"); |
| 68 | + when(request.getQueryString()).thenReturn(null); |
| 69 | + |
| 70 | + when(response.getHeader(HttpHeaders.WWW_AUTHENTICATE)).thenReturn("Bearer"); |
| 71 | + |
| 72 | + AuthenticationException authException = mock(AuthenticationException.class); |
| 73 | + |
| 74 | + entryPoint.commence(request, response, authException); |
| 75 | + |
| 76 | + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 77 | + verify(response).setHeader(eq(HttpHeaders.WWW_AUTHENTICATE), captor.capture()); |
| 78 | + |
| 79 | + String headerValue = captor.getValue(); |
| 80 | + assertThat(headerValue) |
| 81 | + .contains("Bearer resource_metadata=https://my.host.com/.well-known/oauth-protected-resource/mcp"); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments