use of com.nimbusds.jose.JWSHeader in project SEPA by arces-wot.
the class SecurityManagerTest method generateToken.
private SignedJWT generateToken(DigitalIdentity identity, String password) throws ParseException, KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException, JOSEException, SEPASecurityException {
// Prepare JWT with claims set
JWTClaimsSet.Builder claimsSetBuilder = new JWTClaimsSet.Builder();
// Define validity period
Date now = new Date();
long exp = 0;
if (identity.getClass().equals(DeviceIdentity.class)) {
exp = auth.getDeviceExpiringPeriod();
} else if (identity.getClass().equals(ApplicationIdentity.class)) {
exp = auth.getApplicationExpiringPeriod();
} else
exp = auth.getDefaultExpiringPeriod();
Date expires = new Date(now.getTime() + exp * 1000);
claimsSetBuilder.issuer("http://issuer");
claimsSetBuilder.subject("http://subject");
ArrayList<String> audience = new ArrayList<String>();
audience.add("https://audience");
audience.add("wss://audience");
claimsSetBuilder.audience(audience);
claimsSetBuilder.expirationTime(expires);
claimsSetBuilder.issueTime(now);
claimsSetBuilder.jwtID(identity.getUid() + ":" + password + ":" + UUID.randomUUID());
JWTClaimsSet jwtClaims = claimsSetBuilder.build();
// ******************************
// Sign JWT with private RSA key
// ******************************
SignedJWT signedJWT;
signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.RS256), JWTClaimsSet.parse(jwtClaims.toString()));
// // Load the key from the key store
// KeyStore keystore = KeyStore.getInstance("JKS");
//
// keystore.load(new FileInputStream(jksFile), storePass.toCharArray());
// RSAKey jwk = RSAKey.load(keystore, alias, keyPass.toCharArray());
RSAKey jwk = configurationProvider.getRsaKey();
// Get the private and public keys to sign and verify
RSAPrivateKey privateKey = jwk.toRSAPrivateKey();
// Create RSA-signer with the private key
JWSSigner signer = new RSASSASigner(privateKey);
signedJWT.sign(signer);
return signedJWT;
}
use of com.nimbusds.jose.JWSHeader in project iaf by ibissource.
the class ApiListenerServletTest method createJWT.
private String createJWT() throws Exception {
JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).build();
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
builder.issuer("JWTPipeTest");
builder.subject("UnitTest");
builder.audience("Framework");
builder.jwtID("1234");
SignedJWT signedJWT = new SignedJWT(jwsHeader, builder.build());
KeyStore keystore = PkiUtil.createKeyStore(TestFileUtils.getTestFileURL("/JWT/jwt_keystore.p12"), "geheim", KeystoreType.PKCS12, "Keys for signing");
KeyManager[] keymanagers = PkiUtil.createKeyManagers(keystore, "geheim", null);
X509KeyManager keyManager = (X509KeyManager) keymanagers[0];
PrivateKey privateKey = keyManager.getPrivateKey("1");
PublicKey publicKey = keystore.getCertificate("1").getPublicKey();
JWK jwk = new RSAKey.Builder((RSAPublicKey) publicKey).privateKey(privateKey).keyUse(KeyUse.SIGNATURE).keyOperations(Collections.singleton(KeyOperation.SIGN)).algorithm(JWSAlgorithm.RS256).keyStore(keystore).build();
DefaultJWSSignerFactory factory = new DefaultJWSSignerFactory();
JWSSigner jwsSigner = factory.createJWSSigner(jwk, JWSAlgorithm.RS256);
signedJWT.sign(jwsSigner);
return signedJWT.serialize();
}
use of com.nimbusds.jose.JWSHeader in project flow by vaadin.
the class JwtSecurityContextRepositoryTest method saveContext_doesNotSaveJwt_when_keySourceReturnsNull.
@Test
public void saveContext_doesNotSaveJwt_when_keySourceReturnsNull() throws JOSEException {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
JWSHeader header = getHeaderBuilder().build();
JWTClaimsSet claimsSet = getClaimsSetBuilder().build();
Mockito.doReturn(getJwtAuthenticationToken(header, claimsSet)).when(securityContext).getAuthentication();
jwtSecurityContextRepository.setJwkSource((jwkSelector, context) -> null);
Assert.assertThrows(NullPointerException.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 JwtSecurityContextRepositoryTest method saveContext_doesSaveJwt_withExpiresIn.
@Test
public void saveContext_doesSaveJwt_withExpiresIn() 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.setExpiresIn(300);
jwtSecurityContextRepository.saveContext(securityContext, request, response);
String serializedJwt = getSavedSerializedJwt();
JWTClaimsSet decodedClaimsSet = decodeSerializedJwt(serializedJwt, jwtProcessor);
assertClaims(decodedClaimsSet, TEST_USERNAME, TEST_ROLES, 300);
}
use of com.nimbusds.jose.JWSHeader in project flow by vaadin.
the class JwtSecurityContextRepositoryTest method saveContext_doesNotSaveJwt_when_trustResolverIsAnonymousReturnsTrue.
@Test
public void saveContext_doesNotSaveJwt_when_trustResolverIsAnonymousReturnsTrue() throws JOSEException {
SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
JWSHeader header = getHeaderBuilder().build();
JWTClaimsSet claimsSet = getClaimsSetBuilder().build();
Authentication authentication = getJwtAuthenticationToken(header, claimsSet);
Mockito.doReturn(authentication).when(securityContext).getAuthentication();
AuthenticationTrustResolver trustResolver = Mockito.mock(AuthenticationTrustResolver.class);
Mockito.doReturn(true).when(trustResolver).isAnonymous(authentication);
jwtSecurityContextRepository.setTrustResolver(trustResolver);
jwtSecurityContextRepository.saveContext(securityContext, request, response);
Mockito.verify(trustResolver).isAnonymous(authentication);
String serializedJwt = getSavedSerializedJwt();
Assert.assertNull(serializedJwt);
}
Aggregations