|
| 1 | +/* |
| 2 | +Copyright 2026 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package jwt_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "crypto" |
| 21 | + "crypto/ecdsa" |
| 22 | + "crypto/ed25519" |
| 23 | + "crypto/elliptic" |
| 24 | + "crypto/rand" |
| 25 | + "crypto/rsa" |
| 26 | + "encoding/json" |
| 27 | + "strings" |
| 28 | + "testing" |
| 29 | + "time" |
| 30 | + |
| 31 | + jose "github.com/go-jose/go-jose/v4" |
| 32 | + gojwt "github.com/golang-jwt/jwt/v5" |
| 33 | + |
| 34 | + "github.com/fluxcd/pkg/auth/jwt" |
| 35 | +) |
| 36 | + |
| 37 | +func marshalJWK(t *testing.T, key jose.JSONWebKey) string { |
| 38 | + t.Helper() |
| 39 | + b, err := json.Marshal(key) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("failed to marshal JWK: %v", err) |
| 42 | + } |
| 43 | + return string(b) |
| 44 | +} |
| 45 | + |
| 46 | +func TestParseJWK_Errors(t *testing.T) { |
| 47 | + _, edPriv, err := ed25519.GenerateKey(rand.Reader) |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + rsaPriv, err := rsa.GenerateKey(rand.Reader, 2048) |
| 52 | + if err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + jwk string |
| 59 | + wantErr string |
| 60 | + }{ |
| 61 | + { |
| 62 | + name: "not json", |
| 63 | + jwk: "{not json", |
| 64 | + wantErr: "failed to parse JWK", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "rsa rejected as ambiguous", |
| 68 | + jwk: marshalJWK(t, jose.JSONWebKey{Key: rsaPriv, KeyID: "a", Algorithm: "RS256"}), |
| 69 | + wantErr: "unsupported JWK key type *rsa.PrivateKey", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "public key only", |
| 73 | + jwk: marshalJWK(t, jose.JSONWebKey{Key: edPriv.Public(), KeyID: "a", Algorithm: "EdDSA"}), |
| 74 | + wantErr: "unsupported JWK key type ed25519.PublicKey", |
| 75 | + }, |
| 76 | + } |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + _, err := jwt.ParseJWK(tt.jwk) |
| 80 | + if err == nil || !strings.Contains(err.Error(), tt.wantErr) { |
| 81 | + t.Fatalf("expected error containing %q, got: %v", tt.wantErr, err) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestSigningKey_Issue(t *testing.T) { |
| 88 | + ed25519Pub, ed25519Priv, err := ed25519.GenerateKey(rand.Reader) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + es256 := ecKey(t, elliptic.P256()) |
| 93 | + es384 := ecKey(t, elliptic.P384()) |
| 94 | + es521 := ecKey(t, elliptic.P521()) |
| 95 | + |
| 96 | + tests := []struct { |
| 97 | + name string |
| 98 | + priv crypto.PrivateKey |
| 99 | + pub crypto.PublicKey |
| 100 | + wantAlg string |
| 101 | + }{ |
| 102 | + {"ed25519", ed25519Priv, ed25519Pub, "EdDSA"}, |
| 103 | + {"ecdsa P-256", es256.priv, es256.pub, "ES256"}, |
| 104 | + {"ecdsa P-384", es384.priv, es384.pub, "ES384"}, |
| 105 | + {"ecdsa P-521", es521.priv, es521.pub, "ES512"}, |
| 106 | + } |
| 107 | + for _, tt := range tests { |
| 108 | + t.Run(tt.name, func(t *testing.T) { |
| 109 | + const kid = "my-key" |
| 110 | + jwk := marshalJWK(t, jose.JSONWebKey{Key: tt.priv, KeyID: kid}) |
| 111 | + |
| 112 | + key, err := jwt.ParseJWK(jwk) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("ParseJWK: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + signed, err := key.Issue("https://issuer", "my-subject", "my-audience", 10*time.Second) |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("Issue: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + claims := gojwt.MapClaims{} |
| 123 | + tok, err := gojwt.NewParser().ParseWithClaims(signed, claims, func(*gojwt.Token) (any, error) { |
| 124 | + return tt.pub, nil |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + t.Fatalf("token failed to verify: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + if tok.Method.Alg() != tt.wantAlg { |
| 131 | + t.Errorf("alg = %q, want %q", tok.Method.Alg(), tt.wantAlg) |
| 132 | + } |
| 133 | + if got := tok.Header["kid"]; got != kid { |
| 134 | + t.Errorf("kid header = %v, want %q", got, kid) |
| 135 | + } |
| 136 | + |
| 137 | + if got, _ := claims.GetIssuer(); got != "https://issuer" { |
| 138 | + t.Errorf("iss = %q", got) |
| 139 | + } |
| 140 | + if got, _ := claims.GetSubject(); got != "my-subject" { |
| 141 | + t.Errorf("sub = %q", got) |
| 142 | + } |
| 143 | + if got, _ := claims.GetAudience(); len(got) != 1 || got[0] != "my-audience" { |
| 144 | + t.Errorf("aud = %v", got) |
| 145 | + } |
| 146 | + iat, _ := claims.GetIssuedAt() |
| 147 | + nbf, _ := claims.GetNotBefore() |
| 148 | + exp, _ := claims.GetExpirationTime() |
| 149 | + if iat == nil || nbf == nil || exp == nil { |
| 150 | + t.Fatalf("missing time claims: iat=%v nbf=%v exp=%v", iat, nbf, exp) |
| 151 | + } |
| 152 | + if want := iat.Add(-30 * time.Second); !nbf.Equal(want) { |
| 153 | + t.Errorf("nbf = %s, want %s (iat backdated 30s)", nbf, want) |
| 154 | + } |
| 155 | + if d := exp.Sub(iat.Time); d != 10*time.Second { |
| 156 | + t.Errorf("lifetime = %s, want 10s", d) |
| 157 | + } |
| 158 | + if jti, ok := claims["jti"].(string); !ok || jti == "" { |
| 159 | + t.Errorf("jti = %v, want non-empty string", claims["jti"]) |
| 160 | + } |
| 161 | + }) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestSigningKey_Issue_FreshJTIPerCall(t *testing.T) { |
| 166 | + _, priv, err := ed25519.GenerateKey(rand.Reader) |
| 167 | + if err != nil { |
| 168 | + t.Fatal(err) |
| 169 | + } |
| 170 | + jwk := marshalJWK(t, jose.JSONWebKey{Key: priv, KeyID: "k"}) |
| 171 | + key, err := jwt.ParseJWK(jwk) |
| 172 | + if err != nil { |
| 173 | + t.Fatalf("ParseJWK: %v", err) |
| 174 | + } |
| 175 | + |
| 176 | + seen := make(map[string]bool) |
| 177 | + for range 10 { |
| 178 | + signed, err := key.Issue("iss", "sub", "aud", time.Second) |
| 179 | + if err != nil { |
| 180 | + t.Fatalf("Issue: %v", err) |
| 181 | + } |
| 182 | + claims := gojwt.MapClaims{} |
| 183 | + if _, _, err := gojwt.NewParser().ParseUnverified(signed, claims); err != nil { |
| 184 | + t.Fatalf("ParseUnverified: %v", err) |
| 185 | + } |
| 186 | + jti, _ := claims["jti"].(string) |
| 187 | + if seen[jti] { |
| 188 | + t.Fatalf("jti %q reused", jti) |
| 189 | + } |
| 190 | + seen[jti] = true |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +type ecPair struct { |
| 195 | + priv *ecdsa.PrivateKey |
| 196 | + pub *ecdsa.PublicKey |
| 197 | +} |
| 198 | + |
| 199 | +// ecKey generates a deterministic-per-call ECDSA key pair on the given curve. |
| 200 | +func ecKey(t *testing.T, curve elliptic.Curve) ecPair { |
| 201 | + t.Helper() |
| 202 | + priv, err := ecdsa.GenerateKey(curve, rand.Reader) |
| 203 | + if err != nil { |
| 204 | + t.Fatalf("failed to generate ECDSA key: %v", err) |
| 205 | + } |
| 206 | + return ecPair{priv: priv, pub: &priv.PublicKey} |
| 207 | +} |
0 commit comments