use of org.pac4j.jwt.profile.JwtGenerator in project cas by apereo.
the class TokenAuthenticationHandlerTests method verifyKeysAreSane.
@Test
public void verifyKeysAreSane() throws Exception {
final JwtGenerator<CommonProfile> g = new JwtGenerator<>();
g.setSignatureConfiguration(new SecretSignatureConfiguration(SIGNING_SECRET, JWSAlgorithm.HS256));
g.setEncryptionConfiguration(new SecretEncryptionConfiguration(ENCRYPTION_SECRET, JWEAlgorithm.DIR, EncryptionMethod.A192CBC_HS384));
final CommonProfile profile = new CommonProfile();
profile.setId("casuser");
final String token = g.generate(profile);
final TokenCredential c = new TokenCredential(token, RegisteredServiceTestUtils.getService());
final AuthenticationHandlerExecutionResult result = this.tokenAuthenticationHandler.authenticate(c);
assertNotNull(result);
assertEquals(result.getPrincipal().getId(), profile.getId());
}
use of org.pac4j.jwt.profile.JwtGenerator in project cas by apereo.
the class GenerateJwtCommand method generate.
/**
* Generate.
*
* @param signingSecretSize the signing secret size
* @param encryptionSecretSize the encryption secret size
* @param signingAlgorithm the signing algorithm
* @param encryptionAlgorithm the encryption algorithm
* @param encryptionMethod the encryption algorithm
* @param subject the subject
*/
@CliCommand(value = "generate-jwt", help = "Generate a JWT with given size and algorithm for signing and encryption.")
public void generate(@CliOption(key = { "signingSecretSize" }, help = "Size of the signing secret", optionContext = "Size of the signing secret", specifiedDefaultValue = "" + DEFAULT_SIGNING_SECRET_SIZE, unspecifiedDefaultValue = "" + DEFAULT_SIGNING_SECRET_SIZE) final int signingSecretSize, @CliOption(key = { "signingSecretSize" }, help = "Size of the encryption secret", optionContext = "Size of the encryption secret", specifiedDefaultValue = "" + DEFAULT_ENCRYPTION_SECRET_SIZE, unspecifiedDefaultValue = "" + DEFAULT_ENCRYPTION_SECRET_SIZE) final int encryptionSecretSize, @CliOption(key = { "signingAlgorithm" }, help = "Algorithm to use for signing", optionContext = "Algorithm to use for signing", specifiedDefaultValue = DEFAULT_SIGNING_ALGORITHM, unspecifiedDefaultValue = DEFAULT_SIGNING_ALGORITHM) final String signingAlgorithm, @CliOption(key = { "encryptionAlgorithm" }, help = "Algorithm to use for encryption", optionContext = "Algorithm to use for encryption", specifiedDefaultValue = DEFAULT_ENCRYPTION_ALGORITHM, unspecifiedDefaultValue = DEFAULT_ENCRYPTION_ALGORITHM) final String encryptionAlgorithm, @CliOption(key = { "encryptionMethod" }, help = "Method to use for encryption", optionContext = "Method to use for encryption", specifiedDefaultValue = DEFAULT_ENCRYPTION_METHOD, unspecifiedDefaultValue = DEFAULT_ENCRYPTION_METHOD) final String encryptionMethod, @CliOption(key = { "subject" }, help = "Subject to use for the JWT", optionContext = "Subject to use for the JWT", mandatory = true) final String subject) {
final JwtGenerator<CommonProfile> g = new JwtGenerator<>();
configureJwtSigning(signingSecretSize, signingAlgorithm, g);
configureJwtEncryption(encryptionSecretSize, encryptionAlgorithm, encryptionMethod, g);
final CommonProfile profile = new CommonProfile();
profile.setId(subject);
LOGGER.debug(StringUtils.repeat('=', SEP_LENGTH));
LOGGER.info("\nGenerating JWT for subject [{}] with signing key size [{}], signing algorithm [{}], " + "encryption key size [{}], encryption method [{}] and encryption algorithm [{}]\n", subject, signingSecretSize, signingAlgorithm, encryptionSecretSize, encryptionMethod, encryptionAlgorithm);
LOGGER.debug(StringUtils.repeat('=', SEP_LENGTH));
final String token = g.generate(profile);
LOGGER.info("==== JWT ====\n[{}]", token);
}
Aggregations