use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testGenerateAuthenticateClaims.
@Test
public void testGenerateAuthenticateClaims() {
final JwtGenerator<JwtProfile> generator = new JwtGenerator<>(new SecretSignatureConfiguration(MAC_SECRET), new SecretEncryptionConfiguration(MAC_SECRET));
final Map<String, Object> claims = new HashMap<>();
claims.put(JwtClaims.SUBJECT, VALUE);
final Date tomorrow = tomorrow();
claims.put(JwtClaims.EXPIRATION_TIME, tomorrow);
final String token = generator.generate(claims);
final JwtAuthenticator jwtAuthenticator = new JwtAuthenticator(new SecretSignatureConfiguration(MAC_SECRET), new SecretEncryptionConfiguration(MAC_SECRET));
final JwtProfile profile = (JwtProfile) jwtAuthenticator.validateToken(token);
assertEquals(VALUE, profile.getSubject());
assertEquals(tomorrow.getTime() / 1000, profile.getExpirationDate().getTime() / 1000);
final Map<String, Object> claims2 = jwtAuthenticator.validateTokenAndGetClaims(token);
assertEquals(VALUE, claims2.get(JwtClaims.SUBJECT));
assertEquals(tomorrow.getTime() / 1000, ((Date) claims2.get(JwtClaims.EXPIRATION_TIME)).getTime() / 1000);
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project cas by apereo.
the class TokenAuthenticationHandlerTests method verifyKeysAreSane.
@Test
public void verifyKeysAreSane() throws Exception {
val g = new JwtGenerator();
g.setSignatureConfiguration(new SecretSignatureConfiguration(SIGNING_SECRET, JWSAlgorithm.HS256));
g.setEncryptionConfiguration(new SecretEncryptionConfiguration(ENCRYPTION_SECRET, JWEAlgorithm.DIR, EncryptionMethod.A192CBC_HS384));
val profile = new CommonProfile();
profile.setId("casuser");
val token = g.generate(profile);
val c = new TokenCredential(token, RegisteredServiceTestUtils.getService());
val result = this.tokenAuthenticationHandler.authenticate(c);
assertNotNull(result);
assertEquals(result.getPrincipal().getId(), profile.getId());
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testJwtGenerationA256GCM.
@Test
public void testJwtGenerationA256GCM() {
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(MAC_SECRET));
((SecretEncryptionConfiguration) g.getEncryptionConfiguration()).setMethod(EncryptionMethod.A256GCM);
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 testDoubleGenerateAuthenticate.
@Test
public void testDoubleGenerateAuthenticate() {
final JwtGenerator<FacebookProfile> generator = new JwtGenerator<>(new SecretSignatureConfiguration(MAC_SECRET), new SecretEncryptionConfiguration(MAC_SECRET));
final FacebookProfile profile = createProfile();
final String token = generator.generate(profile);
final JwtAuthenticator authenticator = new JwtAuthenticator(new SecretSignatureConfiguration(MAC_SECRET), new SecretEncryptionConfiguration(MAC_SECRET));
final TokenCredentials credentials = new TokenCredentials(token);
authenticator.validate(credentials, null);
final FacebookProfile profile2 = (FacebookProfile) credentials.getUserProfile();
generator.generate(profile2);
}
use of org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration in project pac4j by pac4j.
the class JwtTests method testGenerateAuthenticateDifferentEncryptionConfiguration.
@Test
public void testGenerateAuthenticateDifferentEncryptionConfiguration() {
final JwtGenerator<FacebookProfile> generator = new JwtGenerator<>();
generator.setEncryptionConfiguration(new SecretEncryptionConfiguration(KEY2));
final FacebookProfile profile = createProfile();
final String token = generator.generate(profile);
final JwtAuthenticator jwtAuthenticator = new JwtAuthenticator();
jwtAuthenticator.addEncryptionConfiguration(new SecretEncryptionConfiguration(MAC_SECRET));
final Exception e = TestsHelper.expectException(() -> assertToken(profile, token, jwtAuthenticator));
assertTrue(e.getMessage().startsWith("No encryption algorithm found for JWT:"));
}
Aggregations