|
2 | 2 |
|
3 | 3 | import com.genexus.gam.utils.keys.PrivateKeyUtil; |
4 | 4 | import com.genexus.gam.utils.keys.PublicKeyUtil; |
5 | | -import com.nimbusds.jose.JWSAlgorithm; |
6 | | -import com.nimbusds.jose.JWSHeader; |
7 | | -import com.nimbusds.jose.JWSSigner; |
8 | | -import com.nimbusds.jose.JWSVerifier; |
| 5 | +import com.nimbusds.jose.*; |
9 | 6 | import com.nimbusds.jose.crypto.MACSigner; |
10 | 7 | import com.nimbusds.jose.crypto.MACVerifier; |
11 | 8 | import com.nimbusds.jose.crypto.RSASSASigner; |
|
17 | 14 |
|
18 | 15 | import java.security.interfaces.RSAPrivateKey; |
19 | 16 | import java.security.interfaces.RSAPublicKey; |
| 17 | +import java.text.ParseException; |
| 18 | +import java.util.Objects; |
20 | 19 |
|
21 | 20 | public class Jwt { |
22 | 21 |
|
23 | 22 | private static final Logger logger = LogManager.getLogger(Jwt.class); |
24 | 23 |
|
25 | 24 | /******** EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/ |
26 | 25 |
|
27 | | - public static boolean verify(String path, String alias, String password, String token, String secret, boolean isSymmetric) { |
| 26 | + public static boolean verify(String path, String alias, String password, String token) { |
28 | 27 | logger.debug("verify"); |
29 | 28 | try { |
30 | | - return !isSymmetric ? verify_internal(PublicKeyUtil.getPublicKey(path, alias, password, token), token, "", isSymmetric) : verify_internal(null, token, secret, isSymmetric); |
| 29 | + return verify_internal(path, alias, password, token); |
31 | 30 | } catch (Exception e) { |
32 | 31 | logger.error("verify", e); |
33 | 32 | return false; |
34 | 33 | } |
35 | 34 | } |
36 | 35 |
|
37 | | - public static String create(String path, String alias, String password, String payload, String header, String secret, boolean isSymmetric) { |
| 36 | + public static String create(String path, String alias, String password, String payload, String header) { |
38 | 37 | logger.debug("create"); |
39 | 38 | try { |
40 | | - return !isSymmetric ? create_internal(PrivateKeyUtil.getPrivateKey(path, alias, password), payload, header, "", isSymmetric): create_internal(null, payload, header, secret, isSymmetric); |
| 39 | + return create_internal(path, alias, password, payload, header); |
41 | 40 | }catch (Exception e) |
42 | 41 | { |
43 | 42 | logger.error("create", e); |
@@ -79,29 +78,25 @@ public static boolean verifyAlgorithm(String algorithm, String token) |
79 | 78 |
|
80 | 79 | /******** EXTERNAL OBJECT PUBLIC METHODS - END ********/ |
81 | 80 |
|
82 | | - private static boolean verify_internal(RSAPublicKey publicKey, String token, String secret, boolean isSymmetric){ |
| 81 | + private static boolean verify_internal(String path, String alias, String password, String token) throws JOSEException, ParseException { |
83 | 82 | logger.debug("verify_internal"); |
84 | | - try { |
85 | | - SignedJWT signedJWT = SignedJWT.parse(token); |
86 | | - JWSVerifier verifier = isSymmetric ? new MACVerifier(secret):new RSASSAVerifier(publicKey); |
87 | | - return signedJWT.verify(verifier); |
88 | | - } catch (Exception e) { |
89 | | - logger.error("verify_internal", e); |
90 | | - return false; |
91 | | - } |
| 83 | + JWTAlgorithm algorithm = JWTAlgorithm.getJWTAlgoritm(JWSHeader.parse(getHeader(token)).getAlgorithm().getName()); |
| 84 | + assert algorithm != null; |
| 85 | + boolean isSymmetric = JWTAlgorithm.isSymmetric(algorithm); |
| 86 | + SignedJWT signedJWT = SignedJWT.parse(token); |
| 87 | + JWSVerifier verifier = isSymmetric ? new MACVerifier(password):new RSASSAVerifier(Objects.requireNonNull(PublicKeyUtil.getPublicKey(path, alias, password, token))); |
| 88 | + return signedJWT.verify(verifier); |
92 | 89 | } |
93 | 90 |
|
94 | | - private static String create_internal(RSAPrivateKey privateKey, String payload, String header, String secret, boolean isSymmetric) { |
| 91 | + private static String create_internal(String path, String alias, String password, String payload, String header) throws Exception { |
95 | 92 | logger.debug("create_internal"); |
96 | | - try { |
97 | | - SignedJWT signedJWT = new SignedJWT(JWSHeader.parse(header), JWTClaimsSet.parse(payload)); |
98 | | - JWSSigner signer = isSymmetric ? new MACSigner(secret): new RSASSASigner(privateKey); |
99 | | - signedJWT.sign(signer); |
100 | | - return signedJWT.serialize(); |
101 | | - } catch (Exception e) { |
102 | | - logger.error("create_internal", e); |
103 | | - return ""; |
104 | | - } |
| 93 | + JWSHeader parsedHeader = JWSHeader.parse(header); |
| 94 | + JWTAlgorithm algorithm = JWTAlgorithm.getJWTAlgoritm(parsedHeader.getAlgorithm().getName()); |
| 95 | + assert algorithm != null; |
| 96 | + boolean isSymmetric = JWTAlgorithm.isSymmetric(algorithm); |
| 97 | + SignedJWT signedJWT = new SignedJWT(parsedHeader, JWTClaimsSet.parse(payload)); |
| 98 | + JWSSigner signer = isSymmetric ? new MACSigner(password): new RSASSASigner(Objects.requireNonNull(PrivateKeyUtil.getPrivateKey(path, alias, password))); |
| 99 | + signedJWT.sign(signer); |
| 100 | + return signedJWT.serialize(); |
105 | 101 | } |
106 | | - |
107 | 102 | } |
0 commit comments