AudienceValidator implements OAuth2TokenValidator<Jwt> { public OAuth2TokenValidatorResult validate(Jwt jwt) { List<String> audience = jwt.getAudience(); if (audience.contains("account") || audience.contains("api: return OAuth2TokenValidatorResult.success(); } else { log.warn("Invalid audience: {}", audience); return OAuth2TokenValidatorResult.failure(error); } }  OAuth2TokenValidatorResult validate(Jwt jwt);  }
@Test @SuppressWarnings("unchecked") public void testValidAudience() { Map<String, Object> claims = new HashMap<>(); claims.put("aud", "api: Jwt badJwt = mock(Jwt.class); when(badJwt.getAudience()).thenReturn(new ArrayList(claims.values())); assertThat(validator.validate(badJwt).hasErrors()).isFalse(); }
@Test @SuppressWarnings("unchecked") public void testInvalidAudience() { Map<String, Object> claims = new HashMap<>(); claims.put("aud", "bar"); Jwt badJwt = mock(Jwt.class); when(badJwt.getAudience()).thenReturn(new ArrayList(claims.values())); assertThat(validator.validate(badJwt).hasErrors()).isTrue(); }