Search in sources :

Example 16 with JwtContext

use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.

the class DefaultJWTTokenParser method parseClaims.

private JwtContext parseClaims(String token, JWTAuthContextInfo authContextInfo, ProtectionLevel level) throws ParseException {
    try {
        JwtConsumerBuilder builder = new JwtConsumerBuilder();
        if (level == ProtectionLevel.SIGN) {
            if (authContextInfo.getPublicVerificationKey() != null) {
                builder.setVerificationKey(authContextInfo.getPublicVerificationKey());
            } else if (authContextInfo.getSecretVerificationKey() != null) {
                builder.setVerificationKey(authContextInfo.getSecretVerificationKey());
            } else {
                builder.setVerificationKeyResolver(getVerificationKeyResolver(authContextInfo));
            }
            builder.setJwsAlgorithmConstraints(new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.PERMIT, authContextInfo.getSignatureAlgorithm().getAlgorithm()));
        } else {
            builder.setEnableRequireEncryption();
            builder.setDisableRequireSignature();
            if (authContextInfo.getPrivateDecryptionKey() != null) {
                builder.setDecryptionKey(authContextInfo.getPrivateDecryptionKey());
            } else if (authContextInfo.getSecretDecryptionKey() != null) {
                builder.setDecryptionKey(authContextInfo.getSecretDecryptionKey());
            } else {
                builder.setDecryptionKeyResolver(getDecryptionKeyResolver(authContextInfo));
            }
            builder.setJweAlgorithmConstraints(new AlgorithmConstraints(AlgorithmConstraints.ConstraintType.PERMIT, authContextInfo.getKeyEncryptionAlgorithm().getAlgorithm()));
        }
        builder.setRequireExpirationTime();
        final boolean issuedAtRequired = authContextInfo.getMaxTimeToLiveSecs() == null || authContextInfo.getMaxTimeToLiveSecs() > 0;
        if (issuedAtRequired) {
            builder.setRequireIssuedAt();
        }
        if (authContextInfo.getIssuedBy() != null) {
            builder.setExpectedIssuer(authContextInfo.getIssuedBy());
        }
        if (authContextInfo.getExpGracePeriodSecs() > 0) {
            builder.setAllowedClockSkewInSeconds(authContextInfo.getExpGracePeriodSecs());
        }
        setExpectedAudience(builder, authContextInfo);
        if (authContextInfo.isRelaxVerificationKeyValidation()) {
            builder.setRelaxVerificationKeyValidation();
        }
        JwtConsumer jwtConsumer = builder.build();
        // Validate the JWT and process it to the Claims
        JwtContext jwtContext = jwtConsumer.process(token);
        JwtClaims claimsSet = jwtContext.getJwtClaims();
        if (issuedAtRequired) {
            verifyIatAndExpAndTimeToLive(authContextInfo, claimsSet);
        }
        verifyRequiredClaims(authContextInfo, jwtContext);
        PrincipalUtils.setClaims(claimsSet, token, authContextInfo);
        if (authContextInfo.isRequireNamedPrincipal()) {
            checkNameClaims(jwtContext);
        }
        return jwtContext;
    } catch (InvalidJwtException e) {
        if (e.getCause() instanceof UnresolvableKeyException) {
            PrincipalLogging.log.verificationKeyUnresolvable();
            throw PrincipalMessages.msg.failedToVerifyToken(e.getCause());
        } else {
            PrincipalLogging.log.tokenInvalid();
            throw PrincipalMessages.msg.failedToVerifyToken(e);
        }
    } catch (UnresolvableKeyException e) {
        PrincipalLogging.log.verificationKeyUnresolvable();
        throw PrincipalMessages.msg.failedToVerifyToken(e);
    }
}
Also used : InvalidJwtException(org.jose4j.jwt.consumer.InvalidJwtException) JwtClaims(org.jose4j.jwt.JwtClaims) JwtConsumerBuilder(org.jose4j.jwt.consumer.JwtConsumerBuilder) UnresolvableKeyException(org.jose4j.lang.UnresolvableKeyException) JwtConsumer(org.jose4j.jwt.consumer.JwtConsumer) JwtContext(org.jose4j.jwt.consumer.JwtContext) AlgorithmConstraints(org.jose4j.jwa.AlgorithmConstraints)

Example 17 with JwtContext

use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.

the class DefaultJWTTokenParserTest method testParseMaxTimeToLiveEqualToExpAge.

@Test
public void testParseMaxTimeToLiveEqualToExpAge() throws Exception {
    config.setMaxTimeToLiveSecs(Long.valueOf(300));
    JwtContext context = parser.parse(TokenUtils.signClaims("/Token1.json"), config);
    assertNotNull(context);
}
Also used : JwtContext(org.jose4j.jwt.consumer.JwtContext) Test(org.junit.Test)

Example 18 with JwtContext

use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.

the class DefaultJWTTokenParserTest method testParseMaxTimeToLiveGreaterThanExpAge.

@Test
public void testParseMaxTimeToLiveGreaterThanExpAge() throws Exception {
    config.setMaxTimeToLiveSecs(Long.valueOf(301));
    JwtContext context = parser.parse(TokenUtils.signClaims("/Token1.json"), config);
    assertNotNull(context);
}
Also used : JwtContext(org.jose4j.jwt.consumer.JwtContext) Test(org.junit.Test)

Example 19 with JwtContext

use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.

the class DefaultJWTTokenParserTest method testParse.

@Test
public void testParse() throws Exception {
    JwtContext context = parser.parse(TokenUtils.signClaims("/Token1.json"), config);
    assertNotNull(context);
}
Also used : JwtContext(org.jose4j.jwt.consumer.JwtContext) Test(org.junit.Test)

Example 20 with JwtContext

use of org.jose4j.jwt.consumer.JwtContext in project smallrye-jwt by smallrye.

the class DefaultJWTTokenParserTest method testParseExpectedAudiencePresent.

@Test
public void testParseExpectedAudiencePresent() throws Exception {
    config.setExpectedAudience(Collections.singleton(TCK_TOKEN1_AUD));
    JwtContext context = parser.parse(TokenUtils.signClaims("/Token1.json"), config);
    assertNotNull(context);
}
Also used : JwtContext(org.jose4j.jwt.consumer.JwtContext) Test(org.junit.Test)

Aggregations

JwtContext (org.jose4j.jwt.consumer.JwtContext)22 JwtClaims (org.jose4j.jwt.JwtClaims)14 JwtConsumer (org.jose4j.jwt.consumer.JwtConsumer)14 InvalidJwtException (org.jose4j.jwt.consumer.InvalidJwtException)13 JwtConsumerBuilder (org.jose4j.jwt.consumer.JwtConsumerBuilder)13 MalformedClaimException (org.jose4j.jwt.MalformedClaimException)9 Test (org.junit.Test)6 AlgorithmConstraints (org.jose4j.jwa.AlgorithmConstraints)5 NumericDate (org.jose4j.jwt.NumericDate)4 JoseException (org.jose4j.lang.JoseException)3 ServiceException (io.jenkins.blueocean.commons.ServiceException)2 JWTAuthConfiguration (org.apache.tomee.microprofile.jwt.config.JWTAuthConfiguration)2 JsonWebStructure (org.jose4j.jwx.JsonWebStructure)2 JwksVerificationKeyResolver (org.jose4j.keys.resolvers.JwksVerificationKeyResolver)2 UnresolvableKeyException (org.jose4j.lang.UnresolvableKeyException)2 DigilibServletRequest (digilib.conf.DigilibServletRequest)1 JwtAuthenticationStore (io.jenkins.blueocean.auth.jwt.JwtAuthenticationStore)1 JwtToken (io.jenkins.blueocean.auth.jwt.JwtToken)1 SigningPublicKey (io.jenkins.blueocean.auth.jwt.SigningPublicKey)1 IOException (java.io.IOException)1