use of com.nimbusds.jose.JWSHeader in project flow by vaadin.
the class JwtSecurityContextRepositoryTest method saveContext_doesSaveJwt_withIssuer.
@Test
public void saveContext_doesSaveJwt_withIssuer() throws JOSEException, BadJOSEException, ParseException {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
JWSHeader header = getHeaderBuilder().build();
JWTClaimsSet claimsSet = getClaimsSetBuilder().build();
Mockito.doReturn(getJwtAuthenticationToken(header, claimsSet)).when(securityContext).getAuthentication();
jwtSecurityContextRepository.setIssuer(TEST_ISSUER);
jwtSecurityContextRepository.saveContext(securityContext, request, response);
String serializedJwt = getSavedSerializedJwt();
JWTClaimsSet decodedClaimsSet = decodeSerializedJwt(serializedJwt, jwtProcessor);
assertClaims(decodedClaimsSet, TEST_USERNAME, TEST_ROLES, 1800);
Assert.assertEquals(TEST_ISSUER, decodedClaimsSet.getIssuer());
}
use of com.nimbusds.jose.JWSHeader in project flow by vaadin.
the class JwtSecurityContextRepositoryTest method saveContext_doesNotSaveJwt_when_algorithmNull.
@Test
public void saveContext_doesNotSaveJwt_when_algorithmNull() throws JOSEException {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
JWSHeader header = getHeaderBuilder().build();
JWTClaimsSet claimsSet = getClaimsSetBuilder().build();
Mockito.doReturn(getJwtAuthenticationToken(header, claimsSet)).when(securityContext).getAuthentication();
jwtSecurityContextRepository.setJwsAlgorithm(null);
Assert.assertThrows(IllegalArgumentException.class, () -> jwtSecurityContextRepository.saveContext(securityContext, request, response));
String serializedJwt = getSavedSerializedJwt();
Assert.assertNull(serializedJwt);
}
use of com.nimbusds.jose.JWSHeader in project flow by vaadin.
the class JwtSecurityContextRepository method encodeJwt.
private String encodeJwt(Authentication authentication) throws JOSEException {
if (authentication == null || trustResolver.isAnonymous(authentication)) {
return null;
}
final Date now = new Date();
final List<String> roles = authentication.getAuthorities().stream().map(Objects::toString).filter(a -> a.startsWith(ROLE_AUTHORITY_PREFIX)).map(a -> a.substring(ROLE_AUTHORITY_PREFIX.length())).collect(Collectors.toList());
SignedJWT signedJWT;
JWSHeader jwsHeader = new JWSHeader(jwsAlgorithm);
JWKSelector jwkSelector = new JWKSelector(JWKMatcher.forJWSHeader(jwsHeader));
List<JWK> jwks = jwkSource.get(jwkSelector, null);
JWK jwk = jwks.get(0);
JWSSigner signer = new DefaultJWSSignerFactory().createJWSSigner(jwk, jwsAlgorithm);
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject(authentication.getName()).issuer(issuer).issueTime(now).expirationTime(new Date(now.getTime() + expiresIn * 1000)).claim(ROLES_CLAIM, roles).build();
signedJWT = new SignedJWT(jwsHeader, claimsSet);
signedJWT.sign(signer);
return signedJWT.serialize();
}
use of com.nimbusds.jose.JWSHeader in project spring-cloud-gcp by spring-cloud.
the class FirebaseJwtTokenDecoderTests method invalidIssuedAt.
@Test
public void invalidIssuedAt() throws Exception {
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID("one").build();
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject("test-subject").audience("123456").expirationTime(Date.from(Instant.now().plusSeconds(36000))).issuer("https://securetoken.google.com/123456").issueTime(Date.from(Instant.now().plusSeconds(3600))).claim("auth_time", Instant.now().minusSeconds(3600).getEpochSecond()).build();
SignedJWT signedJWT = signedJwt(keyGeneratorUtils.getPrivateKey(), header, claimsSet);
List<OAuth2TokenValidator<Jwt>> validators = new ArrayList<>();
validators.add(new JwtTimestampValidator());
validators.add(new JwtIssuerValidator("https://securetoken.google.com/123456"));
validators.add(new FirebaseTokenValidator("123456"));
DelegatingOAuth2TokenValidator<Jwt> validator = new DelegatingOAuth2TokenValidator<Jwt>(validators);
RestOperations operations = mockRestOperations();
FirebaseJwtTokenDecoder decoder = new FirebaseJwtTokenDecoder(operations, "https://spring.local", validator);
assertThatExceptionOfType(JwtException.class).isThrownBy(() -> decoder.decode(signedJWT.serialize())).withMessageStartingWith("An error occurred while attempting to decode the Jwt: iat claim header must be in the past");
}
use of com.nimbusds.jose.JWSHeader in project spring-cloud-gcp by spring-cloud.
the class FirebaseJwtTokenDecoderTests method signedTokenTests.
@Test
public void signedTokenTests() throws Exception {
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).keyID("one").build();
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject("test-subject").expirationTime(Date.from(Instant.now().plusSeconds(60))).build();
SignedJWT signedJWT = signedJwt(keyGeneratorUtils.getPrivateKey(), header, claimsSet);
OAuth2TokenValidator validator = mock(OAuth2TokenValidator.class);
when(validator.validate(any())).thenReturn(OAuth2TokenValidatorResult.success());
FirebaseJwtTokenDecoder decoder = new FirebaseJwtTokenDecoder(mockRestOperations(), "https://spring.local", validator);
decoder.decode(signedJWT.serialize());
}
Aggregations