feat: let an account sign in with more than one SSH key (#178)

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 00:52:46 -08:00 • committed by GitHub
parent 8a6e2f905f
commit fc8206d0e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 149 additions and 4 deletions

View file

@ -74,8 +74,15 @@ type Store interface {
// kind and key fingerprint on first sight. If the name exists with a
// different fingerprint, ErrKeyMismatch is returned.
EnsureUser(name, kind, pubkeyFP string) (User, error)
// UserByFingerprint finds an account by SSH key fingerprint.
// UserByFingerprint finds an account by SSH key fingerprint: its primary
// key (users.pubkey_fp) or any extra key added with AddUserKey.
UserByFingerprint(fp string) (User, bool, error)
// AddUserKey lets one more SSH key sign in as the user, so a member with
// several machines is not bound to one key. Idempotent for the same user;
// ErrKeyInUse when the key already belongs to another account.
AddUserKey(userID int64, fp string) error
// UserHasKey reports whether fp is the user's primary or an extra key.
UserHasKey(userID int64, fp string) (bool, error)
// UserByName finds an account by exact username (no creation).
UserByName(name string) (User, bool, error)
// LastSeen reports the start of the user's most recent session.
@ -345,6 +352,9 @@ type DomainMap struct {
// ErrKeyMismatch means a username is already registered with another key.
var ErrKeyMismatch = errors.New("username registered with a different key")
// ErrKeyInUse means the key already signs in as a different account.
var ErrKeyInUse = errors.New("key already belongs to another account")
// ErrInvalidUsername is returned when a new account name is not a safe handle.
var ErrInvalidUsername = errors.New("invalid username: use 2-32 lowercase letters, digits or dashes, starting with a letter or digit")
@ -449,6 +459,12 @@ CREATE TABLE IF NOT EXISTS users (
pubkey_fp TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
);
CREATE TABLE IF NOT EXISTS user_keys (
fp TEXT PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
added_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now'))
);
CREATE INDEX IF NOT EXISTS idx_user_keys_user ON user_keys(user_id);
CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY,
user_id INTEGER,
@ -603,16 +619,52 @@ func (s *sqliteStore) EnsureUser(name, kind, fp string) (User, error) {
return User{}, err
}
if u.PubKeyFP != "" && fp != "" && u.PubKeyFP != fp {
return User{}, ErrKeyMismatch
if ok, err := s.UserHasKey(u.ID, fp); err != nil {
return User{}, err
} else if !ok {
return User{}, ErrKeyMismatch
}
}
return u, nil
}
func (s *sqliteStore) UserHasKey(userID int64, fp string) (bool, error) {
if fp == "" {
return false, nil
}
var n int
err := s.db.QueryRow(`SELECT
(SELECT COUNT(*) FROM users WHERE id = ? AND pubkey_fp = ?) +
(SELECT COUNT(*) FROM user_keys WHERE user_id = ? AND fp = ?)`,
userID, fp, userID, fp).Scan(&n)
return n > 0, err
}
func (s *sqliteStore) AddUserKey(userID int64, fp string) error {
if fp == "" {
return errors.New("empty key fingerprint")
}
if ok, err := s.UserHasKey(userID, fp); err != nil || ok {
return err
}
if other, found, err := s.UserByFingerprint(fp); err != nil {
return err
} else if found && other.ID != userID {
return ErrKeyInUse
}
_, err := s.db.Exec(`INSERT INTO user_keys (fp, user_id) VALUES (?, ?)`, fp, userID)
return err
}
func (s *sqliteStore) UserByFingerprint(fp string) (User, bool, error) {
if fp == "" {
return User{}, false, nil
}
u, err := scanUser(s.db.QueryRow(`SELECT `+userCols+` FROM users WHERE pubkey_fp = ?`, fp))
if errors.Is(err, sql.ErrNoRows) {
u, err = scanUser(s.db.QueryRow(`SELECT `+userCols+` FROM users
WHERE id = (SELECT user_id FROM user_keys WHERE fp = ?)`, fp))
}
if errors.Is(err, sql.ErrNoRows) {
return User{}, false, nil
}