|
21 | 21 | * configuration is required. |
22 | 22 | * |
23 | 23 | * <pre>{@code |
| 24 | + * import java.util.concurrent.ConcurrentHashMap; |
| 25 | + * import java.util.concurrent.ConcurrentMap; |
| 26 | + * |
| 27 | + * import jakarta.enterprise.context.ApplicationScoped; |
| 28 | + * |
| 29 | + * import org.a2aproject.sdk.server.ServerCallContext; |
| 30 | + * import org.a2aproject.sdk.server.auth.TaskAuthorizationProvider; |
| 31 | + * import org.a2aproject.sdk.server.auth.TaskOperation; |
| 32 | + * |
24 | 33 | * @ApplicationScoped |
25 | 34 | * public class MyTaskAuthorizationProvider implements TaskAuthorizationProvider { |
| 35 | + * private final ConcurrentMap<String, String> ownershipStore = new ConcurrentHashMap<>(); |
26 | 36 | * |
27 | 37 | * @Override |
28 | 38 | * public boolean checkRead(ServerCallContext context, String taskId, TaskOperation op) { |
29 | | - * User user = context.getUser(); |
30 | | - * // look up ownership in your backing store |
31 | | - * return isOwner(user, taskId); |
| 39 | + * return isOwner(context, taskId); |
| 40 | + * } |
| 41 | + * |
| 42 | + * private boolean isOwner(ServerCallContext context, String taskId) { |
| 43 | + * String owner = ownershipStore.get(taskId); |
| 44 | + * return owner != null |
| 45 | + * && context.getUser() != null |
| 46 | + * && owner.equals(context.getUser().getUsername()); |
32 | 47 | * } |
33 | 48 | * |
34 | 49 | * @Override |
35 | 50 | * public boolean checkWrite(ServerCallContext context, String taskId, TaskOperation op) { |
36 | | - * return checkRead(context, taskId, op); // same rule |
| 51 | + * return isOwner(context, taskId); |
37 | 52 | * } |
38 | 53 | * |
39 | 54 | * @Override |
40 | 55 | * public boolean checkCreate(ServerCallContext context, TaskOperation op) { |
41 | | - * return context.getUser().isAuthenticated(); |
| 56 | + * return context.getUser() != null && context.getUser().isAuthenticated(); |
42 | 57 | * } |
43 | 58 | * |
44 | 59 | * @Override |
45 | 60 | * public boolean isTaskRecorded(String taskId) { |
46 | | - * return ownershipStore.contains(taskId); |
| 61 | + * return ownershipStore.containsKey(taskId); |
47 | 62 | * } |
48 | 63 | * |
49 | 64 | * @Override |
50 | 65 | * public void recordOwnership(ServerCallContext context, String taskId, TaskOperation op) { |
51 | | - * ownershipStore.put(taskId, context.getUser().getUsername()); |
| 66 | + * if (context.getUser() != null) { |
| 67 | + * ownershipStore.putIfAbsent(taskId, context.getUser().getUsername()); |
| 68 | + * } |
52 | 69 | * } |
53 | 70 | * } |
54 | 71 | * }</pre> |
|
0 commit comments