Skip to content

Agent Mesh

test(identity): add unit tests for VerifyJWT

Antonio Ojea9c968ffe4dc5
diff --git a/internal/identity/oidc_test.go b/internal/identity/oidc_test.go
new file mode 100644
index 00000000..699ebe75
--- /dev/null
+++ b/internal/identity/oidc_test.go
@@ -0,0 +1,185 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package identity
+
+import (
+ "context"
+ "crypto/rand"
+ "crypto/rsa"
+ "encoding/base64"
+ "encoding/json"
+ "math/big"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+
+ "github.com/coreos/go-oidc/v3/oidc"
+ "github.com/golang-jwt/jwt/v5"
+)
+
+const testKID = "mock-key"
+
+// newMockOIDCIssuer starts an in-process OIDC discovery/JWKS server backed by
+// the returned RSA key, so tests can mint their own tokens with arbitrary
+// (including invalid) claims.
+func newMockOIDCIssuer(t *testing.T) (issuer string, key *rsa.PrivateKey) {
+ t.Helper()
+ privKey, err := rsa.GenerateKey(rand.Reader, 2048)
+ if err != nil {
+ t.Fatalf("failed to generate rsa key: %v", err)
+ }
+
+ mux := http.NewServeMux()
+ srv := httptest.NewServer(mux)
+ t.Cleanup(srv.Close)
+ issuer = srv.URL
+
+ mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "issuer": issuer,
+ "jwks_uri": issuer + "/keys",
+ })
+ })
+ mux.HandleFunc("/keys", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "keys": []map[string]any{
+ {
+ "kty": "RSA",
+ "alg": "RS256",
+ "use": "sig",
+ "kid": testKID,
+ "n": base64.RawURLEncoding.EncodeToString(privKey.N.Bytes()),
+ "e": base64.RawURLEncoding.EncodeToString(big.NewInt(int64(privKey.E)).Bytes()),
+ },
+ },
+ })
+ })
+
+ return issuer, privKey
+}
+
+func signToken(t *testing.T, key *rsa.PrivateKey, kid string, claims jwt.MapClaims) string {
+ t.Helper()
+ token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
+ token.Header["kid"] = kid
+ str, err := token.SignedString(key)
+ if err != nil {
+ t.Fatalf("failed to sign token: %v", err)
+ }
+ return str
+}
+
+func TestVerifyJWT(t *testing.T) {
+ issuer, key := newMockOIDCIssuer(t)
+ ctx := context.Background()
+ provider, err := oidc.NewProvider(ctx, issuer)
+ if err != nil {
+ t.Fatalf("failed to discover mock provider: %v", err)
+ }
+ providers := map[string]*oidc.Provider{issuer: provider}
+ allowedAudiences := []string{"sam-mesh-audience"}
+
+ validClaims := func() jwt.MapClaims {
+ return jwt.MapClaims{
+ "iss": issuer,
+ "aud": "sam-mesh-audience",
+ "sub": "user-1",
+ "exp": time.Now().Add(time.Hour).Unix(),
+ }
+ }
+
+ t.Run("valid token succeeds", func(t *testing.T) {
+ tokenStr := signToken(t, key, testKID, validClaims())
+ claims, idToken, err := VerifyJWT(ctx, tokenStr, allowedAudiences, providers)
+ if err != nil {
+ t.Fatalf("expected success, got error: %v", err)
+ }
+ if idToken == nil {
+ t.Fatal("expected non-nil ID token")
+ }
+ if claims["sub"] != "user-1" {
+ t.Fatalf("unexpected sub claim: %v", claims["sub"])
+ }
+ })
+
+ t.Run("malformed token is rejected", func(t *testing.T) {
+ _, _, err := VerifyJWT(ctx, "not-a-valid-jwt", allowedAudiences, providers)
+ if err == nil {
+ t.Fatal("expected error for malformed token")
+ }
+ })
+
+ t.Run("alg=none downgrade is rejected", func(t *testing.T) {
+ header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"none","typ":"JWT"}`))
+ payload := base64.RawURLEncoding.EncodeToString([]byte(`{"iss":"` + issuer + `","aud":"sam-mesh-audience"}`))
+ tokenStr := header + "." + payload + "."
+
+ _, _, err := VerifyJWT(ctx, tokenStr, allowedAudiences, providers)
+ if err == nil {
+ t.Fatal("expected error for alg=none token")
+ }
+ })
+
+ t.Run("missing aud claim is rejected", func(t *testing.T) {
+ claims := validClaims()
+ delete(claims, "aud")
+ tokenStr := signToken(t, key, testKID, claims)
+
+ _, _, err := VerifyJWT(ctx, tokenStr, allowedAudiences, providers)
+ if err == nil {
+ t.Fatal("expected error for missing aud claim")
+ }
+ })
+
+ t.Run("untrusted audience is rejected", func(t *testing.T) {
+ claims := validClaims()
+ claims["aud"] = "some-other-audience"
+ tokenStr := signToken(t, key, testKID, claims)
+
+ _, _, err := VerifyJWT(ctx, tokenStr, allowedAudiences, providers)
+ if err == nil {
+ t.Fatal("expected error for untrusted audience")
+ }
+ })
+
+ t.Run("unknown issuer is rejected", func(t *testing.T) {
+ claims := validClaims()
+ claims["iss"] = "https://unknown-issuer.example"
+ tokenStr := signToken(t, key, testKID, claims)
+
+ _, _, err := VerifyJWT(ctx, tokenStr, allowedAudiences, providers)
+ if err == nil {
+ t.Fatal("expected error for unknown issuer")
+ }
+ })
+
+ t.Run("bad signature is rejected", func(t *testing.T) {
+ otherKey, err := rsa.GenerateKey(rand.Reader, 2048)
+ if err != nil {
+ t.Fatalf("failed to generate rsa key: %v", err)
+ }
+ // Sign with a key that does not match the one published in the JWKS
+ // for this kid, so signature verification must fail.
+ tokenStr := signToken(t, otherKey, testKID, validClaims())
+
+ _, _, err = VerifyJWT(ctx, tokenStr, allowedAudiences, providers)
+ if err == nil {
+ t.Fatal("expected error for bad signature")
+ }
+ })
+}