Package com.tnsai.security.approval
Class InMemoryApprovalTokenStore
java.lang.Object
com.tnsai.security.approval.InMemoryApprovalTokenStore
- All Implemented Interfaces:
ApprovalTokenStore
In-memory implementation of ApprovalTokenStore.
Suitable for development and testing. Tokens are lost on restart. For production, use a persistent implementation like Redis or database.
Features
- Thread-safe with ConcurrentHashMap
- Automatic expiry checking
- Audit logging of approvals
Usage Example
ApprovalTokenStore store = new InMemoryApprovalTokenStore();
// Create token
ApprovalToken token = store.createToken(
"agent-1",
"delete-user",
Duration.ofMinutes(5)
);
// Simulate approval
store.approve(token.getTokenId(), "admin");
// Validate
boolean valid = store.validateAndConsume(
token.getTokenId(),
"agent-1",
"delete-user"
);
// Cleanup
store.cleanupExpired();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanApproves a token.intCleans up expired tokens.voidclear()Clears all tokens (for testing).intcount()Gets total token count.createToken(String agentId, String actionName, Duration ttl) Creates a new approval token.Creates a token with parameters.booleanDeletes a token.getAll()Gets all tokens (for testing/debugging).Gets a token by ID.Lists pending tokens.listPendingForAgent(String agentId) Lists pending tokens for a specific agent.booleanRejects a token.voidsave(ApprovalToken token) Saves a token to the store.booleanvalidateAndConsume(String tokenId, String agentId, String actionName) Validates the token binding and requires a successful consume.
-
Constructor Details
-
InMemoryApprovalTokenStore
public InMemoryApprovalTokenStore()
-
-
Method Details
-
createToken
Description copied from interface:ApprovalTokenStoreCreates a new approval token.- Specified by:
createTokenin interfaceApprovalTokenStore- Parameters:
agentId- agent IDactionName- action namettl- time to live- Returns:
- created token
-
createToken
public ApprovalToken createToken(String agentId, String actionName, Map<String, Object> parameters, Duration ttl) Description copied from interface:ApprovalTokenStoreCreates a token with parameters.- Specified by:
createTokenin interfaceApprovalTokenStore- Parameters:
agentId- agent IDactionName- action nameparameters- action parametersttl- time to live- Returns:
- created token
-
save
Description copied from interface:ApprovalTokenStoreSaves a token to the store.- Specified by:
savein interfaceApprovalTokenStore- Parameters:
token- token to save
-
getToken
Description copied from interface:ApprovalTokenStoreGets a token by ID.- Specified by:
getTokenin interfaceApprovalTokenStore- Parameters:
tokenId- token ID- Returns:
- token or empty if not found
-
validateAndConsume
Description copied from interface:ApprovalTokenStoreValidates the token binding and requires a successful consume.This method:
- Looks up the token by ID
- Checks agent and action match (without consuming)
- Requires a successful
ApprovalToken.consume()— approved, not expired, and not already consumed
- Specified by:
validateAndConsumein interfaceApprovalTokenStore- Parameters:
tokenId- token IDagentId- expected agent IDactionName- expected action name- Returns:
- true if valid and consumed
-
approve
Description copied from interface:ApprovalTokenStoreApproves a token.- Specified by:
approvein interfaceApprovalTokenStore- Parameters:
tokenId- token IDapprover- who approved- Returns:
- true if successfully approved
-
reject
Description copied from interface:ApprovalTokenStoreRejects a token.- Specified by:
rejectin interfaceApprovalTokenStore- Parameters:
tokenId- token IDrejector- who rejectedreason- rejection reason- Returns:
- true if successfully rejected
-
listPending
Description copied from interface:ApprovalTokenStoreLists pending tokens.- Specified by:
listPendingin interfaceApprovalTokenStore- Returns:
- list of pending tokens
-
listPendingForAgent
Description copied from interface:ApprovalTokenStoreLists pending tokens for a specific agent.- Specified by:
listPendingForAgentin interfaceApprovalTokenStore- Parameters:
agentId- agent ID- Returns:
- list of pending tokens for agent
-
delete
Description copied from interface:ApprovalTokenStoreDeletes a token.- Specified by:
deletein interfaceApprovalTokenStore- Parameters:
tokenId- token ID- Returns:
- true if deleted
-
cleanupExpired
public int cleanupExpired()Description copied from interface:ApprovalTokenStoreCleans up expired tokens.- Specified by:
cleanupExpiredin interfaceApprovalTokenStore- Returns:
- number of tokens cleaned
-
count
public int count()Description copied from interface:ApprovalTokenStoreGets total token count.- Specified by:
countin interfaceApprovalTokenStore- Returns:
- token count
-
clear
public void clear()Clears all tokens (for testing). -
getAll
Gets all tokens (for testing/debugging).- Returns:
- all tokens
-