Search in sources :

Example 96 with JwtClaims

use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.

the class JwtVerifierTest method testVerifyJwt.

@Test
public void testVerifyJwt() throws Exception {
    JwtClaims claims = ClaimsUtil.getTestClaims("steve", "EMPLOYEE", "f7d42348-c647-4efb-a52d-4c5787421e72", Arrays.asList("write:pets", "read:pets"), "user");
    String jwt = JwtIssuer.getJwt(claims);
    claims = null;
    Assert.assertNotNull(jwt);
    JwtVerifier jwtVerifier = new JwtVerifier(Config.getInstance().getJsonMapConfig(CONFIG_NAME));
    try {
        claims = jwtVerifier.verifyJwt(jwt, false, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertNotNull(claims);
    Assert.assertEquals("steve", claims.getStringClaimValue(Constants.USER_ID_STRING));
    try {
        claims = jwtVerifier.verifyJwt(jwt, false, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("jwtClaims = " + claims);
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims) KeyStoreException(java.security.KeyStoreException) JoseException(org.jose4j.lang.JoseException) Test(org.junit.Test)

Example 97 with JwtClaims

use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.

the class Http2ClientPoolIT method isTokenExpired.

private static boolean isTokenExpired(String authorization) {
    boolean expired = false;
    String jwt = getJwtFromAuthorization(authorization);
    if (jwt != null) {
        try {
            JwtConsumer consumer = new JwtConsumerBuilder().setSkipAllValidators().setDisableRequireSignature().setSkipSignatureVerification().build();
            JwtContext jwtContext = consumer.process(jwt);
            JwtClaims jwtClaims = jwtContext.getJwtClaims();
            try {
                if ((NumericDate.now().getValue() - 60) >= jwtClaims.getExpirationTime().getValue()) {
                    expired = true;
                }
            } catch (MalformedClaimException e) {
                logger.error("MalformedClaimException:", e);
            }
        } catch (InvalidJwtException e) {
            e.printStackTrace();
        }
    }
    return expired;
}
Also used : InvalidJwtException(org.jose4j.jwt.consumer.InvalidJwtException) MalformedClaimException(org.jose4j.jwt.MalformedClaimException) JwtClaims(org.jose4j.jwt.JwtClaims) JwtConsumerBuilder(org.jose4j.jwt.consumer.JwtConsumerBuilder) JwtConsumer(org.jose4j.jwt.consumer.JwtConsumer) JwtContext(org.jose4j.jwt.consumer.JwtContext)

Example 98 with JwtClaims

use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.

the class Http2ClientPoolIT method getJwt.

private static String getJwt(int expiredInSeconds) throws Exception {
    JwtClaims claims = getTestClaims();
    claims.setExpirationTime(NumericDate.fromMilliseconds(System.currentTimeMillis() + expiredInSeconds * 1000));
    return getJwt(claims);
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims)

Example 99 with JwtClaims

use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.

the class LightProxyHandler method extractClaimsFromJwt.

/**
 * Takes in the header values from the request as a headerMap.
 * Grab the JWT from the auth header, then extract and return the claims.
 *
 * @param headerValues - the header values from the request
 * @return - the claims from the token
 */
private JwtClaims extractClaimsFromJwt(HeaderMap headerValues) {
    // make sure request actually contained authentication header value
    if (headerValues.get(Headers.AUTHORIZATION_STRING) != null) {
        String jwt = String.valueOf(headerValues.get(Headers.AUTHORIZATION_STRING)).split(" ")[1];
        JwtConsumer jwtConsumer = new JwtConsumerBuilder().setSkipSignatureVerification().setSkipAllDefaultValidators().setAllowedClockSkewInSeconds(LONG_CLOCK_SKEW).build();
        JwtClaims jwtClaims = null;
        try {
            jwtClaims = jwtConsumer.processToClaims(jwt);
        } catch (InvalidJwtException e) {
            e.printStackTrace();
        }
        return jwtClaims;
    } else {
        return new JwtClaims();
    }
}
Also used : InvalidJwtException(org.jose4j.jwt.consumer.InvalidJwtException) JwtClaims(org.jose4j.jwt.JwtClaims) JwtConsumerBuilder(org.jose4j.jwt.consumer.JwtConsumerBuilder) JwtConsumer(org.jose4j.jwt.consumer.JwtConsumer) HttpString(io.undertow.util.HttpString)

Example 100 with JwtClaims

use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.

the class ClaimsUtil method getTestCcClaimsScopeScp.

public static JwtClaims getTestCcClaimsScopeScp(String clientId, String scope) {
    JwtClaims claims = JwtIssuer.getDefaultJwtClaims();
    claims.setClaim("client_id", clientId);
    claims.setClaim("scp", scope);
    return claims;
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims)

Aggregations

JwtClaims (org.jose4j.jwt.JwtClaims)130 Test (org.junit.Test)47 JwtConsumer (org.jose4j.jwt.consumer.JwtConsumer)23 JwtConsumerBuilder (org.jose4j.jwt.consumer.JwtConsumerBuilder)23 InvalidJwtException (org.jose4j.jwt.consumer.InvalidJwtException)21 MalformedClaimException (org.jose4j.jwt.MalformedClaimException)19 JoseException (org.jose4j.lang.JoseException)17 lombok.val (lombok.val)15 JsonWebSignature (org.jose4j.jws.JsonWebSignature)15 Map (java.util.Map)14 JwtContext (org.jose4j.jwt.consumer.JwtContext)11 NumericDate (org.jose4j.jwt.NumericDate)9 JsonWebStructure (org.jose4j.jwx.JsonWebStructure)9 HashMap (java.util.HashMap)7 KeyStoreException (java.security.KeyStoreException)6 ArrayList (java.util.ArrayList)5 OidcRegisteredService (org.apereo.cas.services.OidcRegisteredService)5 ExpiredTokenException (com.networknt.exception.ExpiredTokenException)4 JwksVerificationKeyResolver (org.jose4j.keys.resolvers.JwksVerificationKeyResolver)4 Test (org.junit.jupiter.api.Test)4