Search in sources :

Example 26 with JwtClaims

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

the class OauthHelperTest method getTestClaims.

private static JwtClaims getTestClaims() {
    JwtClaims claims = new JwtClaims();
    claims.setIssuer("urn:com:networknt:oauth2:v1");
    claims.setAudience("urn:com.networknt");
    claims.setExpirationTimeMinutesInTheFuture(10);
    // a unique identifier for the token
    claims.setGeneratedJwtId();
    // when the token was issued/created (now)
    claims.setIssuedAtToNow();
    // time before which the token is not yet valid (2 minutes ago)
    claims.setNotBeforeMinutesInThePast(2);
    claims.setClaim("version", "1.0");
    claims.setClaim("user_id", "steve");
    claims.setClaim("user_type", "EMPLOYEE");
    claims.setClaim("client_id", "aaaaaaaa-1234-1234-1234-bbbbbbbb");
    List<String> scope = Arrays.asList("api.r", "api.w");
    // multi-valued claims work too and will end up as a JSON array
    claims.setStringListClaim("scope", scope);
    return claims;
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims)

Example 27 with JwtClaims

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

the class Http2ClientIT 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 28 with JwtClaims

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

the class Http2ClientPoolIT method getTestClaims.

private static JwtClaims getTestClaims() {
    JwtClaims claims = new JwtClaims();
    claims.setIssuer("urn:com:networknt:oauth2:v1");
    claims.setAudience("urn:com.networknt");
    claims.setExpirationTimeMinutesInTheFuture(10);
    // a unique identifier for the token
    claims.setGeneratedJwtId();
    // when the token was issued/created (now)
    claims.setIssuedAtToNow();
    // time before which the token is not yet valid (2 minutes ago)
    claims.setNotBeforeMinutesInThePast(2);
    claims.setClaim("version", "1.0");
    claims.setClaim("user_id", "steve");
    claims.setClaim("user_type", "EMPLOYEE");
    claims.setClaim("client_id", "aaaaaaaa-1234-1234-1234-bbbbbbbb");
    List<String> scope = Arrays.asList("api.r", "api.w");
    // multi-valued claims work too and will end up as a JSON array
    claims.setStringListClaim("scope", scope);
    return claims;
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims)

Example 29 with JwtClaims

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

the class JwtIssuerTest method GroupToRoleAccessControlWrong.

/**
 * The returned token contains groups User_API_Wrong for controller-group-role rule
 * @throws Exception
 */
@Test
public void GroupToRoleAccessControlWrong() throws Exception {
    JwtClaims claims = ClaimsUtil.getTestClaimsGroup("stevehu", "EMPLOYEE", "f7d42348-c647-4efb-a52d-4c5787421e72", Arrays.asList("portal.r", "portal.w"), "User_API_Wrong");
    claims.setExpirationTimeMinutesInTheFuture(5256000);
    String jwt = JwtIssuer.getJwt(claims);
    System.out.println("***Long lived token Authorization code customer with a wrong controller groups that cannot be converted to roles ***: " + jwt);
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims) Test(org.junit.Test)

Example 30 with JwtClaims

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

the class JwtIssuerTest method sidecarReferenceBootstrapWithServiceId.

/**
 * This token is used to connect to the light-config-server with serviceId 0100 for testing with a service specific for a client.
 * @throws Exception
 */
@Test
public void sidecarReferenceBootstrapWithServiceId() throws Exception {
    JwtClaims claims = ClaimsUtil.getTestCcClaimsScopeService("f7d42348-c647-4efb-a52d-4c5787421e72", "A8E73740C0041C03D67C3A951AA1D7533C8F9F2FB57D7BA107210B9BC9E06DA2", "com.networknt.petstore-1.0.0");
    claims.setExpirationTimeMinutesInTheFuture(5256000);
    String jwt = JwtIssuer.getJwt(claims);
    System.out.println("***Reference Long lived Bootstrap token for config server and controller: " + jwt);
}
Also used : JwtClaims(org.jose4j.jwt.JwtClaims) Test(org.junit.Test)

Aggregations

JwtClaims (org.jose4j.jwt.JwtClaims)210 Test (org.junit.Test)90 JsonWebSignature (org.jose4j.jws.JsonWebSignature)51 InvalidJwtException (org.jose4j.jwt.consumer.InvalidJwtException)25 JwtConsumer (org.jose4j.jwt.consumer.JwtConsumer)25 JwtConsumerBuilder (org.jose4j.jwt.consumer.JwtConsumerBuilder)25 JsonWebEncryption (org.jose4j.jwe.JsonWebEncryption)22 MalformedClaimException (org.jose4j.jwt.MalformedClaimException)21 JoseException (org.jose4j.lang.JoseException)19 Map (java.util.Map)18 lombok.val (lombok.val)15 Test (org.junit.jupiter.api.Test)15 JwtContext (org.jose4j.jwt.consumer.JwtContext)12 DefaultJWTCallerPrincipal (io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal)11 HashMap (java.util.HashMap)9 NumericDate (org.jose4j.jwt.NumericDate)9 JsonWebStructure (org.jose4j.jwx.JsonWebStructure)9 JsonWebToken (org.eclipse.microprofile.jwt.JsonWebToken)8 SecretKey (javax.crypto.SecretKey)7 KeyStoreException (java.security.KeyStoreException)6