use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-security by spring-projects.
the class JwtTimestampValidatorTests method validateWhenConfiguredWithClockSkewThenValidatesUsingThatSkew.
@Test
public void validateWhenConfiguredWithClockSkewThenValidatesUsingThatSkew() {
Duration oneDayOff = Duration.ofDays(1);
JwtTimestampValidator jwtValidator = new JwtTimestampValidator(oneDayOff);
Instant now = Instant.now();
Instant almostOneDayAgo = now.minus(oneDayOff).plusSeconds(10);
Instant almostOneDayFromNow = now.plus(oneDayOff).minusSeconds(10);
Instant justOverOneDayAgo = now.minus(oneDayOff).minusSeconds(10);
Instant justOverOneDayFromNow = now.plus(oneDayOff).plusSeconds(10);
Jwt jwt = TestJwts.jwt().expiresAt(almostOneDayAgo).notBefore(almostOneDayFromNow).build();
assertThat(jwtValidator.validate(jwt).hasErrors()).isFalse();
jwt = TestJwts.jwt().expiresAt(justOverOneDayAgo).build();
OAuth2TokenValidatorResult result = jwtValidator.validate(jwt);
// @formatter:off
Collection<String> messages = result.getErrors().stream().map(OAuth2Error::getDescription).collect(Collectors.toList());
// @formatter:on
assertThat(result.hasErrors()).isTrue();
assertThat(messages).contains("Jwt expired at " + justOverOneDayAgo);
jwt = TestJwts.jwt().notBefore(justOverOneDayFromNow).build();
result = jwtValidator.validate(jwt);
// @formatter:off
messages = result.getErrors().stream().map(OAuth2Error::getDescription).collect(Collectors.toList());
// @formatter:on
assertThat(result.hasErrors()).isTrue();
assertThat(result.getErrors().iterator().next().getErrorCode()).isEqualTo(OAuth2ErrorCodes.INVALID_TOKEN);
assertThat(messages).contains("Jwt used before " + justOverOneDayFromNow);
}
use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-security by spring-projects.
the class JwtTimestampValidator method validate.
@Override
public OAuth2TokenValidatorResult validate(Jwt jwt) {
Assert.notNull(jwt, "jwt cannot be null");
Instant expiry = jwt.getExpiresAt();
if (expiry != null) {
if (Instant.now(this.clock).minus(this.clockSkew).isAfter(expiry)) {
OAuth2Error oAuth2Error = createOAuth2Error(String.format("Jwt expired at %s", jwt.getExpiresAt()));
return OAuth2TokenValidatorResult.failure(oAuth2Error);
}
}
Instant notBefore = jwt.getNotBefore();
if (notBefore != null) {
if (Instant.now(this.clock).plus(this.clockSkew).isBefore(notBefore)) {
OAuth2Error oAuth2Error = createOAuth2Error(String.format("Jwt used before %s", jwt.getNotBefore()));
return OAuth2TokenValidatorResult.failure(oAuth2Error);
}
}
return OAuth2TokenValidatorResult.success();
}
use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-security by spring-projects.
the class NimbusReactiveJwtDecoder method validateJwt.
private Jwt validateJwt(Jwt jwt) {
OAuth2TokenValidatorResult result = this.jwtValidator.validate(jwt);
if (result.hasErrors()) {
Collection<OAuth2Error> errors = result.getErrors();
String validationErrorString = getJwtValidationExceptionMessage(errors);
throw new JwtValidationException(validationErrorString, errors);
}
return jwt;
}
use of org.springframework.security.oauth2.core.OAuth2TokenValidatorResult in project spring-cloud-gcp by spring-cloud.
the class IapAuthenticationAutoConfigurationTests method testIapBeansReturnedWhenBothIapWithMultipleAudiencesAndSpringSecurityConfigPresent.
@Test
public void testIapBeansReturnedWhenBothIapWithMultipleAudiencesAndSpringSecurityConfigPresent() {
when(mockJwt.getAudience()).thenReturn(Collections.singletonList("aud1"));
this.contextRunner.withPropertyValues("spring.cloud.gcp.security.iap.audience=aud1, aud2").run((context) -> {
AudienceValidator validator = context.getBean(AudienceValidator.class);
OAuth2TokenValidatorResult result = validator.validate(mockJwt);
assertThat(result.hasErrors()).isFalse();
});
}
Aggregations