use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project cas by apereo.
the class GenerateJwtCommand method configureJwtEncryption.
private void configureJwtEncryption(final int encryptionSecretSize, final String encryptionAlgorithm, final String encryptionMethod, final JwtGenerator<CommonProfile> g) {
if (encryptionSecretSize <= 0 || StringUtils.isBlank(encryptionMethod) || StringUtils.isBlank(encryptionAlgorithm)) {
LOGGER.info("No encryption algorithm or size specified, so the generated JWT will not be encrypted");
return;
}
final String encryptionSecret = RandomStringUtils.randomAlphanumeric(encryptionSecretSize);
LOGGER.info("==== Encryption Secret ====\n[{}]\n", encryptionSecret);
final String acceptedEncAlgs = Arrays.stream(JWEAlgorithm.class.getDeclaredFields()).filter(f -> f.getType().equals(JWEAlgorithm.class)).map(Unchecked.function(f -> {
f.setAccessible(true);
return ((JWEAlgorithm) f.get(null)).getName();
})).collect(Collectors.joining(","));
LOGGER.debug("Encryption algorithm: [{}]. Available algorithms are [{}]", encryptionAlgorithm, acceptedEncAlgs);
final String acceptedEncMethods = Arrays.stream(EncryptionMethod.class.getDeclaredFields()).filter(f -> f.getType().equals(EncryptionMethod.class)).map(Unchecked.function(f -> {
f.setAccessible(true);
return ((EncryptionMethod) f.get(null)).getName();
})).collect(Collectors.joining(","));
LOGGER.debug("Encryption method: [{}]. Available methods are [{}]", encryptionMethod, acceptedEncMethods);
final JWEAlgorithm algorithm = JWEAlgorithm.parse(encryptionAlgorithm);
final EncryptionMethod encryptionMethodAlg = EncryptionMethod.parse(encryptionMethod);
if (DirectDecrypter.SUPPORTED_ALGORITHMS.contains(algorithm)) {
if (!DirectDecrypter.SUPPORTED_ENCRYPTION_METHODS.contains(encryptionMethodAlg)) {
LOGGER.warn("Encrypted method [{}] is not supported for algorithm [{}]. Accepted methods are [{}]", encryptionMethod, encryptionAlgorithm, DirectDecrypter.SUPPORTED_ENCRYPTION_METHODS);
return;
}
}
if (AESDecrypter.SUPPORTED_ALGORITHMS.contains(algorithm)) {
if (!AESDecrypter.SUPPORTED_ENCRYPTION_METHODS.contains(encryptionMethodAlg)) {
LOGGER.warn("Encrypted method [{}] is not supported for algorithm [{}]. Accepted methods are [{}]", encryptionMethod, encryptionAlgorithm, AESDecrypter.SUPPORTED_ENCRYPTION_METHODS);
return;
}
}
g.setEncryptionConfiguration(new SecretEncryptionConfiguration(encryptionSecret, algorithm, encryptionMethodAlg));
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testGenerateAuthenticateDifferentSecrets.
@Test
public void testGenerateAuthenticateDifferentSecrets() {
final SignatureConfiguration signatureConfiguration = new SecretSignatureConfiguration(MAC_SECRET);
final EncryptionConfiguration encryptionConfiguration = new SecretEncryptionConfiguration(KEY2);
final JwtGenerator<FacebookProfile> generator = new JwtGenerator<>(signatureConfiguration, encryptionConfiguration);
final FacebookProfile profile = createProfile();
final String token = generator.generate(profile);
assertToken(profile, token, new JwtAuthenticator(signatureConfiguration, encryptionConfiguration));
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testGenericJwt.
@Test
public void testGenericJwt() {
final String token = "eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiZGlyIn0..NTvhJXwZ_sN4zYBK.exyLJWkOclCVcffz58CE-" + "3XWWV24aYyGWR5HVrfm4HLQi1xgmwglLlEIiFlOSTOSZ_LeAwl2Z3VFh-5EidocjwGkAPGQA_4_KCLbK8Im7M25ZZvDzCJ1kKN1JrDIIrBWCcuI4Mbw0O" + "_YGb8TfIECPkpeG7wEgBG30sb1kH-F_vg9yjYfB4MiJCSFmY7cRqN9-9O23tz3wYv3b-eJh5ACr2CGSVNj2KcMsOMJ6bbALgz6pzQTIWk_" + "fhcE9QSfaSY7RuZ8cRTV-UTjYgZk1gbd1LskgchS.ijMQmfPlObJv7oaPG8LCEg";
final TokenCredentials credentials = new TokenCredentials(token);
final JwtAuthenticator authenticator = new JwtAuthenticator(new SecretSignatureConfiguration(MAC_SECRET), new SecretEncryptionConfiguration(MAC_SECRET));
authenticator.validate(credentials, null);
assertNotNull(credentials.getUserProfile());
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testJwtGenerationA256CBC.
@Test
public void testJwtGenerationA256CBC() {
final JwtGenerator<CommonProfile> g = new JwtGenerator<>(new SecretSignatureConfiguration(MAC_SECRET + MAC_SECRET + MAC_SECRET + MAC_SECRET + MAC_SECRET + MAC_SECRET + MAC_SECRET + MAC_SECRET), new SecretEncryptionConfiguration(KEY2 + KEY2));
((SecretEncryptionConfiguration) g.getEncryptionConfiguration()).setMethod(EncryptionMethod.A256CBC_HS512);
final String g1 = g.generate(new CommonProfile());
assertNotNull(g1);
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testAuthenticateFailed.
@Test(expected = CredentialsException.class)
public void testAuthenticateFailed() {
final JwtAuthenticator authenticator = new JwtAuthenticator(new SecretSignatureConfiguration(MAC_SECRET), new SecretEncryptionConfiguration(MAC_SECRET));
final TokenCredentials credentials = new TokenCredentials("fakeToken");
authenticator.validate(credentials, null);
}
Aggregations