Skip to content

Commit 5311f63

Browse files
committed
GamUtilsEO simplification and module refactor + tests
(cherry picked from commit 055badf)
1 parent a163e6c commit 5311f63

File tree

13 files changed

+313
-471
lines changed

13 files changed

+313
-471
lines changed

gamutils/src/main/java/com/genexus/gam/GamUtilsEO.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import com.genexus.gam.utils.cryptography.Encryption;
55
import com.genexus.gam.utils.cryptography.Hash;
66
import com.genexus.gam.utils.json.Jwk;
7-
import com.genexus.gam.utils.json.Jwks;
87
import com.genexus.gam.utils.json.Jwt;
8+
import com.genexus.gam.utils.json.UnixTimestamp;
9+
10+
import java.util.Date;
911

1012
public class GamUtilsEO {
1113

@@ -45,27 +47,25 @@ public static String getPublicJwk(String jwkString) {
4547
return Jwk.getPublic(jwkString);
4648
}
4749

48-
public static boolean jwk_verifyJWT(String jwkString, String token) {
49-
return Jwk.verifyJWT(jwkString, token);
50+
//**JWT**//
51+
public static boolean verifyJwt(String path, String alias, String password, String token) {
52+
return Jwt.verify(path, alias, password, token);
5053
}
5154

52-
public static String jwk_createJwt(String jwkString, String payload, String header) {
53-
return Jwk.createJwt(jwkString, payload, header);
55+
public static String createJwt(String path, String alias, String password, String payload, String header) {
56+
return Jwt.create(path, alias, password, payload, header);
5457
}
5558

56-
//**JWKS**//
57-
58-
public static boolean jwks_verifyJWT(String jwksString, String token, String kid) {
59-
return Jwks.verifyJWT(jwksString, token, kid);
59+
public static long createUnixTimestamp(Date date) {
60+
return UnixTimestamp.create(date);
6061
}
6162

62-
//**JWT**//
63-
public static boolean verifyJWTWithFile(String path, String alias, String password, String token) {
64-
return Jwt.verify(path, alias, password, token);
63+
public static String getJwtHeader(String token) {
64+
return Jwt.getHeader(token);
6565
}
6666

67-
public static String createJWTWithFile(String path, String alias, String password, String payload, String header) {
68-
return Jwt.create(path, alias, password, payload, header);
67+
public static String getJwtPayload(String token) {
68+
return Jwt.getPayload(token);
6969
}
7070

7171
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/

gamutils/src/main/java/com/genexus/gam/utils/json/Jwk.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static String getPublic(String jwkString) {
4444
}
4545
}
4646

47-
public static boolean verifyJWT(String jwkString, String token) {
47+
/*public static boolean verifyJWT(String jwkString, String token) {
4848
if (jwkString.isEmpty()) {
4949
logger.error("verifyJWT jwkString parameter is empty");
5050
return false;
@@ -83,5 +83,5 @@ public static String createJwt(String jwkString, String payload, String header)
8383
logger.error("createJwt", e);
8484
return "";
8585
}
86-
}
86+
}*/
8787
}

gamutils/src/main/java/com/genexus/gam/utils/json/Jwks.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.genexus.gam.utils.json;
22

3-
import com.genexus.gam.utils.keys.CertificateUtil;
43
import com.genexus.gam.utils.keys.PrivateKeyUtil;
4+
import com.genexus.gam.utils.keys.PublicKeyUtil;
55
import com.nimbusds.jose.JWSHeader;
66
import com.nimbusds.jose.JWSVerifier;
77
import com.nimbusds.jose.crypto.RSASSASigner;
@@ -16,42 +16,73 @@
1616

1717
public class Jwt {
1818

19-
private static Logger logger = LogManager.getLogger(Jwt.class);
19+
private static final Logger logger = LogManager.getLogger(Jwt.class);
2020

2121
/******** EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
2222

23-
public static boolean verify(RSAPublicKey publicKey, String token) {
23+
public static boolean verify(String path, String alias, String password, String token) {
24+
logger.debug("verify");
2425
try {
25-
SignedJWT signedJWT = SignedJWT.parse(token);
26-
JWSVerifier verifier = new RSASSAVerifier(publicKey);
27-
return signedJWT.verify(verifier);
26+
return verify(PublicKeyUtil.getPublicKey(path, alias, password, token), token);
2827
} catch (Exception e) {
2928
logger.error("verify", e);
3029
return false;
3130
}
3231
}
3332

34-
public static String create(RSAPrivateKey privateKey, String payload, String header) {
33+
public static String create(String path, String alias, String password, String payload, String header) {
34+
logger.debug("create");
3535
try {
36-
SignedJWT signedJWT = new SignedJWT(JWSHeader.parse(header), JWTClaimsSet.parse(payload));
37-
signedJWT.sign(new RSASSASigner(privateKey));
38-
return signedJWT.serialize();
39-
} catch (Exception e) {
36+
return create(PrivateKeyUtil.getPrivateKey(path, alias, password), payload, header);
37+
}catch (Exception e)
38+
{
4039
logger.error("create", e);
4140
return "";
4241
}
4342
}
4443

45-
public static boolean verify(String path, String alias, String password, String token) {
46-
return verify((RSAPublicKey) CertificateUtil.getCertificate(path, alias, password).getPublicKey(), token);
44+
public static String getHeader(String token) {
45+
logger.debug("getHeader");
46+
try {
47+
return SignedJWT.parse(token).getHeader().toString();
48+
} catch (Exception e) {
49+
logger.error("getHeader", e);
50+
return "";
51+
}
4752
}
4853

49-
public static String create(String path, String alias, String password, String payload, String header)
50-
{
51-
return create(PrivateKeyUtil.getPrivateKey(path, alias, password), payload, header);
54+
public static String getPayload(String token) {
55+
logger.debug("getPayload");
56+
try {
57+
return SignedJWT.parse(token).getPayload().toString();
58+
} catch (Exception e) {
59+
logger.error("getPayload", e);
60+
return "";
61+
}
5262
}
5363

5464
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
5565

66+
private static boolean verify(RSAPublicKey publicKey, String token) {
67+
try {
68+
SignedJWT signedJWT = SignedJWT.parse(token);
69+
JWSVerifier verifier = new RSASSAVerifier(publicKey);
70+
return signedJWT.verify(verifier);
71+
} catch (Exception e) {
72+
logger.error("verify", e);
73+
return false;
74+
}
75+
}
76+
77+
private static String create(RSAPrivateKey privateKey, String payload, String header) {
78+
try {
79+
SignedJWT signedJWT = new SignedJWT(JWSHeader.parse(header), JWTClaimsSet.parse(payload));
80+
signedJWT.sign(new RSASSASigner(privateKey));
81+
return signedJWT.serialize();
82+
} catch (Exception e) {
83+
logger.error("create", e);
84+
return "";
85+
}
86+
}
5687

5788
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.genexus.gam.utils.json;
2+
3+
import java.util.Date;
4+
5+
public class UnixTimestamp {
6+
7+
public static long create(Date gxdate) {
8+
return gxdate.toInstant().getEpochSecond();
9+
}
10+
11+
}

gamutils/src/main/java/com/genexus/gam/utils/keys/PrivateKeyUtil.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.genexus.gam.utils.keys;
22

3+
import com.nimbusds.jose.jwk.JWK;
34
import org.apache.commons.io.FilenameUtils;
45
import org.apache.logging.log4j.LogManager;
56
import org.apache.logging.log4j.Logger;
@@ -24,10 +25,11 @@
2425
import java.security.Security;
2526
import java.security.interfaces.RSAPrivateKey;
2627
import java.security.spec.PKCS8EncodedKeySpec;
28+
import java.util.Objects;
2729

2830
public enum PrivateKeyUtil {
2931

30-
pfx, jks, pkcs12, p12, pem, key, b64;
32+
pfx, jks, pkcs12, p12, pem, key, b64, json;
3133

3234
private static Logger logger = LogManager.getLogger(PrivateKeyUtil.class);
3335

@@ -47,16 +49,17 @@ public static PrivateKeyUtil value(String ext) {
4749
return key;
4850
case "b64":
4951
return b64;
52+
case "json":
53+
return json;
5054
default:
51-
logger.error("Invalid certificate file extension");
55+
logger.error("Invalid private key file extension");
5256
return null;
5357
}
5458
}
5559

56-
public static RSAPrivateKey getPrivateKey(String path, String alias, String password) {
57-
String extension = FilenameUtils.getExtension(path);
58-
PrivateKeyUtil ext = extension.isEmpty() ? PrivateKeyUtil.value("b64") : PrivateKeyUtil.value(extension);
59-
switch (ext) {
60+
public static RSAPrivateKey getPrivateKey(String path, String alias, String password) throws Exception{
61+
PrivateKeyUtil ext = PrivateKeyUtil.value(fixType(path));
62+
switch (Objects.requireNonNull(ext)) {
6063
case pfx:
6164
case jks:
6265
case pkcs12:
@@ -67,12 +70,37 @@ public static RSAPrivateKey getPrivateKey(String path, String alias, String pass
6770
return loadFromPkcs8(path, password);
6871
case b64:
6972
return loadFromBase64(path);
73+
case json:
74+
return loadFromJson(path);
7075
default:
7176
logger.error("Invalid private key file extension");
7277
return null;
7378
}
7479
}
7580

81+
private static RSAPrivateKey loadFromJson(String json)
82+
{
83+
logger.debug("loadFromJson");
84+
try {
85+
JWK jwk = JWK.parse(json);
86+
return jwk.toRSAKey().toRSAPrivateKey();
87+
} catch (Exception e) {
88+
logger.error("loadFromJson", e);
89+
return null;
90+
}
91+
}
92+
93+
private static String fixType(String input)
94+
{
95+
try {
96+
String extension = FilenameUtils.getExtension(input);
97+
return extension.isEmpty() ? "b64" : extension;
98+
}catch (IllegalArgumentException e)
99+
{
100+
return "json";
101+
}
102+
}
103+
76104
private static RSAPrivateKey loadFromBase64(String base64) {
77105
logger.debug("loadFromBase64");
78106
try (ASN1InputStream stream = new ASN1InputStream(Base64.decode(base64))) {

0 commit comments

Comments
 (0)