use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.
the class JwtHelperTest method testVerifySign.
@Test
public void testVerifySign() 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);
try {
claims = JwtHelper.verifyJwt(jwt, false, false);
} catch (Exception e) {
e.printStackTrace();
}
Assert.assertNotNull(claims);
Assert.assertEquals("steve", claims.getStringClaimValue(Constants.USER_ID_STRING));
try {
claims = JwtHelper.verifyJwt(jwt, false, false);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("jwtClaims = " + claims);
}
use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.
the class JwtHelperTest 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);
try {
claims = JwtHelper.verifyJwt(jwt, false);
} catch (Exception e) {
e.printStackTrace();
}
Assert.assertNotNull(claims);
Assert.assertEquals("steve", claims.getStringClaimValue(Constants.USER_ID_STRING));
try {
claims = JwtHelper.verifyJwt(jwt, false);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("jwtClaims = " + claims);
}
use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.
the class JwtVerifierTest method testVerifyJwtByJsonWebKeys.
@Test
public void testVerifyJwtByJsonWebKeys() throws Exception {
JwtConfig jwtConfig = (JwtConfig) Config.getInstance().getJsonObjectConfig(JwtIssuer.JWT_CONFIG, JwtConfig.class);
String fileName = jwtConfig.getKey().getFilename();
String alias = jwtConfig.getKey().getKeyName();
KeyStore ks = loadKeystore(fileName, jwtConfig.getKey().getPassword());
Key privateKey = ks.getKey(alias, jwtConfig.getKey().getPassword().toCharArray());
JsonWebSignature jws = new JsonWebSignature();
String iss = "my.test.iss";
JwtClaims jwtClaims = JwtClaims.parse("{\n" + " \"sub\": \"5745ed4b-0158-45ff-89af-4ce99bc6f4de\",\n" + " \"iss\": \"" + iss + "\",\n" + " \"subject_type\": \"client-id\",\n" + " \"exp\": 1557419531,\n" + " \"iat\": 1557419231,\n" + " \"scope\": [\n" + " \"my.test.scope.read\",\n" + " \"my.test.scope.write\",\n" + " ],\n" + " \"consumer_application_id\": \"389\",\n" + " \"request_transit\": \"63092\"\n" + "}");
// The payload of the JWS is JSON content of the JWT Claims
jws.setPayload(jwtClaims.toJson());
// use private key to sign the JWT
jws.setKey(privateKey);
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
String jwt = jws.getCompactSerialization();
Assert.assertNotNull(jwt);
System.out.print("JWT = " + jwt);
JwtVerifier jwtVerifier = new JwtVerifier(Config.getInstance().getJsonMapConfig(CONFIG_NAME));
JwtClaims claims = jwtVerifier.verifyJwt(jwt, true, true, (kId, isToken) -> {
try {
// use public key to create the the JsonWebKey
Key publicKey = ks.getCertificate(alias).getPublicKey();
PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(publicKey);
List<JsonWebKey> jwkList = Arrays.asList(jwk);
return new JwksVerificationKeyResolver(jwkList);
} catch (JoseException | KeyStoreException e) {
throw new RuntimeException(e);
}
});
Assert.assertNotNull(claims);
Assert.assertEquals(iss, claims.getStringClaimValue("iss"));
}
use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.
the class JwtVerifierTest method testVerifySign.
@Test
public void testVerifySign() 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, false);
} catch (Exception e) {
e.printStackTrace();
}
Assert.assertNotNull(claims);
Assert.assertEquals("steve", claims.getStringClaimValue(Constants.USER_ID_STRING));
try {
claims = jwtVerifier.verifyJwt(jwt, false, false);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("jwtClaims = " + claims);
}
use of org.jose4j.jwt.JwtClaims in project light-4j by networknt.
the class JwtVerifierTest method testVerifyToken.
@Test
public void testVerifyToken() 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);
}
Aggregations