Package com.tnsai.security.approval
Class ApprovalToken
java.lang.Object
com.tnsai.security.approval.ApprovalToken
Token for human-in-the-loop approval of sensitive actions.
Approval tokens are used to require human confirmation before executing high-risk actions. Tokens are:
- Bound to a specific agent and action
- Time-limited with configurable expiry
- Optionally single-use (consumed on validation)
Token Lifecycle
1. Action requires approval (@ApprovalRequired)
↓
2. Token created with agentId, actionName, expiry
↓
3. Token sent to approval system (UI, CLI, API)
↓
4. Human approves → token status = APPROVED
↓
5. Action executes with approved token
↓
6. Token consumed (if single-use)
Usage Example
// Create token for action
ApprovalToken token = ApprovalToken.builder()
.agentId("agent-1")
.actionName("delete-user")
.parameters(Map.of("userId", "123"))
.timeToLive(Duration.ofMinutes(5))
.singleUse(true)
.build();
// Store and wait for approval
store.save(token);
// Check status
if (token.isApproved()) {
executeAction();
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for ApprovalToken.static enumToken status. -
Method Summary
Modifier and TypeMethodDescriptionbooleanMarks token as approved.static ApprovalToken.Builderbuilder()Creates a token builder.booleanconsume()Authorizes use of an approved token.static ApprovalTokenCreates a simple token.Returns who rejected the token, or null if it was never rejected.Gets remaining time until expiry.booleanChecks if token is approved.booleanChecks if token is consumed.booleanChecks if token is expired.booleanChecks if token is pending.booleanChecks if token is rejected.booleanbooleanisValid()Checks if token can be used for approval.booleanChecks if token matches the expected action.booleanMarks token as rejected.toString()
-
Method Details
-
builder
Creates a token builder.- Returns:
- new builder
-
create
Creates a simple token.- Parameters:
agentId- agent IDactionName- action namettl- time to live- Returns:
- new token
-
getTokenId
-
getAgentId
-
getActionName
-
getParameters
-
getCreatedAt
-
getExpiresAt
-
isSingleUse
public boolean isSingleUse() -
getRequestedBy
-
getReason
-
getStatus
-
getApprovedBy
-
getApprovedAt
-
getRejectionReason
-
getRejectedBy
Returns who rejected the token, or null if it was never rejected. Kept strictly separate fromgetApprovedBy()so the audit trail never names the rejecter as the approver (TAN-5895). -
isApproved
public boolean isApproved()Checks if token is approved.- Returns:
- true if approved
-
isPending
public boolean isPending()Checks if token is pending.- Returns:
- true if pending
-
isRejected
public boolean isRejected()Checks if token is rejected.- Returns:
- true if rejected
-
isExpired
public boolean isExpired()Checks if token is expired.- Returns:
- true if expired
-
isConsumed
public boolean isConsumed()Checks if token is consumed.- Returns:
- true if consumed
-
isValid
public boolean isValid()Checks if token can be used for approval.- Returns:
- true if usable
-
getRemainingTime
Gets remaining time until expiry.- Returns:
- remaining duration
-
approve
Marks token as approved.Atomic with reject, consume, and expiry. Only a non-expired
ApprovalToken.Status.PENDINGorApprovalToken.Status.APPROVEDtoken can be approved. A consumed, rejected, or expired token cannot be resurrected.- Parameters:
approver- who approved- Returns:
- true if successfully approved
-
reject
Marks token as rejected.Atomic with approve, consume, and expiry. Only a non-expired
ApprovalToken.Status.PENDINGtoken can be rejected.- Parameters:
rejector- who rejectedreason- rejection reason- Returns:
- true if successfully rejected
-
consume
public boolean consume()Authorizes use of an approved token.Atomic with approve, reject, and expiry. An expired token is rejected inside this same transition and marked
ApprovalToken.Status.EXPIRED. Single-use tokens move toApprovalToken.Status.CONSUMED; multi-use tokens stayApprovalToken.Status.APPROVEDso they can authorize again.- Returns:
- true if the token authorized this call
-
matches
Checks if token matches the expected action.- Parameters:
agentId- expected agent IDactionName- expected action name- Returns:
- true if matches
-
toString
-