Skip to content

Commit be4b2d5

Browse files
committed
feat: Add an AuthInterceptor that can obtain credentials from a CredentialService
1 parent 61e77d8 commit be4b2d5

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.a2a.client.transport.spi.interceptors.auth;
2+
3+
import java.util.Map;
4+
5+
import io.a2a.client.transport.spi.interceptors.ClientCallContext;
6+
import io.a2a.client.transport.spi.interceptors.ClientCallInterceptor;
7+
import io.a2a.client.transport.spi.interceptors.PayloadAndHeaders;
8+
import io.a2a.spec.AgentCard;
9+
10+
public class AuthInterceptor extends ClientCallInterceptor {
11+
12+
private CredentialService credentialService;
13+
14+
public AuthInterceptor(CredentialService credentialService) {
15+
this.credentialService = credentialService;
16+
}
17+
18+
@Override
19+
public PayloadAndHeaders intercept(String methodName, Object payload, Map<String, String> headers,
20+
AgentCard agentCard, ClientCallContext clientCallContext) {
21+
return null;
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.a2a.client.transport.spi.interceptors.auth;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
5+
import io.a2a.client.transport.spi.interceptors.ClientCallContext;
6+
7+
/**
8+
* Used to retrieve credentials.
9+
*/
10+
public interface CredentialService {
11+
12+
/**
13+
* Retrieves a credential (e.g., token) for a security scheme.
14+
*
15+
* @param securitySchemeName the name of the security scheme
16+
* @param clientCallContext the client call context, which may be {@code null}.
17+
* @return a {@code CompletableFuture} that will complete with the credential string,
18+
* or {@code null} if the credential could not be retrieved
19+
*/
20+
CompletableFuture<String> getCredentials(String securitySchemeName,
21+
ClientCallContext clientCallContext);
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.a2a.client.transport.spi.interceptors.auth;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.concurrent.CompletableFuture;
6+
7+
import io.a2a.client.transport.spi.interceptors.ClientCallContext;
8+
9+
/**
10+
* A simple in-memory store for session-keyed credentials.
11+
* This class uses the 'sessionId' from the {@code ClientCallContext} state to
12+
* store and retrieve credentials
13+
*/
14+
public class InMemoryContextCredentialService implements CredentialService {
15+
16+
private static final String SESSION_ID = "sessionId";
17+
18+
// maps a sessionId to a map of security scheme names to credentials
19+
private final Map<String, Map<String, String>> credentialStore;
20+
21+
public InMemoryContextCredentialService() {
22+
credentialStore = new HashMap<>();
23+
}
24+
25+
@Override
26+
public CompletableFuture<String> getCredentials(String securitySchemeName,
27+
ClientCallContext clientCallContext) {
28+
if (clientCallContext == null || !clientCallContext.getState().containsKey(SESSION_ID)) {
29+
// no credential to retrieve
30+
return CompletableFuture.completedFuture(null);
31+
}
32+
33+
String sessionId = (String) clientCallContext.getState().get(SESSION_ID);
34+
Map<String, String> sessionCredentials = credentialStore.getOrDefault(sessionId, new HashMap<>());
35+
String credential = sessionCredentials.get(securitySchemeName);
36+
return CompletableFuture.completedFuture(credential);
37+
}
38+
39+
/**
40+
* Method to populate the in-memory credential service.
41+
*
42+
* @param sessionId the session ID
43+
* @param securitySchemeName the name of the security scheme
44+
* @param credential the credential string
45+
*/
46+
public void setCredential(String sessionId, String securitySchemeName, String credential) {
47+
Map<String, String> sessionIdCredentials = credentialStore.getOrDefault(sessionId, new HashMap<>());
48+
sessionIdCredentials.put(securitySchemeName, credential);
49+
}
50+
}

0 commit comments

Comments
 (0)