|
21 | 21 | import org.junit.jupiter.api.BeforeEach; |
22 | 22 | import org.junit.jupiter.api.Test; |
23 | 23 | import software.amazon.smithy.java.auth.api.SignResult; |
| 24 | +import software.amazon.smithy.java.auth.api.Signer; |
| 25 | +import software.amazon.smithy.java.auth.api.identity.Identity; |
| 26 | +import software.amazon.smithy.java.auth.api.identity.IdentityResolver; |
| 27 | +import software.amazon.smithy.java.auth.api.identity.IdentityResult; |
| 28 | +import software.amazon.smithy.java.client.core.auth.scheme.AuthScheme; |
| 29 | +import software.amazon.smithy.java.context.Context; |
24 | 30 | import software.amazon.smithy.java.core.serde.document.Document; |
| 31 | +import software.amazon.smithy.java.http.api.HttpRequest; |
25 | 32 | import software.amazon.smithy.java.json.JsonCodec; |
26 | 33 | import software.amazon.smithy.java.mcp.model.JsonRpcRequest; |
27 | 34 | import software.amazon.smithy.java.mcp.model.JsonRpcResponse; |
| 35 | +import software.amazon.smithy.model.shapes.ShapeId; |
28 | 36 | import software.amazon.smithy.model.shapes.ShapeType; |
29 | 37 |
|
30 | 38 | class HttpMcpProxyTest { |
@@ -66,6 +74,110 @@ void testBuilderValidation() { |
66 | 74 | assertThrows(IllegalArgumentException.class, () -> HttpMcpProxy.builder().endpoint("").build()); |
67 | 75 | } |
68 | 76 |
|
| 77 | + @Test |
| 78 | + void testBuilderRejectsSignerAndAuthSchemeTogether() { |
| 79 | + assertThrows(IllegalArgumentException.class, |
| 80 | + () -> HttpMcpProxy.builder() |
| 81 | + .endpoint(serverUrl) |
| 82 | + .signer((request, identity, context) -> new SignResult<>(request)) |
| 83 | + .authScheme(new TestAuthScheme()) |
| 84 | + .identityResolver(TestIdentityResolver.INSTANCE) |
| 85 | + .build()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testBuilderRejectsAuthSchemeWithoutIdentityResolver() { |
| 90 | + assertThrows(IllegalArgumentException.class, |
| 91 | + () -> HttpMcpProxy.builder() |
| 92 | + .endpoint(serverUrl) |
| 93 | + .authScheme(new TestAuthScheme()) |
| 94 | + .build()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testBuilderRejectsIdentityResolverWithoutAuthScheme() { |
| 99 | + assertThrows(IllegalArgumentException.class, |
| 100 | + () -> HttpMcpProxy.builder() |
| 101 | + .endpoint(serverUrl) |
| 102 | + .identityResolver(TestIdentityResolver.INSTANCE) |
| 103 | + .build()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testAuthSchemeSignsRequest() throws IOException { |
| 108 | + String[] capturedHeader = {null}; |
| 109 | + |
| 110 | + mockServer.removeContext("/mcp"); |
| 111 | + mockServer.createContext("/mcp", exchange -> { |
| 112 | + capturedHeader[0] = exchange.getRequestHeaders().getFirst("X-Test-Signed"); |
| 113 | + String response = "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"signed\"}"; |
| 114 | + exchange.getResponseHeaders().set("Content-Type", "application/json"); |
| 115 | + exchange.sendResponseHeaders(200, response.getBytes(StandardCharsets.UTF_8).length); |
| 116 | + try (OutputStream os = exchange.getResponseBody()) { |
| 117 | + os.write(response.getBytes(StandardCharsets.UTF_8)); |
| 118 | + } |
| 119 | + exchange.close(); |
| 120 | + }); |
| 121 | + |
| 122 | + HttpMcpProxy authProxy = HttpMcpProxy.builder() |
| 123 | + .endpoint(serverUrl) |
| 124 | + .authScheme(new TestAuthScheme()) |
| 125 | + .identityResolver(TestIdentityResolver.INSTANCE) |
| 126 | + .build(); |
| 127 | + |
| 128 | + JsonRpcRequest request = JsonRpcRequest.builder() |
| 129 | + .method("test/method") |
| 130 | + .id(Document.of(1)) |
| 131 | + .jsonrpc("2.0") |
| 132 | + .build(); |
| 133 | + |
| 134 | + JsonRpcResponse response = authProxy.rpc(request).join(); |
| 135 | + |
| 136 | + assertNotNull(response); |
| 137 | + assertEquals("signed", response.getResult().asString()); |
| 138 | + assertEquals("test-token", capturedHeader[0]); |
| 139 | + authProxy.shutdown().join(); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + void testAuthSchemeReceivesSignerContext() throws IOException { |
| 144 | + String[] capturedRegion = {null}; |
| 145 | + |
| 146 | + mockServer.removeContext("/mcp"); |
| 147 | + mockServer.createContext("/mcp", exchange -> { |
| 148 | + capturedRegion[0] = exchange.getRequestHeaders().getFirst("X-Region"); |
| 149 | + String response = "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"ok\"}"; |
| 150 | + exchange.getResponseHeaders().set("Content-Type", "application/json"); |
| 151 | + exchange.sendResponseHeaders(200, response.getBytes(StandardCharsets.UTF_8).length); |
| 152 | + try (OutputStream os = exchange.getResponseBody()) { |
| 153 | + os.write(response.getBytes(StandardCharsets.UTF_8)); |
| 154 | + } |
| 155 | + exchange.close(); |
| 156 | + }); |
| 157 | + |
| 158 | + Context signerCtx = Context.create(); |
| 159 | + signerCtx.put(TestAuthScheme.REGION_KEY, "us-west-2"); |
| 160 | + |
| 161 | + HttpMcpProxy authProxy = HttpMcpProxy.builder() |
| 162 | + .endpoint(serverUrl) |
| 163 | + .authScheme(new TestAuthScheme()) |
| 164 | + .identityResolver(TestIdentityResolver.INSTANCE) |
| 165 | + .signerContext(signerCtx) |
| 166 | + .build(); |
| 167 | + |
| 168 | + JsonRpcRequest request = JsonRpcRequest.builder() |
| 169 | + .method("test/method") |
| 170 | + .id(Document.of(1)) |
| 171 | + .jsonrpc("2.0") |
| 172 | + .build(); |
| 173 | + |
| 174 | + JsonRpcResponse response = authProxy.rpc(request).join(); |
| 175 | + |
| 176 | + assertNotNull(response); |
| 177 | + assertEquals("us-west-2", capturedRegion[0]); |
| 178 | + authProxy.shutdown().join(); |
| 179 | + } |
| 180 | + |
69 | 181 | @Test |
70 | 182 | void testBuilderWithCustomName() { |
71 | 183 | HttpMcpProxy customProxy = HttpMcpProxy.builder() |
@@ -555,4 +667,64 @@ public void handle(HttpExchange exchange) throws IOException { |
555 | 667 | } |
556 | 668 | } |
557 | 669 | } |
| 670 | + |
| 671 | + private record TestIdentity(String token) implements Identity {} |
| 672 | + |
| 673 | + private static final class TestIdentityResolver implements IdentityResolver<TestIdentity> { |
| 674 | + static final TestIdentityResolver INSTANCE = new TestIdentityResolver(); |
| 675 | + |
| 676 | + @Override |
| 677 | + public IdentityResult<TestIdentity> resolveIdentity(Context requestProperties) { |
| 678 | + return IdentityResult.of(new TestIdentity("test-token")); |
| 679 | + } |
| 680 | + |
| 681 | + @Override |
| 682 | + public Class<TestIdentity> identityType() { |
| 683 | + return TestIdentity.class; |
| 684 | + } |
| 685 | + } |
| 686 | + |
| 687 | + private static final class TestAuthScheme implements AuthScheme<HttpRequest, TestIdentity> { |
| 688 | + static final Context.Key<String> REGION_KEY = Context.key("test-region"); |
| 689 | + |
| 690 | + @Override |
| 691 | + public ShapeId schemeId() { |
| 692 | + return ShapeId.from("smithy.test#testAuth"); |
| 693 | + } |
| 694 | + |
| 695 | + @Override |
| 696 | + public Class<HttpRequest> requestClass() { |
| 697 | + return HttpRequest.class; |
| 698 | + } |
| 699 | + |
| 700 | + @Override |
| 701 | + public Class<TestIdentity> identityClass() { |
| 702 | + return TestIdentity.class; |
| 703 | + } |
| 704 | + |
| 705 | + @Override |
| 706 | + public Context getSignerProperties(Context context) { |
| 707 | + var ctx = Context.create(); |
| 708 | + var region = context.get(REGION_KEY); |
| 709 | + if (region != null) { |
| 710 | + ctx.put(REGION_KEY, region); |
| 711 | + } |
| 712 | + return ctx; |
| 713 | + } |
| 714 | + |
| 715 | + @Override |
| 716 | + public Signer<HttpRequest, TestIdentity> signer() { |
| 717 | + return (request, identity, properties) -> { |
| 718 | + var r = request.toModifiable(); |
| 719 | + var h = r.headers().toModifiable(); |
| 720 | + h.setHeader("X-Test-Signed", identity.token()); |
| 721 | + var region = properties.get(REGION_KEY); |
| 722 | + if (region != null) { |
| 723 | + h.setHeader("X-Region", region); |
| 724 | + } |
| 725 | + r.setHeaders(h); |
| 726 | + return new SignResult<>(r); |
| 727 | + }; |
| 728 | + } |
| 729 | + } |
558 | 730 | } |
0 commit comments