package api
import (
"context"
"fmt"
"strings"
"craftdesign.group/quarry/internal/ops"
)
// The owner's hard stops: what stays with a person signed in at a browser
// even when a token of theirs holds every letter they do. One table says
// them: the gate below refuses by it, and the sentence a login's page
// shows is generated from it, so the two cannot drift.
// browserOnlyCode is the stable code of a refusal a token cannot get past.
const browserOnlyCode = "browser_only"
type hardStop struct {
// Words says what the token does not do, in the sentence's grammar.
Words string
// Page is where, in a browser, the person does it: a path under the
// repository the call named, "" for the forge's front page.
Page string
// Match says whether a call is one. nil is a stop another rule keeps
// (refuseToken's: the person's sign-in and credentials) and only
// appears in the sentence.
Match func(desc *ops.Descriptor, in any) bool
}
func opIs(names ...string) func(*ops.Descriptor, any) bool {
return func(desc *ops.Descriptor, _ any) bool {
for _, n := range names {
if desc.Name == n {
return true
}
}
return false
}
}
var hardStops = []hardStop{
{Words: "change branch protection", Page: "/settings", Match: opIs("branch.protection.set", "ci.checks.set")},
{Words: "override a failing check", Page: "/landings", Match: func(_ *ops.Descriptor, in any) bool {
return inputField(in, "Override") != ""
}},
{Words: "delete or hand on a repository", Page: "/settings", Match: func(desc *ops.Descriptor, _ any) bool { return deedOps[desc.Name] }},
{Words: "act as site admin", Page: "", Match: func(desc *ops.Descriptor, _ any) bool { return desc.Auth == ops.Admin }},
// A login token's sync push to a protected branch is refused at the sync
// front (xfer.Credential.Login), not here: it is a stop with no call.
{Words: "push straight to a protected branch", Page: "/settings"},
{Words: "mint or hand out credentials", Page: "/me", Match: func(desc *ops.Descriptor, _ any) bool {
// Every op that returns a secret once is one, whatever it is called:
// a token, a code, a key, a link. The sign-in link returns a link
// no tag marks.
return desc.HasOnceSecret() || desc.Name == "me.signin-link"
}},
{Words: "manage your sign-in and credentials"},
}
// hardStopWords is the table's words as one clause: "a, b, or c".
func hardStopWords() string {
words := make([]string, len(hardStops))
for i, h := range hardStops {
words[i] = h.Words
}
return strings.Join(words[:len(words)-1], ", ") + ", or " + words[len(words)-1]
}
// hardStopGate refuses, to a login token, a call the table names. It is
// for login tokens alone: a token that holds its letters on a repository
// keeps using them there (RFC 0021), and a forge agent's site-admin reach
// is by design.
func (d Deps) hardStopGate(ctx context.Context, desc *ops.Descriptor, in any) error {
if tok, ok := ops.TokenOf(ctx); !ok || !tok.Login {
return nil
}
for _, h := range hardStops {
if h.Match == nil || !h.Match(desc, in) {
continue
}
next := d.link(h.Page)
if repo := repoNamed(in, desc.Scope == ops.ScopeRepo); repo != "" {
next = d.link("/r/" + repo + h.Page)
}
return &ops.Refusal{Code: browserOnlyCode, Class: ops.ErrForbidden,
Message: fmt.Sprintf("%s is for you signed in at a browser: a token does not %s; do it at %s", desc.Name, h.Words, next),
Fields: map[string]string{"next": next}}
}
return nil
}
// commandGate is every command's gate: the hard stops, then the hold.
func (d Deps) commandGate(ctx context.Context, desc *ops.Descriptor, in any) error {
if err := d.hardStopGate(ctx, desc, in); err != nil {
return err
}
return d.legalHoldGate(ctx, desc, in)
}
// HardStops is the gate that keeps a login token to the table, for the
// forge's doors to put in front of a registry they build themselves.
func (d Deps) HardStops() ops.CommandGate { return d.hardStopGate }