|
| 1 | +package sidecar |
| 2 | + |
| 3 | +import "errors" |
| 4 | + |
| 5 | +// validateVCClaims enforces the local VC-JWT claim relationships the sidecar |
| 6 | +// expects before deeper cryptographic checks run. |
| 7 | +func validateVCClaims(jwtPayload, vc map[string]any) error { |
| 8 | + if err := requireOptionalClaimMatch( |
| 9 | + asString(jwtPayload["iss"]), |
| 10 | + asString(vc["issuer"]), |
| 11 | + "JWT iss does not match vc.issuer", |
| 12 | + ); err != nil { |
| 13 | + return err |
| 14 | + } |
| 15 | + if err := requireOptionalClaimMatch( |
| 16 | + asString(jwtPayload["jti"]), |
| 17 | + asString(vc["id"]), |
| 18 | + "JWT jti does not match vc.id", |
| 19 | + ); err != nil { |
| 20 | + return err |
| 21 | + } |
| 22 | + |
| 23 | + if subject := asMap(vc["credentialSubject"]); subject != nil { |
| 24 | + if err := requireOptionalClaimMatch( |
| 25 | + asString(jwtPayload["sub"]), |
| 26 | + asString(subject["id"]), |
| 27 | + "JWT sub does not match credentialSubject.id", |
| 28 | + ); err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if err := requireNumericClaim(jwtPayload, "iat", "VC-JWT requires numeric iat"); err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + if err := requireNumericClaim(jwtPayload, "nbf", "VC-JWT requires numeric nbf"); err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +// validateVPClaims enforces the local VP-JWT claim relationships the sidecar |
| 43 | +// expects before nested verification begins. |
| 44 | +func validateVPClaims(jwtPayload, vp map[string]any, audience, nonce string) error { |
| 45 | + if err := requireOptionalClaimMatch( |
| 46 | + asString(jwtPayload["iss"]), |
| 47 | + asString(vp["holder"]), |
| 48 | + "JWT iss does not match vp.holder", |
| 49 | + ); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + if err := requireOptionalClaimMatch( |
| 53 | + asString(jwtPayload["aud"]), |
| 54 | + audience, |
| 55 | + "JWT aud does not match expected audience", |
| 56 | + ); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if err := requireOptionalClaimMatch( |
| 60 | + asString(jwtPayload["nonce"]), |
| 61 | + nonce, |
| 62 | + "JWT nonce does not match expected nonce", |
| 63 | + ); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + return requireNumericClaim(jwtPayload, "iat", "VP-JWT requires numeric iat") |
| 67 | +} |
| 68 | + |
| 69 | +// requireOptionalClaimMatch fails only when the expected value is present and |
| 70 | +// the actual value does not match it. |
| 71 | +func requireOptionalClaimMatch(actual, expected, message string) error { |
| 72 | + if claimMatchesExpected(actual, expected) { |
| 73 | + return nil |
| 74 | + } |
| 75 | + return errors.New(message) |
| 76 | +} |
| 77 | + |
| 78 | +// claimMatchesExpected implements the sidecar's "empty expected means optional" |
| 79 | +// matching rule for JWT claim checks. |
| 80 | +func claimMatchesExpected(actual, expected string) bool { |
| 81 | + return expected == "" || actual == expected |
| 82 | +} |
| 83 | + |
| 84 | +// requireNumericClaim enforces that one decoded JWT payload claim was parsed as |
| 85 | +// a JSON number. |
| 86 | +func requireNumericClaim(payload map[string]any, claim, message string) error { |
| 87 | + if _, ok := payload[claim].(float64); ok { |
| 88 | + return nil |
| 89 | + } |
| 90 | + return errors.New(message) |
| 91 | +} |
0 commit comments