Search in sources :

Example 1 with ImmutableSecret

use of com.nimbusds.jose.jwk.source.ImmutableSecret in project flow by vaadin.

the class JwtSecurityContextRepositoryTest method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.openMocks(this);
    SecurityContextHolder.setStrategyName(TestSecurityContextHolderStrategy.class.getName());
    final ImmutableSecret secret = new ImmutableSecret<>(TEST_KEY);
    holder = new HttpRequestResponseHolder(request, response);
    jwtSecurityContextRepository = new JwtSecurityContextRepository(serializedJwtSplitCookieRepository);
    jwtSecurityContextRepository.setJwkSource(secret);
    jwtSecurityContextRepository.setJwsAlgorithm(JWSAlgorithm.HS256);
    // Processor for asserting saved JWTs
    jwtProcessor = new DefaultJWTProcessor<>();
    jwtProcessor.setJWSKeySelector(new JWSVerificationKeySelector<>(JWSAlgorithm.HS256, secret));
}
Also used : HttpRequestResponseHolder(org.springframework.security.web.context.HttpRequestResponseHolder) ImmutableSecret(com.nimbusds.jose.jwk.source.ImmutableSecret) Before(org.junit.Before)

Example 2 with ImmutableSecret

use of com.nimbusds.jose.jwk.source.ImmutableSecret in project flow by vaadin.

the class JwtSecurityContextRepositoryTest method saveContext_doesSaveJwt_withAlgoritm.

@Test
public void saveContext_doesSaveJwt_withAlgoritm() throws JOSEException, BadJOSEException, ParseException {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    JWSHeader header = getHeaderBuilder().build();
    JWTClaimsSet claimsSet = getClaimsSetBuilder().build();
    Mockito.doReturn(getJwtAuthenticationToken(header, claimsSet)).when(securityContext).getAuthentication();
    final ImmutableSecret secret = new ImmutableSecret<>(TEST_64BYTE_KEY.getEncoded());
    jwtSecurityContextRepository.setJwkSource(secret);
    jwtSecurityContextRepository.setJwsAlgorithm(JWSAlgorithm.HS512);
    jwtSecurityContextRepository.saveContext(securityContext, request, response);
    jwtProcessor.setJWSKeySelector(new JWSVerificationKeySelector<>(JWSAlgorithm.HS512, secret));
    String serializedJwt = getSavedSerializedJwt();
    JWTClaimsSet decodedClaimsSet = decodeSerializedJwt(serializedJwt, jwtProcessor);
    assertClaims(decodedClaimsSet, TEST_USERNAME, TEST_ROLES, 1800);
}
Also used : ImmutableSecret(com.nimbusds.jose.jwk.source.ImmutableSecret) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SecurityContext(org.springframework.security.core.context.SecurityContext) JWSHeader(com.nimbusds.jose.JWSHeader) Test(org.junit.Test)

Example 3 with ImmutableSecret

use of com.nimbusds.jose.jwk.source.ImmutableSecret in project flow by vaadin.

the class JwtSecurityContextRepositoryTest method saveContext_doesSaveJwt_withOtherKey.

@Test
public void saveContext_doesSaveJwt_withOtherKey() throws JOSEException, BadJOSEException, ParseException {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    JWSHeader header = getHeaderBuilder().build();
    JWTClaimsSet claimsSet = getClaimsSetBuilder().build();
    Mockito.doReturn(getJwtAuthenticationToken(header, claimsSet)).when(securityContext).getAuthentication();
    final ImmutableSecret secret = new ImmutableSecret<>(TEST_OTHER_KEY);
    jwtSecurityContextRepository.setJwkSource(secret);
    jwtSecurityContextRepository.saveContext(securityContext, request, response);
    jwtProcessor.setJWSKeySelector(new JWSVerificationKeySelector<>(JWSAlgorithm.HS256, secret));
    String serializedJwt = getSavedSerializedJwt();
    JWTClaimsSet decodedClaimsSet = decodeSerializedJwt(serializedJwt, jwtProcessor);
    assertClaims(decodedClaimsSet, TEST_USERNAME, TEST_ROLES, 1800);
}
Also used : ImmutableSecret(com.nimbusds.jose.jwk.source.ImmutableSecret) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SecurityContext(org.springframework.security.core.context.SecurityContext) JWSHeader(com.nimbusds.jose.JWSHeader) Test(org.junit.Test)

Aggregations

ImmutableSecret (com.nimbusds.jose.jwk.source.ImmutableSecret)3 JWSHeader (com.nimbusds.jose.JWSHeader)2 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)2 Test (org.junit.Test)2 SecurityContext (org.springframework.security.core.context.SecurityContext)2 Before (org.junit.Before)1 HttpRequestResponseHolder (org.springframework.security.web.context.HttpRequestResponseHolder)1