Search in sources :

Example 1 with JwtGenerator

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());
}
Also used : JwtGenerator(org.pac4j.jwt.profile.JwtGenerator) CommonProfile(org.pac4j.core.profile.CommonProfile) SecretEncryptionConfiguration(org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration) AuthenticationHandlerExecutionResult(org.apereo.cas.authentication.AuthenticationHandlerExecutionResult) SecretSignatureConfiguration(org.pac4j.jwt.config.signature.SecretSignatureConfiguration) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with JwtGenerator

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);
}
Also used : JwtGenerator(org.pac4j.jwt.profile.JwtGenerator) CommonProfile(org.pac4j.core.profile.CommonProfile) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Aggregations

CommonProfile (org.pac4j.core.profile.CommonProfile)2 JwtGenerator (org.pac4j.jwt.profile.JwtGenerator)2 AuthenticationHandlerExecutionResult (org.apereo.cas.authentication.AuthenticationHandlerExecutionResult)1 Test (org.junit.Test)1 SecretEncryptionConfiguration (org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration)1 SecretSignatureConfiguration (org.pac4j.jwt.config.signature.SecretSignatureConfiguration)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 CliCommand (org.springframework.shell.core.annotation.CliCommand)1