| 1 | package agentaccess |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestRemoteIdentityNamespace(t *testing.T) { |
| 12 | const home = "https://home.example" |
| 13 | const audience = "https://docs.example/api/v1" |
| 14 | for _, identity := range []string{home, "", "https://another-home.example", "https://connect.example"} { |
| 15 | t.Run(identity, func(t *testing.T) { |
| 16 | server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | 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"}}}) |
| 18 | })) |
| 19 | defer server.Close() |
| 20 | verifier, err := NewRemoteVerifier(server.URL, audience, home) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | verifier.Client = server.Client() |
| 25 | principal, err := verifier.Verify(context.Background(), "opaque-token") |
| 26 | if identity == home { |
| 27 | if err != nil || principal.Subject != "same-opaque-id" { |
| 28 | t.Fatal(principal, err) |
| 29 | } |
| 30 | } else if err == nil || principal.Subject != "" { |
| 31 | t.Fatal("foreign identity namespace accepted", principal) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | 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"} { |
| 36 | if _, err := NewRemoteVerifier("https://connect.example", audience, identity); err == nil { |
| 37 | t.Fatal("invalid identity origin accepted", identity) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |