mirror of
https://github.com/profullstack/agentbbs.git
synced 2026-12-01 01:14:47 +00:00
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:
parent
8a6e2f905f
commit
fc8206d0e2
4 changed files with 149 additions and 4 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -194,3 +194,39 @@ func TestEnsureUserRejectsUnsafeNames(t *testing.T) {
|
|||
t.Fatalf("EnsureUser(okname): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtraUserKeys(t *testing.T) {
|
||||
st := openTest(t)
|
||||
a, _ := st.EnsureUser("alice", "member", "SHA256:laptop")
|
||||
b, _ := st.EnsureUser("bob", "member", "SHA256:bob")
|
||||
|
||||
if _, err := st.EnsureUser("alice", "member", "SHA256:desktop"); !errors.Is(err, ErrKeyMismatch) {
|
||||
t.Fatalf("unknown key must mismatch, got %v", err)
|
||||
}
|
||||
if err := st.AddUserKey(a.ID, "SHA256:desktop"); err != nil {
|
||||
t.Fatalf("add: %v", err)
|
||||
}
|
||||
if err := st.AddUserKey(a.ID, "SHA256:desktop"); err != nil {
|
||||
t.Fatalf("re-add must be idempotent: %v", err)
|
||||
}
|
||||
if u, err := st.EnsureUser("alice", "member", "SHA256:desktop"); err != nil || u.ID != a.ID {
|
||||
t.Fatalf("extra key must sign in as alice: %v %+v", err, u)
|
||||
}
|
||||
if u, ok, err := st.UserByFingerprint("SHA256:desktop"); err != nil || !ok || u.ID != a.ID {
|
||||
t.Fatalf("lookup by extra key: %v %v %+v", err, ok, u)
|
||||
}
|
||||
if u, ok, _ := st.UserByFingerprint("SHA256:laptop"); !ok || u.ID != a.ID {
|
||||
t.Fatal("primary key must still work")
|
||||
}
|
||||
for fp, want := range map[string]bool{"SHA256:laptop": true, "SHA256:desktop": true, "SHA256:bob": false, "": false} {
|
||||
if got, _ := st.UserHasKey(a.ID, fp); got != want {
|
||||
t.Fatalf("UserHasKey(%q)=%v want %v", fp, got, want)
|
||||
}
|
||||
}
|
||||
if err := st.AddUserKey(a.ID, "SHA256:bob"); !errors.Is(err, ErrKeyInUse) {
|
||||
t.Fatalf("bob's primary key must not attach to alice: %v", err)
|
||||
}
|
||||
if err := st.AddUserKey(b.ID, "SHA256:desktop"); !errors.Is(err, ErrKeyInUse) {
|
||||
t.Fatalf("alice's extra key must not attach to bob: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue