ramu11 commented on code in PR #9509:
URL: https://github.com/apache/camel-quarkus/pull/9509#discussion_r4704316867


##########
extensions/mcp-server/runtime/src/main/java/org/apache/camel/quarkus/component/mcp/server/QuarkusMcpServerEngine.java:
##########
@@ -86,6 +129,122 @@ public void initialize(McpServerInfo info) {
         LOG.debug("quarkus-mcp-server engine initialized; serving 
configuration is owned by quarkus.mcp.server.*");
     }
 
+    /**
+     * Converts the current Quarkus request identity to the transport-neutral 
Camel MCP context.
+     *
+     * The CurrentIdentityAssociation is request scoped. It is deliberately 
read inside the
+     * tool callback so concurrent MCP requests cannot share or overwrite each 
other's identity.
+     */
+    private McpToolCallContext currentToolCallContext() {
+        // Applications without quarkus-security do not have this association.
+        if (currentIdentityAssociation == null) {
+            return McpToolCallContext.EMPTY;
+        }
+
+        SecurityIdentity identity = currentIdentityAssociation.getIdentity();
+        if (identity == null || identity.isAnonymous()) {
+            return McpToolCallContext.EMPTY;
+        }
+
+        Principal principal = identity.getPrincipal();
+        String principalName = principal != null ? principal.getName() : null;
+        if (principalName != null && principalName.isBlank()) {
+            principalName = null;
+        }
+
+        List<String> roles = identity.getRoles().stream()
+                .filter(role -> role != null && !role.isBlank())
+                .sorted()
+                .toList();
+
+        OAuthIdentity oauth = oauthIdentity(principal);
+
+        if (principal == null && principalName == null && roles.isEmpty() && 
oauth == null) {
+            return McpToolCallContext.EMPTY;
+        }
+
+        return new McpToolCallContext(
+                principal,
+                principalName,
+                roles.isEmpty() ? null : roles,
+                oauth != null ? oauth.subject() : null,
+                oauth != null ? oauth.scopes() : null,
+                oauth != null ? oauth.claims() : null,
+                null);
+    }
+
+    /**
+     * Extracts OAuth information from a MicroProfile JWT principal without 
making
+     * the MP JWT API a mandatory runtime dependency of the MCP extension.
+     */
+    private static OAuthIdentity oauthIdentity(Principal principal) {
+        if (principal == null) {
+            return null;
+        }
+
+        try {
+            ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
+            if (classLoader == null) {
+                classLoader = principal.getClass().getClassLoader();
+            }
+
+            Class<?> jwtType = Class.forName(
+                    "org.eclipse.microprofile.jwt.JsonWebToken", false, 
classLoader);
+
+            if (!jwtType.isInstance(principal)) {
+                return null;
+            }
+
+            String subject = (String) 
jwtType.getMethod("getSubject").invoke(principal);
+            Object claimNames = 
jwtType.getMethod("getClaimNames").invoke(principal);
+
+            Map<String, Object> claims = new LinkedHashMap<>();
+            if (claimNames instanceof Collection<?> names) {
+                var getClaim = jwtType.getMethod("getClaim", String.class);
+                for (Object name : names) {
+                    if (name instanceof String claimName) {
+                        claims.put(claimName, getClaim.invoke(principal, 
claimName));

Review Comment:
   The raw_token issue is already fixed in ed222bd3d8, with a corresponding 
OAuth test assertion.



-- 
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]

Reply via email to