-
Notifications
You must be signed in to change notification settings - Fork 150
feat!: add TaskAuthorizationProvider SPI for per-user task authorization #935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kabir
wants to merge
1
commit into
a2aproject:main
Choose a base branch
from
kabir:authorisation-spi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
.../java/org/a2aproject/sdk/compat03/server/grpc/quarkus/QuarkusCallContextFactory_v0_3.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package org.a2aproject.sdk.compat03.server.grpc.quarkus; | ||
|
|
||
| import static org.a2aproject.sdk.server.ServerCallContext.TRANSPORT_KEY; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
| import jakarta.enterprise.inject.Instance; | ||
| import jakarta.inject.Inject; | ||
|
|
||
| import io.grpc.Context; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.stub.StreamObserver; | ||
| import io.quarkus.security.identity.SecurityIdentity; | ||
| import org.a2aproject.sdk.compat03.conversion.A2AProtocol_v0_3; | ||
| import org.a2aproject.sdk.compat03.transport.grpc.context.GrpcContextKeys_v0_3; | ||
| import org.a2aproject.sdk.compat03.transport.grpc.handler.CallContextFactory_v0_3; | ||
| import org.a2aproject.sdk.server.ServerCallContext; | ||
| import org.a2aproject.sdk.server.auth.AuthenticatedUser; | ||
| import org.a2aproject.sdk.server.auth.UnauthenticatedUser; | ||
| import org.a2aproject.sdk.server.auth.User; | ||
| import org.a2aproject.sdk.spec.TransportProtocol; | ||
|
|
||
| @ApplicationScoped | ||
| public class QuarkusCallContextFactory_v0_3 implements CallContextFactory_v0_3 { | ||
|
|
||
| @Inject | ||
| Instance<SecurityIdentity> securityIdentityInstance; | ||
|
|
||
| @Override | ||
| public <V> ServerCallContext create(StreamObserver<V> responseObserver) { | ||
| User user; | ||
| if (securityIdentityInstance.isResolvable()) { | ||
| SecurityIdentity securityIdentity = securityIdentityInstance.get(); | ||
| if (!securityIdentity.isAnonymous()) { | ||
| user = new AuthenticatedUser(securityIdentity.getPrincipal().getName()); | ||
| } else { | ||
| user = UnauthenticatedUser.INSTANCE; | ||
| } | ||
| } else { | ||
| user = UnauthenticatedUser.INSTANCE; | ||
| } | ||
|
|
||
| Map<String, Object> state = new HashMap<>(); | ||
| state.put(TRANSPORT_KEY, TransportProtocol.GRPC); | ||
| state.put("grpc_response_observer", responseObserver); | ||
|
|
||
| Context currentContext = Context.current(); | ||
| if (currentContext != null) { | ||
| state.put("grpc_context", currentContext); | ||
| io.grpc.Metadata grpcMetadata = GrpcContextKeys_v0_3.METADATA_KEY.get(currentContext); | ||
| if (grpcMetadata != null) { | ||
| state.put("grpc_metadata", grpcMetadata); | ||
| Map<String, String> headers = new HashMap<>(); | ||
| for (String key : grpcMetadata.keys()) { | ||
| if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { | ||
| continue; | ||
| } | ||
| headers.put(key, grpcMetadata.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER))); | ||
| } | ||
|
kabir marked this conversation as resolved.
|
||
| state.put("headers", headers); | ||
| } | ||
| String methodName = GrpcContextKeys_v0_3.METHOD_NAME_KEY.get(currentContext); | ||
| if (methodName != null) { | ||
| state.put("grpc_method_name", methodName); | ||
| } | ||
| String peerInfo = GrpcContextKeys_v0_3.PEER_INFO_KEY.get(currentContext); | ||
| if (peerInfo != null) { | ||
| state.put("grpc_peer_info", peerInfo); | ||
| } | ||
| } | ||
|
|
||
| return new ServerCallContext(user, state, new HashSet<>(), A2AProtocol_v0_3.PROTOCOL_VERSION); | ||
| } | ||
| } | ||
65 changes: 65 additions & 0 deletions
65
...oject/sdk/compat03/server/grpc/quarkus/QuarkusA2AGrpc_v0_3_WithTaskAuthorizationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package org.a2aproject.sdk.compat03.server.grpc.quarkus; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.ManagedChannelBuilder; | ||
| import io.quarkus.test.junit.QuarkusTest; | ||
| import io.quarkus.test.junit.TestProfile; | ||
| import org.a2aproject.sdk.compat03.client.ClientBuilder_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.grpc.GrpcTransport_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.grpc.GrpcTransportConfigBuilder_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.spi.interceptors.auth.AuthInterceptor_v0_3; | ||
| import org.a2aproject.sdk.compat03.conversion.AbstractA2AServerWithTaskAuthorizationTest_v0_3; | ||
| import org.a2aproject.sdk.compat03.conversion.TaskAuthorizationTestProfile_v0_3; | ||
| import org.a2aproject.sdk.compat03.spec.TransportProtocol_v0_3; | ||
| import org.junit.jupiter.api.AfterAll; | ||
|
|
||
| @QuarkusTest | ||
| @TestProfile(TaskAuthorizationTestProfile_v0_3.class) | ||
| public class QuarkusA2AGrpc_v0_3_WithTaskAuthorizationTest extends AbstractA2AServerWithTaskAuthorizationTest_v0_3 { | ||
|
|
||
| private static final Map<String, ManagedChannel> channels = new ConcurrentHashMap<>(); | ||
|
|
||
| public QuarkusA2AGrpc_v0_3_WithTaskAuthorizationTest() { | ||
| super(8081); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTransportProtocol() { | ||
| return TransportProtocol_v0_3.GRPC.asString(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTransportUrl() { | ||
| return "localhost:8081"; | ||
| } | ||
|
|
||
| @Override | ||
| protected void configureTransportWithCredentials(ClientBuilder_v0_3 builder, String username, String password) { | ||
| AuthInterceptor_v0_3 authInterceptor = new AuthInterceptor_v0_3( | ||
| (schemeName, context) -> BASIC_AUTH_SCHEME_NAME.equals(schemeName) | ||
| ? getEncodedCredentials(username, password) : null); | ||
| builder.withTransport(GrpcTransport_v0_3.class, new GrpcTransportConfigBuilder_v0_3() | ||
| .channelFactory(target -> { | ||
| ManagedChannel channel = ManagedChannelBuilder.forTarget(target).usePlaintext().build(); | ||
| channels.put(username, channel); | ||
| return channel; | ||
| }) | ||
| .addInterceptor(authInterceptor)); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void closeChannels() { | ||
| channels.values().forEach(ch -> { | ||
| ch.shutdownNow(); | ||
| try { | ||
| ch.awaitTermination(10, TimeUnit.SECONDS); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...k/compat03/server/apps/quarkus/QuarkusA2AJSONRPC_v0_3_WithTaskAuthorizationVertxTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.a2aproject.sdk.compat03.server.apps.quarkus; | ||
|
|
||
| import io.quarkus.test.junit.QuarkusTest; | ||
| import io.quarkus.test.junit.TestProfile; | ||
| import io.vertx.core.Vertx; | ||
| import jakarta.inject.Inject; | ||
| import org.a2aproject.sdk.client.http.VertxA2AHttpClient; | ||
| import org.a2aproject.sdk.compat03.client.ClientBuilder_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.jsonrpc.JSONRPCTransport_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.jsonrpc.JSONRPCTransportConfigBuilder_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.spi.interceptors.auth.AuthInterceptor_v0_3; | ||
| import org.a2aproject.sdk.compat03.conversion.AbstractA2AServerWithTaskAuthorizationTest_v0_3; | ||
| import org.a2aproject.sdk.compat03.conversion.TaskAuthorizationTestProfile_v0_3; | ||
| import org.a2aproject.sdk.compat03.spec.TransportProtocol_v0_3; | ||
|
|
||
| @QuarkusTest | ||
| @TestProfile(TaskAuthorizationTestProfile_v0_3.class) | ||
| public class QuarkusA2AJSONRPC_v0_3_WithTaskAuthorizationVertxTest extends AbstractA2AServerWithTaskAuthorizationTest_v0_3 { | ||
|
|
||
| @Inject | ||
| Vertx vertx; | ||
|
|
||
| public QuarkusA2AJSONRPC_v0_3_WithTaskAuthorizationVertxTest() { | ||
| super(8081); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTransportProtocol() { | ||
| return TransportProtocol_v0_3.JSONRPC.asString(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTransportUrl() { | ||
| return "http://localhost:8081"; | ||
| } | ||
|
|
||
| @Override | ||
| protected void configureTransportWithCredentials(ClientBuilder_v0_3 builder, String username, String password) { | ||
| AuthInterceptor_v0_3 authInterceptor = new AuthInterceptor_v0_3( | ||
| (schemeName, context) -> BASIC_AUTH_SCHEME_NAME.equals(schemeName) | ||
| ? getEncodedCredentials(username, password) : null); | ||
| builder.withTransport(JSONRPCTransport_v0_3.class, | ||
| new JSONRPCTransportConfigBuilder_v0_3() | ||
| .httpClient(new VertxA2AHttpClient(vertx)) | ||
| .addInterceptor(authInterceptor)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../sdk/compat03/server/rest/quarkus/QuarkusA2ARest_v0_3_WithTaskAuthorizationVertxTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.a2aproject.sdk.compat03.server.rest.quarkus; | ||
|
|
||
| import io.quarkus.test.junit.QuarkusTest; | ||
| import io.quarkus.test.junit.TestProfile; | ||
| import io.vertx.core.Vertx; | ||
| import jakarta.inject.Inject; | ||
| import org.a2aproject.sdk.client.http.VertxA2AHttpClient; | ||
| import org.a2aproject.sdk.compat03.client.ClientBuilder_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.rest.RestTransport_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.rest.RestTransportConfigBuilder_v0_3; | ||
| import org.a2aproject.sdk.compat03.client.transport.spi.interceptors.auth.AuthInterceptor_v0_3; | ||
| import org.a2aproject.sdk.compat03.conversion.AbstractA2AServerWithTaskAuthorizationTest_v0_3; | ||
| import org.a2aproject.sdk.compat03.conversion.TaskAuthorizationTestProfile_v0_3; | ||
| import org.a2aproject.sdk.compat03.spec.TransportProtocol_v0_3; | ||
|
|
||
| @QuarkusTest | ||
| @TestProfile(TaskAuthorizationTestProfile_v0_3.class) | ||
| public class QuarkusA2ARest_v0_3_WithTaskAuthorizationVertxTest extends AbstractA2AServerWithTaskAuthorizationTest_v0_3 { | ||
|
|
||
| @Inject | ||
| Vertx vertx; | ||
|
|
||
| public QuarkusA2ARest_v0_3_WithTaskAuthorizationVertxTest() { | ||
| super(8081); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTransportProtocol() { | ||
| return TransportProtocol_v0_3.HTTP_JSON.asString(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTransportUrl() { | ||
| return "http://localhost:8081"; | ||
| } | ||
|
|
||
| @Override | ||
| protected void configureTransportWithCredentials(ClientBuilder_v0_3 builder, String username, String password) { | ||
| AuthInterceptor_v0_3 authInterceptor = new AuthInterceptor_v0_3( | ||
| (schemeName, context) -> BASIC_AUTH_SCHEME_NAME.equals(schemeName) | ||
| ? getEncodedCredentials(username, password) : null); | ||
| builder.withTransport(RestTransport_v0_3.class, | ||
| new RestTransportConfigBuilder_v0_3() | ||
| .httpClient(new VertxA2AHttpClient(vertx)) | ||
| .addInterceptor(authInterceptor)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a duplicate of QuarkusCallContextFactory maybe we should have a helper method for this