Agents are users of kind agent, each given modules to read, or to read and act on, and the CRM scope. Admins make them under Settings → Agents, on every edition; keys (sm_…) are shown once and kept as hashes, can expire, be rotated (the old one works a day) or revoked, and record their last use. /mcp takes agent keys and offers each agent only the tools its scopes allow. On the local edition, mcp-token is the key of a built-in Hermes agent with the CRM and the Market. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.4 KiB
Go
108 lines
3.4 KiB
Go
package users
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAgentKeys(t *testing.T) {
|
|
d := openDB(t)
|
|
now := time.Date(2026, 11, 26, 10, 0, 0, 0, time.UTC)
|
|
a, err := CreateAgent(d, "Morning writer", "Drafts posts from What to write next.", now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.Kind != Agent || a.Status != Active || !strings.HasSuffix(a.Email, "@agents.invalid") || !strings.HasPrefix(a.Email, "morning-writer-") {
|
|
t.Errorf("agent %+v", a)
|
|
}
|
|
if _, err := CreateAgent(d, " ", "", now); err == nil {
|
|
t.Error("no name accepted")
|
|
}
|
|
// Agents never sign in with a password.
|
|
if CheckPassword(a, "") {
|
|
t.Error("password")
|
|
}
|
|
|
|
key, k, err := NewKey(d, a.ID, now.Add(30*24*time.Hour), now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasPrefix(key, "sm_") || len(key) != 43 || k.Prefix != key[:10] {
|
|
t.Errorf("key %q, prefix %q", key, k.Prefix)
|
|
}
|
|
u, got, err := Authenticate(d, key, "203.0.113.7", now)
|
|
if err != nil || u.ID != a.ID || got.ID != k.ID {
|
|
t.Fatalf("auth: %v", err)
|
|
}
|
|
keys, _ := Keys(d, a.ID)
|
|
if keys[0].UsedFrom != "203.0.113.7" || !keys[0].Used.Equal(now) {
|
|
t.Errorf("use not noted: %+v", keys[0])
|
|
}
|
|
for _, bad := range []string{"", "sm_nope", key + "x", strings.ToUpper(key)} {
|
|
if _, _, err := Authenticate(d, bad, "", now); !errors.Is(err, ErrBadKey) {
|
|
t.Errorf("%q: %v", bad, err)
|
|
}
|
|
}
|
|
// Expiry.
|
|
if _, _, err := Authenticate(d, key, "", now.Add(31*24*time.Hour)); !errors.Is(err, ErrBadKey) {
|
|
t.Error("expired key works")
|
|
}
|
|
|
|
// Rotation: the old key works for a day; the new one keeps the expiry.
|
|
key2, k2, err := Rotate(d, a.ID, now)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !k2.Expires.Equal(now.Add(30 * 24 * time.Hour)) {
|
|
t.Errorf("new key's expiry %v", k2.Expires)
|
|
}
|
|
if _, _, err := Authenticate(d, key, "", now.Add(23*time.Hour)); err != nil {
|
|
t.Error("old key stopped within the day")
|
|
}
|
|
if _, _, err := Authenticate(d, key, "", now.Add(RotationGrace)); !errors.Is(err, ErrBadKey) {
|
|
t.Error("old key works after a day")
|
|
}
|
|
if _, _, err := Authenticate(d, key2, "", now.Add(RotationGrace)); err != nil {
|
|
t.Error("new key doesn't work")
|
|
}
|
|
|
|
// Revoking stops it at once; turning the agent off stops every key.
|
|
Revoke(d, a.ID, k2.ID, now)
|
|
if _, _, err := Authenticate(d, key2, "", now); !errors.Is(err, ErrBadKey) {
|
|
t.Error("revoked key works")
|
|
}
|
|
key3, _, _ := NewKey(d, a.ID, time.Time{}, now)
|
|
SetStatus(d, a.ID, Disabled)
|
|
if _, _, err := Authenticate(d, key3, "", now); !errors.Is(err, ErrBadKey) {
|
|
t.Error("a turned-off agent's key works")
|
|
}
|
|
SetStatus(d, a.ID, Active)
|
|
RevokeAll(d, a.ID, now)
|
|
if _, _, err := Authenticate(d, key3, "", now); !errors.Is(err, ErrBadKey) {
|
|
t.Error("revoke all")
|
|
}
|
|
|
|
// A person's "key" never authenticates, even if one were stored.
|
|
p := &User{Email: "pat@example.com", Status: Active}
|
|
Create(d, p, now)
|
|
AddKey(d, p.ID, "sm_person_key_0123456789012345678901234", time.Time{}, now)
|
|
if _, _, err := Authenticate(d, "sm_person_key_0123456789012345678901234", "", now); !errors.Is(err, ErrBadKey) {
|
|
t.Error("a person's key works")
|
|
}
|
|
|
|
// A legacy token (64 hex digits) is a key too.
|
|
legacy := strings.Repeat("ab", 32)
|
|
AddKey(d, a.ID, legacy, time.Time{}, now)
|
|
if _, _, err := Authenticate(d, legacy, "", now); err != nil {
|
|
t.Errorf("legacy token: %v", err)
|
|
}
|
|
if err := Delete(d, a.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ks, _ := Keys(d, a.ID); len(ks) != 0 {
|
|
t.Error("keys outlived the agent")
|
|
}
|
|
}
|