feat: let an account sign in with more than one SSH key

A member was bound to the single key they first signed in with, so a new
machine got the finger card and could not reach the hub (or renew a plan).
Extra keys live in user_keys; EnsureUser, UserByFingerprint and the finger
check accept them. Operators add one with: agentbbs add-key NAME <pubkey|fp>.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Anthony Ettinger 2026-11-30 08:51:32 +00:00
parent 8a6e2f905f
commit d64d5f1dba
4 changed files with 149 additions and 4 deletions

50
cmd/agentbbs/addkey.go Normal file
View file

@ -0,0 +1,50 @@
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/profullstack/agentbbs/internal/auth"
"github.com/profullstack/agentbbs/internal/store"
)
// addKeyCmd lets one more SSH key sign in as an existing member, so an account
// is not tied to the single machine it first signed in from:
//
// agentbbs add-key NAME 'ssh-ed25519 AAAA… comment'
// agentbbs add-key NAME SHA256:…
//
// Without it, a member on a new machine gets the finger card (the key is "not
// theirs") and cannot reach the hub.
func addKeyCmd(st store.Store, args []string) {
if len(args) < 2 {
fmt.Fprintln(os.Stderr, "usage: agentbbs add-key <username> <authorized_keys line | SHA256:fingerprint>")
os.Exit(2)
}
name := strings.ToLower(args[0])
key := strings.TrimSpace(strings.Join(args[1:], " "))
fp := key
if !strings.HasPrefix(key, "SHA256:") {
var err error
if fp, err = auth.FingerprintAuthorizedKey(key); err != nil {
fmt.Fprintln(os.Stderr, "parse key:", err)
os.Exit(2)
}
}
u, found, err := st.UserByName(name)
if err != nil || !found {
fmt.Fprintln(os.Stderr, "no such user:", name)
os.Exit(1)
}
if err := st.AddUserKey(u.ID, fp); err != nil {
if errors.Is(err, store.ErrKeyInUse) {
fmt.Fprintln(os.Stderr, "that key already signs in as another account")
} else {
fmt.Fprintln(os.Stderr, "add key:", err)
}
os.Exit(1)
}
fmt.Printf("%s can now sign in with %s\n", u.Name, fp)
}

View file

@ -21,6 +21,7 @@
//
// agentbbs serve (default)
// agentbbs grant-pod NAME MONTHS manually extend a pod subscription
// agentbbs add-key NAME PUBKEY|FP let one more SSH key sign in as NAME
// agentbbs map-domain DOMAIN NAME map a custom domain to a homepage
// agentbbs unmap-domain DOMAIN NAME remove a custom-domain mapping
// agentbbs mint-token NAME issue a WebSocket API token for NAME
@ -169,6 +170,10 @@ func main() {
}
defer st.Close()
if len(os.Args) > 1 && os.Args[1] == "add-key" {
addKeyCmd(st, os.Args[2:])
return
}
if len(os.Args) > 1 && os.Args[1] == "grant-pod" {
grantPod(st, os.Args[2:])
return
@ -2355,8 +2360,10 @@ func (a *app) handleFinger(s ssh.Session, username string) bool {
if err != nil || !found {
return false // unclaimed name → hub (claim flow)
}
if fp := auth.Fingerprint(s.PublicKey()); fp != "" && fp == u.PubKeyFP {
return false // it's them → hub
if fp := auth.Fingerprint(s.PublicKey()); fp != "" {
if ok, _ := a.st.UserHasKey(u.ID, fp); ok {
return false // it's them (primary or extra key) → hub
}
}
lastSeen := "never"