|
1 | 1 | package org.a2aproject.sdk.server.common.quarkus; |
2 | 2 |
|
| 3 | +import java.util.Map; |
| 4 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 5 | + |
3 | 6 | import jakarta.enterprise.inject.Instance; |
4 | 7 | import jakarta.inject.Inject; |
5 | 8 | import jakarta.inject.Singleton; |
6 | 9 |
|
7 | 10 | import io.quarkus.arc.Arc; |
| 11 | +import io.quarkus.arc.InjectableContext; |
8 | 12 | import io.quarkus.arc.ManagedContext; |
9 | 13 | import io.quarkus.security.identity.CurrentIdentityAssociation; |
10 | 14 | import io.quarkus.vertx.http.runtime.security.ChallengeData; |
11 | 15 | import io.quarkus.vertx.http.runtime.security.HttpAuthenticator; |
12 | 16 | import io.vertx.core.Context; |
13 | 17 | import io.vertx.ext.web.RoutingContext; |
14 | | -import java.util.Map; |
15 | 18 |
|
16 | 19 | /** |
17 | 20 | * CDI helper for integrating Quarkus security with Vert.x Web routes. |
@@ -122,6 +125,81 @@ public void runInRequestContext(RoutingContext ctx, Runnable task) { |
122 | 125 | } |
123 | 126 | } |
124 | 127 |
|
| 128 | + /** |
| 129 | + * Authenticates the request and executes a task, deferring CDI context destruction to the HTTP |
| 130 | + * response lifecycle. |
| 131 | + * |
| 132 | + * <p>Unlike {@link #runInRequestContext}, this method does <b>not</b> terminate the CDI request |
| 133 | + * context when the task completes. Instead, it: |
| 134 | + * <ol> |
| 135 | + * <li>Activates the CDI request context and captures its {@link InjectableContext.ContextState}</li> |
| 136 | + * <li>Triggers HTTP authentication</li> |
| 137 | + * <li>Executes the task</li> |
| 138 | + * <li><b>Deactivates</b> the context (detaches from the worker thread without destroying beans)</li> |
| 139 | + * <li>Destroys the context state when the HTTP response ends or the connection closes</li> |
| 140 | + * </ol> |
| 141 | + * |
| 142 | + * <p>This is required for streaming (SSE) requests where the task dispatches work to a background |
| 143 | + * thread (via {@code CompletableFuture.runAsync} with a {@code ManagedExecutor}) and returns |
| 144 | + * immediately. The {@code ManagedExecutor} captures the CDI context at submit time and propagates |
| 145 | + * it to the agent thread — but only if the context hasn't been destroyed yet. By deferring |
| 146 | + * destruction to the response lifecycle, the agent thread can access all {@code @RequestScoped} |
| 147 | + * beans (including OIDC token credentials for token propagation). |
| 148 | + * |
| 149 | + * <p>This method is also safe for non-streaming requests: {@code response.end()} is called |
| 150 | + * synchronously inside the task, and the {@code endHandler} fires the cleanup shortly after. |
| 151 | + * |
| 152 | + * @param ctx the Vert.x routing context containing the HTTP request |
| 153 | + * @param task the code to execute within the authenticated request context |
| 154 | + * @throws io.quarkus.security.UnauthorizedException if authentication fails |
| 155 | + * @throws io.quarkus.security.ForbiddenException if authorization fails |
| 156 | + * @throws RuntimeException if the task throws an exception |
| 157 | + */ |
| 158 | + public void runInRequestContextDeferred(RoutingContext ctx, Runnable task) { |
| 159 | + if (Context.isOnEventLoopThread()) { |
| 160 | + throw new IllegalStateException( |
| 161 | + "Cannot perform blocking authentication on event loop thread. Use blockingHandler()."); |
| 162 | + } |
| 163 | + ManagedContext requestContext = Arc.container().requestContext(); |
| 164 | + boolean wasActive = requestContext.isActive(); |
| 165 | + if (wasActive) { |
| 166 | + if (!httpAuthenticator.isUnsatisfied()) { |
| 167 | + var identity = httpAuthenticator.get().attemptAuthentication(ctx).await().indefinitely(); |
| 168 | + currentIdentityAssociation.get().setIdentity(identity); |
| 169 | + } |
| 170 | + task.run(); |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + InjectableContext.ContextState state = requestContext.activate(); |
| 175 | + |
| 176 | + AtomicBoolean destroyed = new AtomicBoolean(false); |
| 177 | + Runnable cleanup = () -> { |
| 178 | + if (destroyed.compareAndSet(false, true)) { |
| 179 | + requestContext.destroy(state); |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + ctx.response().endHandler(v -> cleanup.run()); |
| 184 | + ctx.response().closeHandler(v -> cleanup.run()); |
| 185 | + |
| 186 | + try { |
| 187 | + if (!httpAuthenticator.isUnsatisfied()) { |
| 188 | + var identity = httpAuthenticator.get().attemptAuthentication(ctx).await().indefinitely(); |
| 189 | + currentIdentityAssociation.get().setIdentity(identity); |
| 190 | + } |
| 191 | + task.run(); |
| 192 | + } catch (Throwable t) { |
| 193 | + // Destroy bean instances immediately on error. The subsequent deactivate() in the |
| 194 | + // finally block is still needed to detach the (now-empty) context from the worker |
| 195 | + // thread. destroy(state) only destroys beans — it does not deactivate the context. |
| 196 | + cleanup.run(); |
| 197 | + throw t; |
| 198 | + } finally { |
| 199 | + requestContext.deactivate(); |
| 200 | + } |
| 201 | + } |
| 202 | + |
125 | 203 | /** |
126 | 204 | * Handles authentication or authorization errors by sending the appropriate HTTP error response. |
127 | 205 | * |
|
0 commit comments