oscerd commented on code in PR #27767:
URL: https://github.com/apache/camel/pull/27767#discussion_r4268219499


##########
components/camel-ai/camel-mcp-server/src/main/java/org/apache/camel/component/mcp/server/vertx/VertxMcpPrincipal.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.camel.component.mcp.server.vertx;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import io.vertx.core.json.JsonArray;
+import io.vertx.core.json.JsonObject;
+import io.vertx.ext.auth.User;
+
+/**
+ * Derives a runtime-neutral view — a caller name and role names — from a 
Vert.x {@link User}, so the MCP bridge can
+ * stamp a stable caller identity ({@code CamelMcpSecurityPrincipalName} / 
{@code CamelMcpSecurityPrincipalRoles}) that
+ * a tool route or its {@link org.apache.camel.spi.AuthorizationPolicy} can 
authorize on without depending on Vert.x.
+ */
+final class VertxMcpPrincipal {
+
+    private VertxMcpPrincipal() {
+    }
+
+    /**
+     * The caller name: the Vert.x {@link User#subject() subject} when 
non-null and non-blank, else the {@code username}
+     * or {@code sub} claim of the principal when non-null and non-blank, else 
{@code null}.

Review Comment:
   Already in place at `56c20f06`: the `name()` Javadoc reads "the Vert.x 
{@link User#subject() subject} **when non-null and non-blank**, else the 
`username` or `sub` claim of the principal **when non-null and non-blank**, 
else `null`" (VertxMcpPrincipal.java:37–38). It conveys that a blank subject is 
skipped — and also qualifies the `username`/`sub` fallback the same way, which 
is slightly more precise than the suggestion. So this is addressed.
   
   _Claude Code on behalf of oscerd_
   



##########
components/camel-ai/camel-mcp-server/src/test/java/org/apache/camel/component/mcp/server/vertx/VertxMcpPrincipalTest.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.camel.component.mcp.server.vertx;
+
+import io.vertx.core.json.JsonArray;
+import io.vertx.core.json.JsonObject;
+import io.vertx.ext.auth.User;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Unit tests for {@link VertxMcpPrincipal}: deriving a runtime-neutral name 
and roles from a Vert.x {@link User}.
+ */
+class VertxMcpPrincipalTest {
+
+    @Test
+    void nameFromUsernameClaim() {
+        User user = User.create(new JsonObject().put("username", "camel"));
+        assertThat(VertxMcpPrincipal.name(user)).isEqualTo("camel");
+    }
+
+    @Test
+    void nameFromSubClaimWhenNoUsername() {
+        User user = User.create(new JsonObject().put("sub", "user-123"));
+        assertThat(VertxMcpPrincipal.name(user)).isEqualTo("user-123");
+    }
+
+    @Test
+    void nameIsNullForNullUserOrNoClaims() {
+        assertThat(VertxMcpPrincipal.name(null)).isNull();
+        assertThat(VertxMcpPrincipal.name(User.create(new 
JsonObject()))).isNull();
+    }
+
+    @Test
+    void rolesFromTopLevelRolesClaim() {
+        User user = User.create(new JsonObject().put("roles", new 
JsonArray().add("admin").add("user")));
+        assertThat(VertxMcpPrincipal.roles(user)).containsExactly("admin", 
"user");
+    }
+
+    @Test
+    void rolesFromKeycloakRealmAccess() {
+        User user = User.create(new JsonObject()
+                .put("realm_access", new JsonObject().put("roles", new 
JsonArray().add("kc-role"))));
+        assertThat(VertxMcpPrincipal.roles(user)).containsExactly("kc-role");
+    }
+
+    @Test
+    void rolesFromGroupsClaim() {
+        User user = User.create(new JsonObject().put("groups", new 
JsonArray().add("g1").add("g2")));
+        assertThat(VertxMcpPrincipal.roles(user)).containsExactly("g1", "g2");
+    }
+
+    @Test
+    void topLevelRolesTakePrecedenceOverGroups() {
+        User user = User.create(new JsonObject()
+                .put("roles", new JsonArray().add("a"))
+                .put("groups", new JsonArray().add("b")));
+        assertThat(VertxMcpPrincipal.roles(user)).containsExactly("a");
+    }
+
+    @Test
+    void rolesSkipBlankAndNonStringEntries() {

Review Comment:
   Both the guard and the test are already in `56c20f06`:
   
   - `roles()` no longer calls `getJsonArray`/`getJsonObject` — it reads via 
`principal.getValue(k)` and checks `instanceof JsonArray`/`JsonObject` before 
use, so a scalar `"roles":"admin"` (or scalar `groups` / `realm_access.roles`, 
or a non-object `realm_access`) cannot throw `ClassCastException`.
   - `VertxMcpPrincipalTest.rolesIgnoreNonArrayClaimShapesInsteadOfThrowing` 
covers exactly those scalar shapes, and 
`rolesFallThroughFromNonArrayRolesToGroups` covers a scalar `roles` alongside a 
valid `groups` array.
   
   On null-vs-wrap: I deliberately **ignore** a scalar (it falls through 
`roles` → `realm_access.roles` → `groups` → `null`) rather than wrap it into a 
one-element list. This is an authorization input, so I prefer to fail safe: a 
non-standard scalar claim yields no roles (treated as unauthorized, per the 
docs) instead of synthesising a role the issuer may not have intended. An 
issuer that wants role-based authz should emit a JSON array.
   
   _Claude Code on behalf of oscerd_
   



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