|
| 1 | +package crypto_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/golang-jwt/jwt/v4" |
| 10 | + |
| 11 | + "instant.dev/common/crypto" |
| 12 | +) |
| 13 | + |
| 14 | +var jwtSecret = []byte("supersecret-test-key-32-byte-minimum-required-here-pad-zzzzzz") |
| 15 | + |
| 16 | +func TestSignAndVerifyJWT_Roundtrip(t *testing.T) { |
| 17 | + claims := crypto.InstantClaims{ |
| 18 | + Fingerprint: "fp1", |
| 19 | + Country: "US", |
| 20 | + CloudVendor: "aws", |
| 21 | + Tokens: []string{"tok1"}, |
| 22 | + ResourceTypes: []string{"postgres"}, |
| 23 | + SuggestedPlan: "hobby", |
| 24 | + } |
| 25 | + signed, err := crypto.SignJWT(jwtSecret, claims) |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("SignJWT: %v", err) |
| 28 | + } |
| 29 | + if signed == "" { |
| 30 | + t.Fatal("expected signed token") |
| 31 | + } |
| 32 | + |
| 33 | + parsed, err := crypto.VerifyJWT(jwtSecret, signed) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("VerifyJWT: %v", err) |
| 36 | + } |
| 37 | + if parsed.Fingerprint != "fp1" || parsed.Country != "US" { |
| 38 | + t.Errorf("parsed = %+v", parsed) |
| 39 | + } |
| 40 | + if parsed.ID == "" { |
| 41 | + t.Error("expected auto-generated jti") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestVerifyJWT_BadSecret(t *testing.T) { |
| 46 | + signed, _ := crypto.SignJWT(jwtSecret, crypto.InstantClaims{Fingerprint: "x"}) |
| 47 | + _, err := crypto.VerifyJWT([]byte("wrong-secret"), signed) |
| 48 | + if err == nil { |
| 49 | + t.Fatal("expected error from wrong secret") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestVerifyJWT_MalformedToken(t *testing.T) { |
| 54 | + _, err := crypto.VerifyJWT(jwtSecret, "not-a-jwt") |
| 55 | + if err == nil { |
| 56 | + t.Fatal("expected error for malformed token") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestVerifyJWT_FutureIssuedAt(t *testing.T) { |
| 61 | + claims := crypto.InstantClaims{Fingerprint: "fp"} |
| 62 | + claims.IssuedAt = jwt.NewNumericDate(time.Now().UTC().Add(2 * time.Hour)) |
| 63 | + signed, err := crypto.SignJWT(jwtSecret, claims) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("SignJWT: %v", err) |
| 66 | + } |
| 67 | + _, err = crypto.VerifyJWT(jwtSecret, signed) |
| 68 | + if err == nil { |
| 69 | + t.Fatal("expected error for future iat") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestSignOnboardingJWT_Roundtrip(t *testing.T) { |
| 74 | + claims := crypto.OnboardingClaims{ |
| 75 | + Fingerprint: "fp", |
| 76 | + Tokens: []string{"a", "b"}, |
| 77 | + SuggestedPlan: "pro", |
| 78 | + } |
| 79 | + signed, jti, err := crypto.SignOnboardingJWT(jwtSecret, claims) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("SignOnboardingJWT: %v", err) |
| 82 | + } |
| 83 | + if jti == "" || signed == "" { |
| 84 | + t.Error("expected non-empty jti + signed") |
| 85 | + } |
| 86 | + parsed, err := crypto.VerifyOnboardingJWT(jwtSecret, signed) |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("VerifyOnboardingJWT: %v", err) |
| 89 | + } |
| 90 | + if parsed.ID != jti { |
| 91 | + t.Errorf("ID = %q, want %q", parsed.ID, jti) |
| 92 | + } |
| 93 | + if len(parsed.Tokens) != 2 { |
| 94 | + t.Errorf("Tokens = %v", parsed.Tokens) |
| 95 | + } |
| 96 | + // ExpiresAt should be ~7 days from now |
| 97 | + if parsed.ExpiresAt == nil || time.Until(parsed.ExpiresAt.Time) < 6*24*time.Hour { |
| 98 | + t.Errorf("expected ~7d expiry, got %v", parsed.ExpiresAt) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestVerifyOnboardingJWT_Bad(t *testing.T) { |
| 103 | + if _, err := crypto.VerifyOnboardingJWT(jwtSecret, "garbage"); err == nil { |
| 104 | + t.Fatal("expected error") |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestVerifyOnboardingJWT_FutureIssuedAt(t *testing.T) { |
| 109 | + // Hand-craft an onboarding JWT with iat in the future. |
| 110 | + claims := crypto.OnboardingClaims{Fingerprint: "fp"} |
| 111 | + claims.RegisteredClaims = jwt.RegisteredClaims{ |
| 112 | + IssuedAt: jwt.NewNumericDate(time.Now().UTC().Add(2 * time.Hour)), |
| 113 | + ExpiresAt: jwt.NewNumericDate(time.Now().UTC().Add(72 * time.Hour)), |
| 114 | + ID: "jti-future", |
| 115 | + } |
| 116 | + tok := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| 117 | + signed, err := tok.SignedString(jwtSecret) |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("manual sign: %v", err) |
| 120 | + } |
| 121 | + if _, err := crypto.VerifyOnboardingJWT(jwtSecret, signed); err == nil { |
| 122 | + t.Fatal("expected error for future iat") |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// TestErrJWTSign_Wrapping exercises the Error/Unwrap on the typed errors. |
| 127 | +func TestErrJWTSign_Wrapping(t *testing.T) { |
| 128 | + cause := errors.New("underlying boom") |
| 129 | + e := &crypto.ErrJWTSign{Cause: cause} |
| 130 | + if !strings.Contains(e.Error(), "boom") { |
| 131 | + t.Errorf("Error() = %q", e.Error()) |
| 132 | + } |
| 133 | + if !errors.Is(e, cause) { |
| 134 | + t.Errorf("Unwrap should return cause") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestErrJWTVerify_Wrapping(t *testing.T) { |
| 139 | + cause := errors.New("verify boom") |
| 140 | + e := &crypto.ErrJWTVerify{Cause: cause} |
| 141 | + if !strings.Contains(e.Error(), "verify boom") { |
| 142 | + t.Errorf("Error() = %q", e.Error()) |
| 143 | + } |
| 144 | + if !errors.Is(e, cause) { |
| 145 | + t.Errorf("Unwrap should return cause") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// TestVerifyJWT_WrongAlg verifies the alg-confusion guard rejects tokens signed |
| 150 | +// with an unexpected method. |
| 151 | +func TestVerifyJWT_WrongAlg(t *testing.T) { |
| 152 | + // Sign with the "none" alg by forcing an unsigned token. The library refuses |
| 153 | + // to sign with "none" by default, so build with an unsupported alg path: |
| 154 | + // craft a token claiming alg=ES256 but signed with HS256 — the parser will |
| 155 | + // reject it because keyfunc only returns the HMAC key. |
| 156 | + tok := jwt.New(jwt.SigningMethodHS256) |
| 157 | + tok.Method = jwt.SigningMethodRS256 // mismatch — verify must refuse |
| 158 | + // Sign with HMAC anyway (force the wrong signature path). |
| 159 | + // Easier: pre-craft a fixed header-payload-fake-sig string. |
| 160 | + bad := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcCI6ImEifQ.sig" |
| 161 | + _, err := crypto.VerifyJWT(jwtSecret, bad) |
| 162 | + if err == nil { |
| 163 | + t.Fatal("expected error for non-HMAC alg") |
| 164 | + } |
| 165 | +} |
0 commit comments