Search in sources :

Example 1 with JwtProfile

use of org.pac4j.jwt.profile.JwtProfile 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);
}
Also used : JwtGenerator(org.pac4j.jwt.profile.JwtGenerator) JwtAuthenticator(org.pac4j.jwt.credentials.authenticator.JwtAuthenticator) SecretEncryptionConfiguration(org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration) JwtProfile(org.pac4j.jwt.profile.JwtProfile) SecretSignatureConfiguration(org.pac4j.jwt.config.signature.SecretSignatureConfiguration) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 SecretEncryptionConfiguration (org.pac4j.jwt.config.encryption.SecretEncryptionConfiguration)1 SecretSignatureConfiguration (org.pac4j.jwt.config.signature.SecretSignatureConfiguration)1 JwtAuthenticator (org.pac4j.jwt.credentials.authenticator.JwtAuthenticator)1 JwtGenerator (org.pac4j.jwt.profile.JwtGenerator)1 JwtProfile (org.pac4j.jwt.profile.JwtProfile)1