package api
import (
"context"
"fmt"
"slices"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// reissueInOf reads an asked input, a value or a pointer, as the replacement's.
func reissueInOf(in any) agentReissueIn {
switch v := in.(type) {
case agentReissueIn:
return v
case *agentReissueIn:
return *v
}
return agentReissueIn{}
}
// planReissue fixes, when a replacement is asked for, what approval will
// do: the token copied, its limits and the tokens that end, each as of
// now. The page shows this and approval carries out exactly it. The
// agent must be the asker's person's own (checked by the preflight);
// asking about oneself needs a pickup code, since approving revokes the
// very token the answer would be read with; and a token asks only for
// what it holds itself.
func (d Deps) planReissue(ctx context.Context, person, pickup string, in any) (agentReissueIn, error) {
ri := reissueInOf(in)
as, err := d.agents()
if err != nil {
return ri, err
}
caller := ops.User(ctx)
if ri.Name == caller && pickup == "" {
return ri, fmt.Errorf("replacing your own token revokes the one you are asking with, and you could not collect the new one: add Quarry-Pickup (the hex SHA-256 of a key you keep) and collect it with POST /api/v1/pickups/collect: %w", ops.ErrInvalid)
}
plan, ok, err := as.PlanReissue(ri.Name)
if err != nil {
return ri, err
}
if !ok {
return ri, fmt.Errorf("%s holds no live token to copy; ask for a new token (agent.token.new) instead: %w", ri.Name, ops.ErrInvalid)
}
if tok, byToken := ops.TokenOf(ctx); byToken {
toks, err := as.Tokens(caller)
if err != nil {
return ri, err
}
found := false
for _, t := range toks {
if t.ID == tok.ID {
found = true
if err := d.withinToken(caller, t, ri.Name, plan); err != nil {
return ri, err
}
}
}
if !found && tok.Limited {
return ri, fmt.Errorf("a token limited to repositories asks only within its own limits, and this one cannot be read: %w", ops.ErrForbidden)
}
}
ri.Plan = &plan
return ri, nil
}
// probeLetters are the concrete capability letters (oltp's allCaps), each
// asked of a token's effective set in turn.
const probeLetters = "abcefghijklmnopqrstwxyz234567ABCD"
// letterAt is whether a token of account, cut to ceiling ("" is the
// account's own letters), may use letter on repo.
func (d Deps) letterAt(account, ceiling, repo string, letter rune) bool {
if d.CapsFor == nil {
return true
}
l := string(letter)
return d.CapsFor(account, repo).HasAny(l) && (ceiling == "" || oltp.CapSet(ceiling).HasAny(l))
}
// withinToken is nil when what a replacement of target's token would mint
// is no wider than the asking token of askerAcct: scopes and repositories
// inside, and the letters each can actually use (the account's own,
// cut to its ceiling) inside, since an empty ceiling means a different
// thing for a different account.
func (d Deps) withinToken(askerAcct string, asker oltp.Token, target string, plan oltp.ReissuePlan) error {
wider := fmt.Errorf("the replacement would hold more than the token you ask with (scopes, repositories or letters); ask with a token that holds at least that: %w", ops.ErrForbidden)
for _, sc := range plan.Scopes {
if !asker.Has(sc) {
return wider
}
}
if len(asker.Repos) > 0 {
if len(plan.Repos) == 0 {
return wider
}
for _, r := range plan.Repos {
if !slices.Contains(asker.Repos, r) {
return wider
}
}
}
// Where to compare: the repositories either names, or the forge level
// and every repository when neither does.
repos := append(slices.Clone(plan.Repos), asker.Repos...)
if len(repos) == 0 {
repos = []string{""}
if d.Repos != nil {
repos = append(repos, d.Repos.Names()...)
}
}
for _, repo := range repos {
for _, l := range probeLetters {
if d.letterAt(target, plan.Ceiling, repo, l) && !d.letterAt(askerAcct, asker.Ceiling, repo, l) {
return wider
}
}
}
return nil
}