Files
archie/internal/infrastructure/egress/oauth_scope_test.go
T
sam 21a5893040 feat(egress): capture the OAuth scope set and carry it to the harness secret
A Kit's credentialFile cannot render {{.Scopes}} because nothing records the
granted scopes: the token response's `scope` field (RFC 6749 section 5.1,
space-delimited) was never read, and the token set had nowhere to hold it.

capture now parses `scope` into []string and keeps the previous set when the
field is absent or empty -- the same rule the refresh token follows, so a
refresh that does not repeat the scopes preserves what the login captured.

The set is carried end to end: Scopes on harnesssecret.Secret, in
harnessSecretPayload (JSON inside the existing encrypted envelope, so no
migration), on message HarnessSecret as `repeated string scopes = 8`, and
both ways through staterpc values.go. The proto field is load-bearing: the
daemon reads the token set over gRPC, so a local-struct-only field is
silently lost in production -- TestHarnessSecretConformance proves the round
trip in both its local and grpc modes.
2026-09-30 07:52:13 +10:00

64 lines
2.2 KiB
Go

package egress
import (
"net/http"
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
)
// The provider's granted scopes are captured with the token set (RFC 6749
// section 5.1 spells them as one space-delimited string), so a Kit's
// credential file can render them.
func TestOAuthCaptureRecordsScopes(t *testing.T) {
h := newHarness(t)
h.token.response = `{"access_token":"new-access","refresh_token":"new-refresh","expires_in":3600,"scope":"user:inference user:profile"}`
_ = h.oauth.PutHarnessSecret(t.Context(), harnesssecret.Secret{
Org: "org-1", Service: "claude-code",
AccessToken: "old-access", RefreshToken: "real-refresh",
Scopes: []string{"user:inference"},
})
s := h.oauthSession(t, true, true)
status, _ := postToken(t, h, s, "grant_type=refresh_token&refresh_token="+sentinelRefresh)
if status != http.StatusOK {
t.Fatalf("status = %d, want 200", status)
}
stored, err := h.oauth.GetHarnessSecret(t.Context(), "org-1", "claude-code")
if err != nil {
t.Fatal(err)
}
if want := []string{"user:inference", "user:profile"}; !slices.Equal(stored.Scopes, want) {
t.Fatalf("stored scopes = %v, want the granted set %v", stored.Scopes, want)
}
}
// A refresh that does not repeat the scope keeps the set the login captured:
// a provider may return scopes only on the initial exchange.
func TestOAuthCaptureKeepsScopesWhenTheResponseOmitsThem(t *testing.T) {
h := newHarness(t)
h.token.response = `{"access_token":"new-access","refresh_token":"new-refresh","expires_in":3600}`
want := []string{"user:inference", "user:profile"}
_ = h.oauth.PutHarnessSecret(t.Context(), harnesssecret.Secret{
Org: "org-1", Service: "claude-code",
AccessToken: "old-access", RefreshToken: "real-refresh",
Scopes: want,
})
s := h.oauthSession(t, true, true)
status, _ := postToken(t, h, s, "grant_type=refresh_token&refresh_token="+sentinelRefresh)
if status != http.StatusOK {
t.Fatalf("status = %d, want 200", status)
}
stored, err := h.oauth.GetHarnessSecret(t.Context(), "org-1", "claude-code")
if err != nil {
t.Fatal(err)
}
if !slices.Equal(stored.Scopes, want) {
t.Fatalf("stored scopes = %v, want the set the login captured %v", stored.Scopes, want)
}
}