// agenttokenvault.go — carry a freshly minted repository agent token,
// and everything the page needs to describe it, from the POST that minted
// it to the GET that shows it, bound to the person and repository that
// asked. The URL carries only an opaque nonce, never the value.
//
// The process-wide secretVault (secretvault.go) is the right carrier for
// a secret with no further meaning: whoever spends the nonce gets the
// value. This is a different contract. The page beside the token names
// the agent, its role's ceiling and the exact sync URL, none of which may
// be taken from the query — an `an=` in a URL is attacker-controlled and
// would reach the page, and a shell recipe, unquoted. And the nonce must
// not be a bearer a DIFFERENT repository's admin can redeem by pasting it
// through their own settings page.
//
// So the payload is a small typed record held server-side, keyed by the
// nonce, and take() checks the asking person and repository against it
// BEFORE it forgets anything. A mismatch reads nothing and leaves the
// rightful holder's secret intact: a request that could not be the one
// that minted it does not spend it.
package web
import (
"crypto/rand"
"encoding/hex"
"sync"
"time"
)
// agentTokenTTL matches the secret vault: long enough for the redirect
// the reader is already following, short enough that an abandoned mint
// does not linger.
const agentTokenTTL = 2 * time.Minute
// agentTokenPayload is what the once panel needs, all of it decided at
// mint time and none of it read back from the request that renders it.
type agentTokenPayload struct {
user string
repo string
value string
agent string
ceiling string
at time.Time
}
type agentTokenVault struct {
mu sync.Mutex
m map[string]agentTokenPayload
}
func newAgentTokenVault() *agentTokenVault {
return &agentTokenVault{m: map[string]agentTokenPayload{}}
}
// repoAgentTokens is the process-wide vault. One forge, one process.
var repoAgentTokens = newAgentTokenVault()
// stash files a payload for user and repo and returns the nonce that
// stands for it. An empty value (a mint the page could not show) files
// nothing and returns "", so there is never a nonce that resolves to an
// empty token.
func (v *agentTokenVault) stash(user, repo string, p agentTokenPayload) string {
if p.value == "" {
return ""
}
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return ""
}
nonce := hex.EncodeToString(b)
p.user, p.repo, p.at = user, repo, time.Now()
v.mu.Lock()
defer v.mu.Unlock()
v.sweepLocked()
v.m[nonce] = p
return nonce
}
// take returns the payload for nonce when it was filed for this user and
// repo, and forgets it. A request from anyone else, or for any other
// repository, gets nothing and does NOT consume it: the check comes
// before the delete, so a nonce seen out of place cannot spend the
// rightful holder's secret. Single use for the right holder, as "shown
// once" claims.
func (v *agentTokenVault) take(nonce, user, repo string) (agentTokenPayload, bool) {
if nonce == "" {
return agentTokenPayload{}, false
}
v.mu.Lock()
defer v.mu.Unlock()
v.sweepLocked()
e, ok := v.m[nonce]
if !ok || e.user != user || e.repo != repo || time.Since(e.at) > agentTokenTTL {
return agentTokenPayload{}, false
}
delete(v.m, nonce)
return e, true
}
func (v *agentTokenVault) sweepLocked() {
for k, e := range v.m {
if time.Since(e.at) > agentTokenTTL {
delete(v.m, k)
}
}
}