Skip to content

Commit

Permalink
fixes #2070 update claim version to ver (#2071)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu authored Jan 9, 2024
1 parent c6ce781 commit 3490d5e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
6 changes: 2 additions & 4 deletions security/src/main/java/com/networknt/security/JwtIssuer.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public static String getJwt(JwtClaims claims, String kid, PrivateKey privateKey)
* @return JwtClaims
*/
public static JwtClaims getDefaultJwtClaims() {

JwtClaims claims = new JwtClaims();

claims.setIssuer(jwtConfig.getIssuer());
Expand All @@ -88,7 +87,7 @@ public static JwtClaims getDefaultJwtClaims() {
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
claims.setClaim("version", jwtConfig.getVersion());
claims.setClaim("ver", jwtConfig.getVersion());
return claims;

}
Expand All @@ -99,7 +98,6 @@ public static JwtClaims getDefaultJwtClaims() {
* @return JwtClaims
*/
public static JwtClaims getJwtClaimsWithExpiresIn(int expiresIn) {

JwtClaims claims = new JwtClaims();

claims.setIssuer(jwtConfig.getIssuer());
Expand All @@ -108,7 +106,7 @@ public static JwtClaims getJwtClaimsWithExpiresIn(int expiresIn) {
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
claims.setClaim("version", jwtConfig.getVersion());
claims.setClaim("ver", jwtConfig.getVersion());
return claims;
}

Expand Down
36 changes: 36 additions & 0 deletions security/src/test/java/com/networknt/security/JwtIssuerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ public void longlivedCcLocalPortalWithScp() throws Exception {
System.out.println("***Long lived token for portal lightapi***: " + jwt);
}

/**
* The returned token contains scp as the key for the scope. Some OAuth 2.0 provider like Okta use this claim. All scopes are separated by comma.
* @throws Exception
*/
@Test
public void longlivedCcPetstoreWithScp() throws Exception {
JwtClaims claims = ClaimsUtil.getTestCcClaimsWithScp("f7d42348-c647-4efb-a52d-4c5787421e73", Arrays.asList("write:pets", "read:pets"));
claims.setExpirationTimeMinutesInTheFuture(5256000);
String jwt = JwtIssuer.getJwt(claims, long_kid, KeyUtil.deserializePrivateKey(long_key, KeyUtil.RSA));
System.out.println("***Long lived token for portal lightapi***: " + jwt);
}

/**
* The returned token contains scope as the key for the scope. All scopes are separated by space.
* @throws Exception
Expand All @@ -222,6 +234,30 @@ public void longlivedCcLocalPortalScope() throws Exception {
System.out.println("***Long lived token for portal lightapi***: " + jwt);
}

/**
* The returned token contains scope as the key for the scope. All scopes are separated by space.
* @throws Exception
*/
@Test
public void longlivedCcPetstoreScope() throws Exception {
JwtClaims claims = ClaimsUtil.getTestCcClaimsScope("f7d42348-c647-4efb-a52d-4c5787421e73", "write:pets read:pets");
claims.setExpirationTimeMinutesInTheFuture(5256000);
String jwt = JwtIssuer.getJwt(claims, long_kid, KeyUtil.deserializePrivateKey(long_key, KeyUtil.RSA));
System.out.println("***Long lived token for portal lightapi***: " + jwt);
}

/**
* The returned token contains scope as the key for the scope. All scopes are separated by space.
* @throws Exception
*/
@Test
public void longlivedCcPetstoreScpString() throws Exception {
JwtClaims claims = ClaimsUtil.getTestCcClaimsScopeScp("f7d42348-c647-4efb-a52d-4c5787421e73", "write:pets read:pets");
claims.setExpirationTimeMinutesInTheFuture(5256000);
String jwt = JwtIssuer.getJwt(claims, long_kid, KeyUtil.deserializePrivateKey(long_key, KeyUtil.RSA));
System.out.println("***Long lived token for portal lightapi***: " + jwt);
}

/**
* The returned token contains scope as the key for the scope. All scopes are separated by space.
* @throws Exception
Expand Down

0 comments on commit 3490d5e

Please sign in to comment.