|
| 1 | +package dev.arcp.auth; |
| 2 | + |
| 3 | +import com.nimbusds.jose.JOSEException; |
| 4 | +import com.nimbusds.jose.JWSAlgorithm; |
| 5 | +import com.nimbusds.jose.JWSVerifier; |
| 6 | +import com.nimbusds.jose.crypto.MACVerifier; |
| 7 | +import com.nimbusds.jwt.JWTClaimsSet; |
| 8 | +import com.nimbusds.jwt.SignedJWT; |
| 9 | +import dev.arcp.error.ARCPException; |
| 10 | +import dev.arcp.error.ErrorCode; |
| 11 | +import java.text.ParseException; |
| 12 | +import java.time.Clock; |
| 13 | +import java.time.Instant; |
| 14 | +import java.util.Objects; |
| 15 | + |
| 16 | +/** |
| 17 | + * HMAC-SHA256 JWT validator (RFC §8.2 {@code signed_jwt}). Verifies signature, |
| 18 | + * expiry, and issuer; returns a {@link Principal} keyed on the {@code sub} |
| 19 | + * claim. |
| 20 | + * |
| 21 | + * <p> |
| 22 | + * Production deployments substitute a richer validator for RSA/EC keys and |
| 23 | + * JWKS-backed key rotation; this implementation covers the v0.1 reference |
| 24 | + * surface. |
| 25 | + */ |
| 26 | +public final class JwtValidator implements CredentialValidator { |
| 27 | + |
| 28 | + private final JWSVerifier verifier; |
| 29 | + private final String expectedIssuer; |
| 30 | + private final Clock clock; |
| 31 | + |
| 32 | + public JwtValidator(byte[] hmacKey, String expectedIssuer) { |
| 33 | + this(hmacKey, expectedIssuer, Clock.systemUTC()); |
| 34 | + } |
| 35 | + |
| 36 | + public JwtValidator(byte[] hmacKey, String expectedIssuer, Clock clock) { |
| 37 | + Objects.requireNonNull(hmacKey, "hmacKey"); |
| 38 | + this.expectedIssuer = Objects.requireNonNull(expectedIssuer, "expectedIssuer"); |
| 39 | + this.clock = Objects.requireNonNull(clock, "clock"); |
| 40 | + try { |
| 41 | + this.verifier = new MACVerifier(hmacKey); |
| 42 | + } catch (JOSEException e) { |
| 43 | + throw new ARCPException(ErrorCode.INTERNAL, "could not build JWT verifier", e); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Principal validate(Credentials credentials) { |
| 49 | + if (!(credentials instanceof Credentials.JwtCredentials jwt)) { |
| 50 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "signed_jwt scheme required"); |
| 51 | + } |
| 52 | + SignedJWT parsed; |
| 53 | + try { |
| 54 | + parsed = SignedJWT.parse(jwt.jwt()); |
| 55 | + } catch (ParseException e) { |
| 56 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "malformed JWT", e); |
| 57 | + } |
| 58 | + if (!JWSAlgorithm.HS256.equals(parsed.getHeader().getAlgorithm())) { |
| 59 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "expected HS256 JWT"); |
| 60 | + } |
| 61 | + try { |
| 62 | + if (!parsed.verify(verifier)) { |
| 63 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "bad signature"); |
| 64 | + } |
| 65 | + } catch (JOSEException e) { |
| 66 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "verifier error", e); |
| 67 | + } |
| 68 | + JWTClaimsSet claims; |
| 69 | + try { |
| 70 | + claims = parsed.getJWTClaimsSet(); |
| 71 | + } catch (ParseException e) { |
| 72 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "malformed claims", e); |
| 73 | + } |
| 74 | + if (!expectedIssuer.equals(claims.getIssuer())) { |
| 75 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "bad issuer"); |
| 76 | + } |
| 77 | + if (claims.getExpirationTime() == null || claims.getExpirationTime().toInstant().isBefore(Instant.now(clock))) { |
| 78 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "expired or missing exp"); |
| 79 | + } |
| 80 | + String subject = claims.getSubject(); |
| 81 | + if (subject == null || subject.isBlank()) { |
| 82 | + throw new ARCPException(ErrorCode.UNAUTHENTICATED, "missing subject"); |
| 83 | + } |
| 84 | + String trust = claims.getClaim("trust_level") instanceof String s ? s : "trusted"; |
| 85 | + return new Principal(subject, trust); |
| 86 | + } |
| 87 | +} |
0 commit comments