99import io .jsonwebtoken .Claims ;
1010import io .jsonwebtoken .JwtException ;
1111import io .jsonwebtoken .Jwts ;
12- import io .jsonwebtoken .SignatureAlgorithm ;
1312import org .apiguardian .api .API ;
1413import org .bouncycastle .asn1 .pkcs .RSAPrivateKey ;
1514import org .bouncycastle .crypto .params .RSAPrivateCrtKeyParameters ;
2322import java .io .StringReader ;
2423import java .nio .charset .StandardCharsets ;
2524import java .security .GeneralSecurityException ;
26- import java .security .Key ;
2725import java .security .KeyFactory ;
2826import java .security .NoSuchAlgorithmException ;
2927import java .security .PrivateKey ;
@@ -65,7 +63,7 @@ public class JwtHelper {
6563
6664
6765 /**
68- * Creates a JWT with the provided user name and expiration date, signed with the provided private key.
66+ * Creates a JWT with the provided username and expiration date, signed with the provided private key.
6967 *
7068 * @param user the username to authenticate; will be verified by the pod
7169 * @param expiration of the authentication request in milliseconds; cannot be longer than the value defined on the
@@ -75,16 +73,16 @@ public class JwtHelper {
7573 * the public key stored for the user
7674 * @return a signed JWT for a specific user (or subject)
7775 */
78- public static String createSignedJwt (String user , long expiration , Key privateKey ) {
76+ public static String createSignedJwt (String user , long expiration , PrivateKey privateKey ) {
7977 return Jwts .builder ()
80- .setSubject (user )
81- .setExpiration (new Date (System .currentTimeMillis () + expiration ))
82- .signWith (SignatureAlgorithm . RS512 , privateKey )
78+ .subject (user )
79+ .expiration (new Date (System .currentTimeMillis () + expiration ))
80+ .signWith (privateKey , Jwts . SIG . RS512 )
8381 .compact ();
8482 }
8583
8684 /**
87- * Creates a RSA Private Key from a PEM String. It supports PKCS#1 and PKCS#8 string formats.
85+ * Creates an RSA Private Key from a PEM String. It supports PKCS#1 and PKCS#8 string formats.
8886 *
8987 * @param pemPrivateKey RSA Private Key content
9088 * @return a {@link PrivateKey} instance
@@ -110,18 +108,21 @@ else if (pemPrivateKey.contains(PEM_RSA_PRIVATE_START)) {
110108 /**
111109 * Validates a jwt against a certificate.
112110 *
113- * @param jwt
111+ * @param jwt string of the jwt to be validated
114112 * @param certificate string of the X.509 certificate content in pem format.
115- * @return the content of jwt clain "user" if jwt is successfully validated.
113+ * @return the content of jwt claim "user" if jwt is successfully validated.
116114 * @throws AuthInitializationException if certificate or jwt are invalid.
117115 */
118116 public static UserClaim validateJwt (String jwt , String certificate ) throws AuthInitializationException {
119117 final Certificate x509Certificate = parseX509Certificate (certificate );
120118
121119 try {
122- final Claims body = Jwts .parser ().setSigningKey (x509Certificate .getPublicKey ())
123- .parseClaimsJws (jwt ).getBody ();
124- return mapper .convertValue (body .get ("user" ), UserClaim .class );
120+ final Claims claims = Jwts .parser ()
121+ .verifyWith (x509Certificate .getPublicKey ())
122+ .build ()
123+ .parseSignedClaims (jwt )
124+ .getPayload ();
125+ return mapper .convertValue (claims .get ("user" ), UserClaim .class );
125126 } catch (JwtException e ) {
126127 throw new AuthInitializationException ("Unable to validate JWT" , e );
127128 }
0 commit comments