use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testGenerateAuthenticateNotSigned.
@Test
public void testGenerateAuthenticateNotSigned() {
final JwtGenerator<FacebookProfile> generator = new JwtGenerator<>();
generator.setEncryptionConfiguration(new SecretEncryptionConfiguration(MAC_SECRET));
final FacebookProfile profile = createProfile();
final String token = generator.generate(profile);
assertToken(profile, token);
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testGenerateAuthenticateUselessSignatureConfiguration.
@Test
public void testGenerateAuthenticateUselessSignatureConfiguration() {
final SignatureConfiguration signatureConfiguration = new SecretSignatureConfiguration(KEY2);
final SignatureConfiguration signatureConfiguration2 = new SecretSignatureConfiguration(MAC_SECRET);
final EncryptionConfiguration encryptionConfiguration = new SecretEncryptionConfiguration(MAC_SECRET);
final JwtGenerator<FacebookProfile> generator = new JwtGenerator<>(signatureConfiguration, encryptionConfiguration);
final FacebookProfile profile = createProfile();
final String token = generator.generate(profile);
final JwtAuthenticator jwtAuthenticator = new JwtAuthenticator();
jwtAuthenticator.addSignatureConfiguration(signatureConfiguration);
jwtAuthenticator.addSignatureConfiguration(signatureConfiguration2);
jwtAuthenticator.setEncryptionConfiguration(encryptionConfiguration);
assertToken(profile, token, jwtAuthenticator);
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testGenerateAuthenticate.
@Test
public void testGenerateAuthenticate() {
final JwtGenerator<FacebookProfile> generator = new JwtGenerator<>(new SecretSignatureConfiguration(MAC_SECRET), new SecretEncryptionConfiguration(MAC_SECRET));
final FacebookProfile profile = createProfile();
final String token = generator.generate(profile);
assertToken(profile, token);
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project cas by apereo.
the class GenerateJwtCommand method configureJwtEncryption.
private static void configureJwtEncryption(final int encryptionSecretSize, final String encryptionAlgorithm, final String encryptionMethod, final JwtGenerator 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;
}
val encryptionSecret = RandomUtils.randomAlphanumeric(encryptionSecretSize);
LOGGER.info("==== Encryption Secret ====\n[{}]\n", encryptionSecret);
val 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);
val 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);
val algorithm = JWEAlgorithm.parse(encryptionAlgorithm);
val 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 cas by apereo.
the class TokenAuthenticationHandler method getSecretEncryptionConfiguration.
/**
* Gets secret encryption configuration.
*
* @param service the service
* @return the secret encryption configuration
*/
protected SecretEncryptionConfiguration getSecretEncryptionConfiguration(final RegisteredService service) {
val encryptionSecret = getRegisteredServiceJwtEncryptionSecret(service);
val sets = new HashSet<Algorithm>(0);
sets.addAll(JWEAlgorithm.Family.AES_GCM_KW);
sets.addAll(JWEAlgorithm.Family.AES_KW);
sets.addAll(JWEAlgorithm.Family.ASYMMETRIC);
sets.addAll(JWEAlgorithm.Family.ECDH_ES);
sets.addAll(JWEAlgorithm.Family.PBES2);
sets.addAll(JWEAlgorithm.Family.RSA);
sets.addAll(JWEAlgorithm.Family.SYMMETRIC);
val encryptionAlg = getRegisteredServiceJwtProperty(service, RegisteredServiceProperties.TOKEN_SECRET_ENCRYPTION_ALG);
val encryptionSecretAlg = StringUtils.defaultString(encryptionAlg, JWEAlgorithm.DIR.getName());
val encAlg = findAlgorithmFamily(sets, encryptionSecretAlg, JWEAlgorithm.class);
sets.clear();
sets.addAll(EncryptionMethod.Family.AES_CBC_HMAC_SHA);
sets.addAll(EncryptionMethod.Family.AES_GCM);
val encryptionMethod = getRegisteredServiceJwtProperty(service, RegisteredServiceProperties.TOKEN_SECRET_ENCRYPTION_METHOD);
val encryptionSecretMethod = StringUtils.defaultString(encryptionMethod, EncryptionMethod.A192CBC_HS384.getName());
val encMethod = findAlgorithmFamily(sets, encryptionSecretMethod, EncryptionMethod.class);
val encSecretBytes = getSecretBytes(encryptionSecret, areSecretsBase64Encoded(service));
return new SecretEncryptionConfiguration(encSecretBytes, encAlg, encMethod);
}
Aggregations