@Test public void validate_subject_success() throws Exception { ToBeSignedJwt unverified = new ToBeSignedJwt.Builder().setAlgorithm("HS256").setSubject("123").build(); JwtValidator validator = new JwtValidator.Builder().setSubject("123").build(); Jwt token = validator.validate("HS256", unverified); assertThat(token.getSubject()).isEqualTo("123"); }
Jwt validate(String algorithm, ToBeSignedJwt target) throws InvalidAlgorithmParameterException, JwtExpiredException, JwtNotBeforeException, JwtInvalidException { validateTimestampClaims(target); if (!target.getAlgorithm().equals(algorithm)) { throw new InvalidAlgorithmParameterException( String.format( "invalid algorithm; expected %s, got %s", algorithm, target.getAlgorithm())); } @SuppressWarnings("unchecked") Iterator<String> headerIterator = this.header.keys(); while (headerIterator.hasNext()) { String name = headerIterator.next(); if (name.equals(JwtNames.HEADER_ALGORITHM)) { continue; } String value = target.getHeader(name); if (value == null || !value.equals(this.getHeader(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected header '%s' with value %s, but got %s", name, value, this.getHeader(name))); } } @SuppressWarnings("unchecked") Iterator<String> payloadIterator = this.payload.keys(); while (payloadIterator.hasNext()) { String name = payloadIterator.next(); if (name.equals(JwtNames.CLAIM_AUDIENCE)) { continue; } Object value = target.getClaim(name); if (value == null || !value.equals(this.getClaim(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected claim '%s' with value %s, but got %s", name, value, this.getClaim(name))); } } List<String> audiences = target.getAudiences(); String audience = (String) this.getClaim(JwtNames.CLAIM_AUDIENCE); if ((audiences == null && audience != null) || (audiences != null && !audiences.contains(audience))) { throw new JwtInvalidException( String.format( "invalid JWT; cannot find the expected audience %s in claimed audiences %s", audience, audiences)); } return new Jwt(target.getHeader(), target.getPayload(), this.clock, this.clockSkew); }
JwtValidator { Jwt validate(String algorithm, ToBeSignedJwt target) throws InvalidAlgorithmParameterException, JwtExpiredException, JwtNotBeforeException, JwtInvalidException { validateTimestampClaims(target); if (!target.getAlgorithm().equals(algorithm)) { throw new InvalidAlgorithmParameterException( String.format( "invalid algorithm; expected %s, got %s", algorithm, target.getAlgorithm())); } @SuppressWarnings("unchecked") Iterator<String> headerIterator = this.header.keys(); while (headerIterator.hasNext()) { String name = headerIterator.next(); if (name.equals(JwtNames.HEADER_ALGORITHM)) { continue; } String value = target.getHeader(name); if (value == null || !value.equals(this.getHeader(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected header '%s' with value %s, but got %s", name, value, this.getHeader(name))); } } @SuppressWarnings("unchecked") Iterator<String> payloadIterator = this.payload.keys(); while (payloadIterator.hasNext()) { String name = payloadIterator.next(); if (name.equals(JwtNames.CLAIM_AUDIENCE)) { continue; } Object value = target.getClaim(name); if (value == null || !value.equals(this.getClaim(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected claim '%s' with value %s, but got %s", name, value, this.getClaim(name))); } } List<String> audiences = target.getAudiences(); String audience = (String) this.getClaim(JwtNames.CLAIM_AUDIENCE); if ((audiences == null && audience != null) || (audiences != null && !audiences.contains(audience))) { throw new JwtInvalidException( String.format( "invalid JWT; cannot find the expected audience %s in claimed audiences %s", audience, audiences)); } return new Jwt(target.getHeader(), target.getPayload(), this.clock, this.clockSkew); } }
JwtValidator { Jwt validate(String algorithm, ToBeSignedJwt target) throws InvalidAlgorithmParameterException, JwtExpiredException, JwtNotBeforeException, JwtInvalidException { validateTimestampClaims(target); if (!target.getAlgorithm().equals(algorithm)) { throw new InvalidAlgorithmParameterException( String.format( "invalid algorithm; expected %s, got %s", algorithm, target.getAlgorithm())); } @SuppressWarnings("unchecked") Iterator<String> headerIterator = this.header.keys(); while (headerIterator.hasNext()) { String name = headerIterator.next(); if (name.equals(JwtNames.HEADER_ALGORITHM)) { continue; } String value = target.getHeader(name); if (value == null || !value.equals(this.getHeader(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected header '%s' with value %s, but got %s", name, value, this.getHeader(name))); } } @SuppressWarnings("unchecked") Iterator<String> payloadIterator = this.payload.keys(); while (payloadIterator.hasNext()) { String name = payloadIterator.next(); if (name.equals(JwtNames.CLAIM_AUDIENCE)) { continue; } Object value = target.getClaim(name); if (value == null || !value.equals(this.getClaim(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected claim '%s' with value %s, but got %s", name, value, this.getClaim(name))); } } List<String> audiences = target.getAudiences(); String audience = (String) this.getClaim(JwtNames.CLAIM_AUDIENCE); if ((audiences == null && audience != null) || (audiences != null && !audiences.contains(audience))) { throw new JwtInvalidException( String.format( "invalid JWT; cannot find the expected audience %s in claimed audiences %s", audience, audiences)); } return new Jwt(target.getHeader(), target.getPayload(), this.clock, this.clockSkew); } private  JwtValidator(Builder builder); }
JwtValidator { Jwt validate(String algorithm, ToBeSignedJwt target) throws InvalidAlgorithmParameterException, JwtExpiredException, JwtNotBeforeException, JwtInvalidException { validateTimestampClaims(target); if (!target.getAlgorithm().equals(algorithm)) { throw new InvalidAlgorithmParameterException( String.format( "invalid algorithm; expected %s, got %s", algorithm, target.getAlgorithm())); } @SuppressWarnings("unchecked") Iterator<String> headerIterator = this.header.keys(); while (headerIterator.hasNext()) { String name = headerIterator.next(); if (name.equals(JwtNames.HEADER_ALGORITHM)) { continue; } String value = target.getHeader(name); if (value == null || !value.equals(this.getHeader(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected header '%s' with value %s, but got %s", name, value, this.getHeader(name))); } } @SuppressWarnings("unchecked") Iterator<String> payloadIterator = this.payload.keys(); while (payloadIterator.hasNext()) { String name = payloadIterator.next(); if (name.equals(JwtNames.CLAIM_AUDIENCE)) { continue; } Object value = target.getClaim(name); if (value == null || !value.equals(this.getClaim(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected claim '%s' with value %s, but got %s", name, value, this.getClaim(name))); } } List<String> audiences = target.getAudiences(); String audience = (String) this.getClaim(JwtNames.CLAIM_AUDIENCE); if ((audiences == null && audience != null) || (audiences != null && !audiences.contains(audience))) { throw new JwtInvalidException( String.format( "invalid JWT; cannot find the expected audience %s in claimed audiences %s", audience, audiences)); } return new Jwt(target.getHeader(), target.getPayload(), this.clock, this.clockSkew); } private  JwtValidator(Builder builder);  }
JwtValidator { Jwt validate(String algorithm, ToBeSignedJwt target) throws InvalidAlgorithmParameterException, JwtExpiredException, JwtNotBeforeException, JwtInvalidException { validateTimestampClaims(target); if (!target.getAlgorithm().equals(algorithm)) { throw new InvalidAlgorithmParameterException( String.format( "invalid algorithm; expected %s, got %s", algorithm, target.getAlgorithm())); } @SuppressWarnings("unchecked") Iterator<String> headerIterator = this.header.keys(); while (headerIterator.hasNext()) { String name = headerIterator.next(); if (name.equals(JwtNames.HEADER_ALGORITHM)) { continue; } String value = target.getHeader(name); if (value == null || !value.equals(this.getHeader(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected header '%s' with value %s, but got %s", name, value, this.getHeader(name))); } } @SuppressWarnings("unchecked") Iterator<String> payloadIterator = this.payload.keys(); while (payloadIterator.hasNext()) { String name = payloadIterator.next(); if (name.equals(JwtNames.CLAIM_AUDIENCE)) { continue; } Object value = target.getClaim(name); if (value == null || !value.equals(this.getClaim(name))) { throw new JwtInvalidException( String.format( "invalid JWT; expected claim '%s' with value %s, but got %s", name, value, this.getClaim(name))); } } List<String> audiences = target.getAudiences(); String audience = (String) this.getClaim(JwtNames.CLAIM_AUDIENCE); if ((audiences == null && audience != null) || (audiences != null && !audiences.contains(audience))) { throw new JwtInvalidException( String.format( "invalid JWT; cannot find the expected audience %s in claimed audiences %s", audience, audiences)); } return new Jwt(target.getHeader(), target.getPayload(), this.clock, this.clockSkew); } private  JwtValidator(Builder builder);   }