Skip to content

Commit 4ef7fd3

Browse files
sgramponeBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:gamutils_eo' into beta
1 parent 8caeef5 commit 4ef7fd3

28 files changed

+1045
-507
lines changed

gamutils/pom.xml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@
1515
<properties>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
</properties>
18-
1918
<dependencies>
20-
<dependency>
21-
<groupId>${project.groupId}</groupId>
22-
<artifactId>gxcommon</artifactId>
23-
<version>${project.version}</version>
24-
</dependency>
2519
<dependency>
2620
<groupId>com.nimbusds</groupId>
2721
<artifactId>nimbus-jose-jwt</artifactId>
@@ -37,6 +31,21 @@
3731
<artifactId>bcpkix-jdk18on</artifactId>
3832
<version>1.78.1</version>
3933
</dependency>
34+
<dependency>
35+
<groupId>org.apache.logging.log4j</groupId>
36+
<artifactId>log4j-core</artifactId>
37+
<version>${log4j.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>commons-io</groupId>
41+
<artifactId>commons-io</artifactId>
42+
<version>2.11.0</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>${project.groupId}</groupId>
46+
<artifactId>gxcommon</artifactId>
47+
<version>${project.version}</version>
48+
</dependency>
4049
<dependency>
4150
<groupId>commons-codec</groupId>
4251
<artifactId>commons-codec</artifactId>

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
11
package com.genexus.gam;
22

3-
import com.genexus.gam.utils.*;
3+
import com.genexus.gam.utils.Encoding;
4+
import com.genexus.gam.utils.Random;
5+
import com.genexus.gam.utils.cryptography.Encryption;
6+
import com.genexus.gam.utils.cryptography.Hash;
7+
import com.genexus.gam.utils.json.Jwk;
8+
import com.genexus.gam.utils.json.Jwt;
9+
import com.genexus.gam.utils.json.UnixTimestamp;
10+
11+
import java.util.Date;
412

513
public class GamUtilsEO {
614

715
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
816

917
//**HASH**//
1018
public static String sha512(String plainText) {
11-
return Hash.sha512(plainText);
19+
return Hash.hash(plainText, Hash.SHA512);
20+
}
21+
22+
public static String sha256(String plainText) {
23+
return Hash.hash(plainText, Hash.SHA256);
24+
}
25+
26+
//**ENCRYPTION**//
27+
28+
public static String AesGcm(String input, String key, String nonce, int macSize, boolean toEncrypt) {
29+
return Encryption.AesGcm(input, key, nonce, macSize, toEncrypt);
1230
}
1331

1432
//**RANDOM**//
1533
public static String randomAlphanumeric(int length) {
16-
return Random.randomAlphanumeric(length);
34+
return Random.alphanumeric(length);
1735
}
1836

1937
public static String randomNumeric(int length) {
20-
return Random.randomNumeric(length);
38+
return Random.numeric(length);
2139
}
2240

41+
public static String randomHexaBits(int bits) {
42+
return Random.hexaBits(bits);
43+
}
2344

2445
//**JWK**//
2546

@@ -31,23 +52,30 @@ public static String getPublicJwk(String jwkString) {
3152
return Jwk.getPublic(jwkString);
3253
}
3354

34-
public static boolean jwk_verifyJWT(String jwkString, String token) {
35-
return Jwk.verifyJWT(jwkString, token);
55+
//**JWT**//
56+
public static boolean verifyJwt(String path, String alias, String password, String token) {
57+
return Jwt.verify(path, alias, password, token);
3658
}
3759

38-
public static String jwk_createJwt(String jwkString, String payload, String header) {
39-
return Jwk.createJwt(jwkString, payload, header);
60+
public static String createJwt(String path, String alias, String password, String payload, String header) {
61+
return Jwt.create(path, alias, password, payload, header);
4062
}
4163

42-
//**JWKS**//
64+
public static long createUnixTimestamp(Date date) {
65+
return UnixTimestamp.create(date);
66+
}
4367

44-
public static boolean jwks_verifyJWT(String jwksString, String token, String kid) {
45-
return Jwks.verifyJWT(jwksString, token, kid);
68+
public static String getJwtHeader(String token) {
69+
return Jwt.getHeader(token);
4670
}
4771

48-
//**JWT**//
49-
public static boolean verifyJWTWithFile(String path, String alias, String password, String token) {
50-
return Jwt.verify(path, alias, password, token);
72+
public static String getJwtPayload(String token) {
73+
return Jwt.getPayload(token);
74+
}
75+
76+
//**ENCODING**//
77+
public static String base64ToBase64Url(String base64) {
78+
return Encoding.b64ToB64Url(base64);
5179
}
5280

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

gamutils/src/main/java/com/genexus/gam/utils/CertificateUtil.java

Lines changed: 0 additions & 113 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.genexus.gam.utils;
2+
3+
import com.genexus.ModelContext;
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
7+
import java.lang.reflect.Constructor;
8+
import java.lang.reflect.InvocationTargetException;
9+
10+
@SuppressWarnings("unused")
11+
public class DynamicCall {
12+
private final ModelContext mContext;
13+
private final Integer mRemoteHandle;
14+
private static final Logger logger = LogManager.getLogger(DynamicCall.class);
15+
16+
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
17+
18+
@SuppressWarnings("unused")
19+
public DynamicCall(ModelContext context, Integer remoteHandle) {
20+
mContext = context;
21+
mRemoteHandle = remoteHandle;
22+
}
23+
24+
@SuppressWarnings("unused")
25+
public boolean execute(String assembly, String typeName, boolean useContext, String method, String jsonParms, String[] jsonOutput) {
26+
logger.debug("execute");
27+
return doCall(assembly, typeName, useContext, method, false, "", jsonParms, jsonOutput);
28+
}
29+
30+
@SuppressWarnings("unused")
31+
public boolean executeEventHandler(String assembly, String typeName, boolean useContext, String method, String eventType, String jsonInput, String[] jsonOutput) {
32+
logger.debug("executeEventHandler");
33+
return doCall(assembly, typeName, useContext, method, true, eventType, jsonInput, jsonOutput);
34+
}
35+
36+
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/
37+
38+
39+
private boolean doCall(String assembly, String typeName, boolean useContext, String method, boolean isEventHandler, String parm1, String parm2, String[] jsonOutput) {
40+
logger.debug("doCall");
41+
Object[] parms;
42+
Class<?>[] parmTypes;
43+
if (isEventHandler) {
44+
parms = new Object[]{parm1, parm2, new String[]{jsonOutput[0]}};
45+
parmTypes = new Class[]{String.class, String.class, String[].class};
46+
} else {
47+
parms = new Object[]{parm2, new String[]{jsonOutput[0]}};
48+
parmTypes = new Class[]{String.class, String[].class};
49+
}
50+
51+
try {
52+
Class<?> myClass = Class.forName(typeName);
53+
Class<?>[] constructorParms;
54+
Constructor<?> constructor;
55+
Object instance = null;
56+
57+
if (useContext && (mContext != null)) {
58+
try {
59+
constructorParms = new Class[]{int.class, ModelContext.class};
60+
constructor = myClass.getConstructor(constructorParms);
61+
instance = constructor.newInstance(new Object[]{mRemoteHandle, mContext});
62+
} catch (NoSuchMethodException e) {
63+
logger.error("doCall", e);
64+
}
65+
}
66+
67+
if (instance == null) {
68+
constructorParms = new Class[]{int.class};
69+
constructor = myClass.getConstructor(constructorParms);
70+
instance = constructor.newInstance(new Object[]{-2});
71+
}
72+
73+
myClass.getMethod(method, parmTypes).invoke(instance, parms);
74+
} catch (ClassNotFoundException e) {
75+
logger.error("doCall", e);
76+
jsonOutput[0] = "{\"error\":\"" + " class " + typeName + " not found" + "\"}";
77+
return false;
78+
} catch (NoSuchMethodException e) {
79+
logger.error("doCall", e);
80+
jsonOutput[0] = "{\"error\":\"" + " method " + method + " not found" + "\"}";
81+
return false;
82+
} catch (InstantiationException e) {
83+
logger.error("doCall", e);
84+
jsonOutput[0] = "{\"error\":\"" + " cannot instantiate type " + typeName + "\"}";
85+
return false;
86+
} catch (IllegalAccessException e) {
87+
logger.error("doCall", e);
88+
jsonOutput[0] = "{\"error\":\"" + " cannot access method " + method + "\"}";
89+
return false;
90+
} catch (InvocationTargetException e) {
91+
logger.error("doCall", e);
92+
jsonOutput[0] = "{\"error\":\"" + " InvocationTargetException in class " + typeName + "\"}";
93+
return false;
94+
}
95+
String[] result = (String[]) parms[parms.length - 1];
96+
jsonOutput[0] = result[0];
97+
logger.debug("doCall result {}", result[0]);
98+
return true;
99+
}
100+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.genexus.gam.utils;
2+
3+
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
import org.bouncycastle.util.encoders.Base64;
7+
import org.bouncycastle.util.encoders.UrlBase64;
8+
9+
public class Encoding {
10+
11+
private static final Logger logger = LogManager.getLogger(Encoding.class);
12+
13+
public static String b64ToB64Url(String input) {
14+
logger.debug("b64ToB64Url");
15+
try {
16+
return new String(UrlBase64.encode(Base64.decode(input)), "UTF-8").replaceAll("[\ufffd]", "");
17+
} catch (Exception e) {
18+
logger.error("b64ToB64Url", e);
19+
return "";
20+
}
21+
}
22+
}

gamutils/src/main/java/com/genexus/gam/utils/Hash.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)