oficina / agent-access Public
Bind delegated credentials and persisted grants to the Home identity namespace
10 files changed,
+163
−18
README.md+7 −0connection/server.go+1 −1connection/server_test.go+1 −1grant/identity.go+25 −0grant/identity_test.go+60 −0grant/server.go+17 −8grant/server_test.go+2 −2grant/store.go+1 −0remote.go+9 −6remote_test.go+40 −0
CI passed — run details
diff --git a/README.md b/README.md| index d4c1339..2224686 100644 |
| --- a/README.md |
| +++ b/README.md |
| @@ -137,3 +137,10 @@ credentials), callback, consent scopes/teams, and results. Required exercises: |
| allow and deny; missing scope; wrong team; create and same-key retry; explicit |
| invitation-link permission where applicable; refresh; revoke in `/connections`; |
| then repeat a call in the already open host session and require refusal. |
| + |
| +Credential introspection includes `identity_issuer`, the fixed Home identity |
| +namespace, distinct from the connection service issuer. Each app must supply its |
| +configured Home issuer to `NewRemoteVerifier`; subjects are released only after |
| +an exact match. A grant database binds that Home issuer persistently and refuses |
| +a changed issuer or an unbound database already containing grants. Changing the |
| +Home authority requires an explicit migration, not a configuration-only restart. |
diff --git a/connection/server.go b/connection/server.go| index 0e6a2fb..d262886 100644 |
| --- a/connection/server.go |
| +++ b/connection/server.go |
| @@ -131,7 +131,7 @@ func New(ctx context.Context, cfg Config, d *db.DB, logger *slog.Logger) (*Serve |
| if err != nil { |
| return nil, err |
| } |
| - grants, err := grant.NewServer(grant.Config{Origin: cfg.Origin, Resource: cfg.Origin + "/mcp", Store: store, ConsentKey: consentKey, Clients: clients, Scopes: scopes, Subject: fresh, Teams: provider.teams, ResourceTeams: provider.resourceTeams, SignInPath: handoff.StartPath}) |
| + grants, err := grant.NewServer(grant.Config{IdentityIssuer: cfg.HomeIssuer, Origin: cfg.Origin, Resource: cfg.Origin + "/mcp", Store: store, ConsentKey: consentKey, Clients: clients, Scopes: scopes, Subject: fresh, Teams: provider.teams, ResourceTeams: provider.resourceTeams, SignInPath: handoff.StartPath}) |
| if err != nil { |
| return nil, err |
| } |
diff --git a/connection/server_test.go b/connection/server_test.go| index c56c19d..108cdfc 100644 |
| --- a/connection/server_test.go |
| +++ b/connection/server_test.go |
| @@ -252,7 +252,7 @@ func TestHomeBrowserConsentToHTTPMCPAndLogout(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - remote, err := access.NewRemoteVerifier(serviceOrigin, app.URL+"/api/v1") |
| + remote, err := access.NewRemoteVerifier(serviceOrigin, app.URL+"/api/v1", home) |
| if err != nil { |
| t.Fatal(err) |
| } |
diff --git a/grant/identity.go b/grant/identity.go| new file mode 100644 |
| index 0000000..3a27c29 |
| --- /dev/null |
| +++ b/grant/identity.go |
| @@ -0,0 +1,25 @@ |
| +package grant |
| + |
| +import ( |
| + "context" |
| + "errors" |
| +) |
| + |
| +// A database's opaque subjects belong to one Home namespace for its lifetime. |
| +// Binding at startup prevents a config change from relabelling live grants. |
| +func (s *Store) bindIdentityIssuer(ctx context.Context, issuer string) error { |
| + tx, err := s.DB.BeginTx(ctx, nil) |
| + if err != nil { |
| + return err |
| + } |
| + defer tx.Rollback() |
| + // First statement reserves the writer before inspecting any existing grants. |
| + if _, err = tx.ExecContext(ctx, `INSERT INTO agent_identity_binding(id,issuer) SELECT 1,? WHERE NOT EXISTS (SELECT 1 FROM agent_grants) ON CONFLICT(id) DO NOTHING`, issuer); err != nil { |
| + return err |
| + } |
| + var bound string |
| + if err = tx.QueryRowContext(ctx, `SELECT issuer FROM agent_identity_binding WHERE id=1`).Scan(&bound); err != nil || bound != issuer { |
| + return errors.New("grant database identity namespace differs or is unbound; explicit migration required") |
| + } |
| + return tx.Commit() |
| +} |
diff --git a/grant/identity_test.go b/grant/identity_test.go| new file mode 100644 |
| index 0000000..164d411 |
| --- /dev/null |
| +++ b/grant/identity_test.go |
| @@ -0,0 +1,60 @@ |
| +package grant |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| + "testing" |
| +) |
| + |
| +func TestIdentityBindingSurvivesRestart(t *testing.T) { |
| + server, _, _ := setup(t) |
| + ctx := context.Background() |
| + if _, err := server.cfg.Store.CreateCode(ctx, Grant{Subject: "same-opaque-id", ClientID: "client", Teams: []string{"acme"}, Scopes: []string{"notes:write"}}, Code{Resource: server.cfg.Resource}); err != nil { |
| + t.Fatal(err) |
| + } |
| + var sequence int |
| + var name, path string |
| + if err := server.cfg.Store.DB.QueryRowContext(ctx, "PRAGMA database_list").Scan(&sequence, &name, &path); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := server.cfg.Store.DB.Close(); err != nil { |
| + t.Fatal(err) |
| + } |
| + reopenedDB, err := sql.Open("sqlite", path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { reopenedDB.Close() }) |
| + reopened, err := NewStore(ctx, reopenedDB) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + cfg := server.cfg |
| + cfg.Store = reopened |
| + if _, err = NewServer(cfg); err != nil { |
| + t.Fatal("same Home restart", err) |
| + } |
| + cfg.IdentityIssuer = "https://another-home.example" |
| + if _, err = NewServer(cfg); err == nil { |
| + t.Fatal("live grants relabelled under another Home") |
| + } |
| + cfg.IdentityIssuer = server.cfg.IdentityIssuer |
| + // An older database without provenance cannot acquire a guessed namespace. |
| + if _, err = reopened.DB.ExecContext(ctx, `DELETE FROM agent_identity_binding`); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err = NewServer(cfg); err == nil { |
| + t.Fatal("unbound existing grants relabelled") |
| + } |
| +} |
| + |
| +func TestIdentityOriginConfiguration(t *testing.T) { |
| + server, _, _ := setup(t) |
| + for _, issuer := range []string{"", server.cfg.Origin, "http://home.example", "https://home.example?", "https://home.example#", "https://home.example/path"} { |
| + cfg := server.cfg |
| + cfg.IdentityIssuer = issuer |
| + if _, err := NewServer(cfg); err == nil { |
| + t.Fatal("invalid Home origin accepted", issuer) |
| + } |
| + } |
| +} |
diff --git a/grant/server.go b/grant/server.go| index f582db3..17e620c 100644 |
| --- a/grant/server.go |
| +++ b/grant/server.go |
| @@ -31,12 +31,14 @@ type Team struct { |
| Name string `json:"name"` |
| } |
| type Config struct { |
| - Origin string |
| - Resource string |
| - Store *Store |
| - ConsentKey []byte |
| - Clients []Client |
| - Scopes map[string]string |
| + // IdentityIssuer is the fixed Home namespace; it is not the grant Origin. |
| + IdentityIssuer string |
| + Origin string |
| + Resource string |
| + Store *Store |
| + ConsentKey []byte |
| + Clients []Client |
| + Scopes map[string]string |
| // Subject reads a verified browser session. Empty means sign-in is needed. |
| Subject func(*http.Request) string |
| // Teams reads CURRENT memberships, including on token verification. |
| @@ -60,9 +62,13 @@ type ticket struct { |
| func NewServer(cfg Config) (*Server, error) { |
| u, err := url.Parse(cfg.Origin) |
| - if err != nil || u.Scheme != "https" || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || u.Path != "" { |
| + if err != nil || u.Scheme != "https" || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || u.Path != "" || cfg.Origin != "https://"+u.Host { |
| return nil, errors.New("grant issuer must be an HTTPS origin") |
| } |
| + identity, identityErr := url.Parse(cfg.IdentityIssuer) |
| + if identityErr != nil || identity.Scheme != "https" || identity.Host == "" || identity.User != nil || identity.Path != "" || identity.RawQuery != "" || identity.Fragment != "" || cfg.IdentityIssuer != "https://"+identity.Host || cfg.IdentityIssuer == cfg.Origin { |
| + return nil, errors.New("Home identity issuer must be an HTTPS origin") |
| + } |
| if cfg.Resource != cfg.Origin+"/mcp" || cfg.Store == nil || cfg.Subject == nil || cfg.Teams == nil || len(cfg.Scopes) == 0 || !strings.HasPrefix(cfg.SignInPath, "/") || strings.HasPrefix(cfg.SignInPath, "//") { |
| return nil, errors.New("incomplete grant configuration") |
| } |
| @@ -98,6 +104,9 @@ func NewServer(cfg Config) (*Server, error) { |
| if _, err = cfg.Store.DB.Exec(`CREATE TABLE IF NOT EXISTS agent_pending(hash TEXT PRIMARY KEY, expires INTEGER NOT NULL)`); err != nil { |
| return nil, err |
| } |
| + if err := cfg.Store.bindIdentityIssuer(context.Background(), cfg.IdentityIssuer); err != nil { |
| + return nil, err |
| + } |
| return s, nil |
| } |
| func validRedirect(raw string) bool { |
| @@ -492,5 +501,5 @@ func (s *Server) credential(w http.ResponseWriter, r *http.Request) { |
| fail(w, 401, "invalid_token") |
| return |
| } |
| - write(w, 200, map[string]any{"resource": in.Resource, "principal": p}) |
| + write(w, 200, map[string]any{"resource": in.Resource, "identity_issuer": s.cfg.IdentityIssuer, "principal": p}) |
| } |
diff --git a/grant/server_test.go b/grant/server_test.go| index 620c989..34c33af 100644 |
| --- a/grant/server_test.go |
| +++ b/grant/server_test.go |
| @@ -40,7 +40,7 @@ func setup(t *testing.T) (*Server, *httptest.Server, *bool) { |
| var handler http.Handler |
| ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler.ServeHTTP(w, r) })) |
| t.Cleanup(ts.Close) |
| - s, err := NewServer(Config{Origin: ts.URL, Resource: ts.URL + "/mcp", Store: store, ConsentKey: []byte(strings.Repeat("k", 32)), Clients: []Client{{ID: client.ClientID, Name: "Oficina CLI", Loopback: true}}, Scopes: map[string]string{"notes:write": "Create and edit notes", "offline_access": "Keep this connection"}, Subject: func(r *http.Request) string { |
| + s, err := NewServer(Config{IdentityIssuer: "https://home.example", Origin: ts.URL, Resource: ts.URL + "/mcp", Store: store, ConsentKey: []byte(strings.Repeat("k", 32)), Clients: []Client{{ID: client.ClientID, Name: "Oficina CLI", Loopback: true}}, Scopes: map[string]string{"notes:write": "Create and edit notes", "offline_access": "Keep this connection"}, Subject: func(r *http.Request) string { |
| if r.Header.Get("Test-Session") == "signed-in" { |
| return "opaque-person" |
| } |
| @@ -135,7 +135,7 @@ func TestBrowserLoginRefreshReplayAndRevocation(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - remote, err := access.NewRemoteVerifier(ts.URL, "https://notes.example/api/v1") |
| + remote, err := access.NewRemoteVerifier(ts.URL, "https://notes.example/api/v1", "https://home.example") |
| if err != nil { |
| t.Fatal(err) |
| } |
diff --git a/grant/store.go b/grant/store.go| index 4383969..94c6d63 100644 |
| --- a/grant/store.go |
| +++ b/grant/store.go |
| @@ -19,6 +19,7 @@ import ( |
| ) |
| const schema = ` |
| +CREATE TABLE IF NOT EXISTS agent_identity_binding (id INTEGER PRIMARY KEY CHECK(id=1), issuer TEXT NOT NULL); |
| CREATE TABLE IF NOT EXISTS agent_grants ( |
| id TEXT PRIMARY KEY, subject TEXT NOT NULL, client_id TEXT NOT NULL, |
| teams TEXT NOT NULL, scopes TEXT NOT NULL, created INTEGER NOT NULL, |
diff --git a/remote.go b/remote.go| index 476cfcb..0a4190f 100644 |
| --- a/remote.go |
| +++ b/remote.go |
| @@ -14,16 +14,18 @@ import ( |
| // on the next request. Resolve the returned Home subject locally before use. |
| type RemoteVerifier struct { |
| Endpoint, Audience string |
| + IdentityIssuer string |
| Client *http.Client |
| } |
| -func NewRemoteVerifier(issuer, audience string) (*RemoteVerifier, error) { |
| +func NewRemoteVerifier(issuer, audience, identityIssuer string) (*RemoteVerifier, error) { |
| u, err := url.Parse(issuer) |
| a, ae := url.Parse(audience) |
| - if err != nil || ae != nil || u.Scheme != "https" || u.Host == "" || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" || a.Scheme != "https" || a.Host == "" || a.User != nil || a.RawQuery != "" || a.Fragment != "" { |
| + i, ie := url.Parse(identityIssuer) |
| + if ie != nil || i.Scheme != "https" || i.Host == "" || i.User != nil || i.Path != "" || i.RawQuery != "" || i.Fragment != "" || identityIssuer != "https://"+i.Host || identityIssuer == issuer || err != nil || ae != nil || u.Scheme != "https" || u.Host == "" || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" || issuer != "https://"+u.Host || a.Scheme != "https" || a.Host == "" || a.User != nil || a.RawQuery != "" || a.Fragment != "" { |
| return nil, Fail("invalid", "Use trusted HTTPS issuer and app resource URLs.") |
| } |
| - return &RemoteVerifier{Endpoint: issuer + "/api/v1/credential", Audience: audience, Client: &http.Client{Timeout: 10 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}, nil |
| + return &RemoteVerifier{Endpoint: issuer + "/api/v1/credential", Audience: audience, IdentityIssuer: identityIssuer, Client: &http.Client{Timeout: 10 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}, nil |
| } |
| func (v *RemoteVerifier) Verify(ctx context.Context, token string) (Principal, error) { |
| raw, _ := json.Marshal(map[string]string{"resource": v.Audience}) |
| @@ -49,10 +51,11 @@ func (v *RemoteVerifier) Verify(ctx context.Context, token string) (Principal, e |
| return Principal{}, Fail("unavailable", "Authorization is unavailable.") |
| } |
| var out struct { |
| - Resource string `json:"resource"` |
| - Principal Principal `json:"principal"` |
| + Resource string `json:"resource"` |
| + IdentityIssuer string `json:"identity_issuer"` |
| + Principal Principal `json:"principal"` |
| } |
| - if json.Unmarshal(body, &out) != nil || out.Resource != v.Audience || out.Principal.Subject == "" || out.Principal.Grant == "" { |
| + if UniqueJSON(body) != nil || json.Unmarshal(body, &out) != nil || v.IdentityIssuer == "" || out.IdentityIssuer != v.IdentityIssuer || out.Resource != v.Audience || out.Principal.Subject == "" || out.Principal.Grant == "" { |
| return Principal{}, Fail("unavailable", "Authorization is unavailable.") |
| } |
| return out.Principal, nil |
diff --git a/remote_test.go b/remote_test.go| new file mode 100644 |
| index 0000000..aa21d41 |
| --- /dev/null |
| +++ b/remote_test.go |
| @@ -0,0 +1,40 @@ |
| +package agentaccess |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "net/http" |
| + "net/http/httptest" |
| + "testing" |
| +) |
| + |
| +func TestRemoteIdentityNamespace(t *testing.T) { |
| + const home = "https://home.example" |
| + const audience = "https://docs.example/api/v1" |
| + for _, identity := range []string{home, "", "https://another-home.example", "https://connect.example"} { |
| + t.Run(identity, func(t *testing.T) { |
| + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + json.NewEncoder(w).Encode(map[string]any{"resource": audience, "identity_issuer": identity, "principal": Principal{Subject: "same-opaque-id", Grant: "grant", Client: "client", Teams: []string{"team"}, Scopes: []string{"docs:read"}}}) |
| + })) |
| + defer server.Close() |
| + verifier, err := NewRemoteVerifier(server.URL, audience, home) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + verifier.Client = server.Client() |
| + principal, err := verifier.Verify(context.Background(), "opaque-token") |
| + if identity == home { |
| + if err != nil || principal.Subject != "same-opaque-id" { |
| + t.Fatal(principal, err) |
| + } |
| + } else if err == nil || principal.Subject != "" { |
| + t.Fatal("foreign identity namespace accepted", principal) |
| + } |
| + }) |
| + } |
| + for _, identity := range []string{"", "http://home.example", "https://home.example/path", "https://user@home.example", "https://home.example?x=1", "https://home.example?", "https://home.example#", "https://connect.example"} { |
| + if _, err := NewRemoteVerifier("https://connect.example", audience, identity); err == nil { |
| + t.Fatal("invalid identity origin accepted", identity) |
| + } |
| + } |
| +} |