Skip to content
quarry Artifact [aaa23f79d8]
Login

Artifact [aaa23f79d8]

Artifact aaa23f79d8cef38e6f7490e4e47069c94b61264808d0c8f51ff047b0a8e79d9e:


package api

import (
	"encoding/json"
)

// MintView is the limits of a token a request will mint, or did: the
// same facts a setup request's page draws for its credential, so a plan
// step or a replacement is read as closely as a setup is. It is told to
// the person asked only; the asker learns what it was given from the
// credentials beside the secret.
type MintView struct {
	Agent  string   `json:"agent"`
	Scopes []string `json:"scopes"`
	// Repos is where the token reaches; empty is every repository the
	// agent reaches.
	Repos []string `json:"repos"`
	// Ceiling is the letters the token is cut to; empty is the agent's own.
	Ceiling string `json:"ceiling"`
	// Days is the fixed life in days, 0 for none; Expires is a fixed end
	// date a replacement keeps from the token it copies.
	Days    int    `json:"days"`
	Expires string `json:"expires,omitempty"`
	// Replaces are the live tokens approving ends, while the request waits.
	Replaces []MintRevoked `json:"replaces,omitempty"`
}

// MintRevoked names a token a replacement ends, never its value.
type MintRevoked struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

// mintViewOf is what an op that mints a token will mint, nil for any
// other op or any reader but the owner. answer is what the approved call
// answered, nil while it waits. A replacement is shown from the plan
// fixed when it was asked, never from the tokens as they are now.
func (d Deps) mintViewOf(op string, input, answer json.RawMessage, owner, who string, pending bool) *MintView {
	if who != owner {
		return nil
	}
	switch op {
	case "agent.token.new":
		var in agentTokenNewIn
		if json.Unmarshal(input, &in) != nil {
			return nil
		}
		return &MintView{Agent: in.Agent, Scopes: orEmpty(splitList(in.Scopes)), Repos: orEmpty(splitList(in.Repos)),
			Ceiling: in.Ceiling, Days: in.Days}
	case reissueOp:
		var in agentReissueIn
		if json.Unmarshal(input, &in) != nil {
			return nil
		}
		if len(answer) > 0 {
			var out agentTokenReissueOut
			if json.Unmarshal(answer, &out) != nil || out.Token == nil {
				return nil
			}
			return &MintView{Agent: in.Name, Scopes: orEmpty(out.Token.Scopes), Repos: orEmpty(out.Token.Repos),
				Ceiling: out.Token.Ceiling, Expires: out.Token.Expires}
		}
		if in.Plan == nil {
			return nil
		}
		v := &MintView{Agent: in.Name, Scopes: orEmpty(in.Plan.Scopes), Repos: orEmpty(in.Plan.Repos),
			Ceiling: in.Plan.Ceiling, Expires: in.Plan.Expires}
		if pending {
			for _, e := range in.Plan.Ends {
				v.Replaces = append(v.Replaces, MintRevoked{ID: e.ID, Name: e.Name})
			}
		}
		return v
	}
	return nil
}