funky-eyes commented on code in PR #7876: URL: https://github.com/apache/incubator-seata/pull/7876#discussion_r2639471398
########## console/src/main/java/org/apache/seata/mcp/core/props/MCPProperties.java: ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.mcp.core.props; + +import jakarta.annotation.PostConstruct; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +@Component +public class MCPProperties { + + private final Environment env; + + public static final String SSE_TYPE = "sse"; + + public static final String STREAMABLE_TYPE = "streamable"; + + private boolean enableAuth = true; + + private Long queryDuration = TimeUnit.DAYS.toMillis(1); + + private String mcpType = SSE_TYPE; + + private StreamableProperties streamableProperties; + + private SseServerProperties sseServerProperties; + + public MCPProperties(Environment env) { + this.env = env; + } + + public boolean isSseType() { + return mcpType.equals(SSE_TYPE); + } + + public List<String> getEndpoints() { + List<String> result = new ArrayList<>(); + if (isSseType()) { + result.add(sseServerProperties.sseEndpoint); + result.add(sseServerProperties.messageEndpoint); + } else { + result.add(streamableProperties.mcpEndpoint); + } + return result; + } + + public Long getQueryDuration() { + return queryDuration; + } + + public static class StreamableProperties { + private final String mcpEndpoint; + + public StreamableProperties(String mcpEndPoint) { + this.mcpEndpoint = mcpEndPoint; + } + } + + public static class SseServerProperties { + + private final String sseEndpoint; + + private final String messageEndpoint; + + public SseServerProperties(String sseEndpoint, String messageEndpoint) { + this.sseEndpoint = sseEndpoint; + this.messageEndpoint = messageEndpoint; + } + } + + @PostConstruct + public void init() { + mcpType = env.getProperty("spring.ai.mcp.server.protocol", "sse"); + if (mcpType.equals(STREAMABLE_TYPE)) { + String mcpEndPoint = env.getProperty("spring.ai.mcp.server.streamable-http.mcp-endpoint", "/mcp"); + streamableProperties = new StreamableProperties(mcpEndPoint); + } else { + mcpType = SSE_TYPE; + String sseEndpoint = env.getProperty("spring.ai.mcp.server.sse-endpoint", "/sse"); Review Comment: Doesn't this parameter have a corresponding properties bean? ########## console/src/main/java/org/apache/seata/console/filter/MCPAuthenticationFilter.java: ########## @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.seata.console.filter; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.seata.console.security.User; +import org.springframework.http.HttpStatus; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.OncePerRequestFilter; + +import java.io.IOException; + +public class MCPAuthenticationFilter extends OncePerRequestFilter { + + private final AuthenticationManager authenticationManager; + + /** + * Instantiates a new MCP authentication filter that authenticates requests + * using username and password provided in HTTP headers. + * + * @param authenticationManager the authentication manager used to validate credentials + */ + public MCPAuthenticationFilter(AuthenticationManager authenticationManager) { + this.authenticationManager = authenticationManager; + } + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) + throws IOException, ServletException { + + Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); + if (existingAuth != null && existingAuth.isAuthenticated()) { + chain.doFilter(request, response); + return; + } + + User user = resolveHeaders(request); + + UsernamePasswordAuthenticationToken authenticationToken = + new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()); + + try { + Authentication authentication = authenticationManager.authenticate(authenticationToken); + SecurityContextHolder.getContext().setAuthentication(authentication); + } catch (AuthenticationException e) { + response.sendError(HttpStatus.UNAUTHORIZED.value(), "Invalid credentials"); + return; + } + + chain.doFilter(request, response); + } + + /** + * Get userDetails from header + */ + private User resolveHeaders(HttpServletRequest request) { + String username = request.getHeader("X-Mcp-Username"); Review Comment: Is this a standard MCP HTTP header? If not, I wouldn't recommend having two separate authentication logics—we should just reuse the existing authentication logic for verification instead. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
