fix: validate provisioned account kind (#112)
Some checks are pending
CI / build (push) Waiting to run
deploy / deploy (push) Waiting to run
test / test (push) Waiting to run

This commit is contained in:
RissRIce 2026-08-10 21:18:44 -06:00 committed by GitHub
parent bf777a1475
commit 03a2ce3f8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 1 deletions

View file

@ -34,6 +34,10 @@ func provisionUser(st store.Store, args []string) {
pubkeyFile := fs.String("pubkey-file", "", "read the SSH public key from this file")
kind := fs.String("kind", string(auth.Member), "account kind: member | agent")
fs.Parse(args)
accountKind, ok := parseProvisionKind(*kind)
if !ok {
fail(`invalid --kind: must be "member" or "agent"`)
}
// Normalize with the same rules the hub uses for self-service joins, so
// store-provisioned handles are indistinguishable from join@ ones.
@ -67,7 +71,7 @@ func provisionUser(st store.Store, args []string) {
fail(fmt.Sprintf("this key already belongs to member %q (fp %s)", existing.Name, fp))
}
u, err := st.EnsureUser(handle, *kind, fp)
u, err := st.EnsureUser(handle, string(accountKind), fp)
if err != nil {
if errors.Is(err, store.ErrKeyMismatch) {
fail(fmt.Sprintf("handle %q is already registered with a different key", handle))
@ -84,6 +88,17 @@ func provisionUser(st store.Store, args []string) {
})
}
func parseProvisionKind(value string) (auth.Kind, bool) {
switch auth.Kind(strings.ToLower(strings.TrimSpace(value))) {
case auth.Member:
return auth.Member, true
case auth.Agent:
return auth.Agent, true
default:
return "", false
}
}
func fail(msg string) {
fmt.Fprintln(os.Stderr, "provision-user: "+msg)
os.Exit(1)

View file

@ -0,0 +1,28 @@
package main
import (
"testing"
"github.com/profullstack/agentbbs/internal/auth"
)
func TestParseProvisionKind(t *testing.T) {
tests := []struct {
input string
want auth.Kind
ok bool
}{
{input: "member", want: auth.Member, ok: true},
{input: " Agent ", want: auth.Agent, ok: true},
{input: "guest", ok: false},
{input: "admin", ok: false},
{input: "", ok: false},
}
for _, test := range tests {
got, ok := parseProvisionKind(test.input)
if got != test.want || ok != test.ok {
t.Errorf("parseProvisionKind(%q) = (%q, %t), want (%q, %t)", test.input, got, ok, test.want, test.ok)
}
}
}