Skip to content

Commit 7e4e777

Browse files
sgramponeBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:gamutils_eo' into beta
1 parent 3a14d80 commit 7e4e777

File tree

22 files changed

+783
-665
lines changed

22 files changed

+783
-665
lines changed

dotnet/src/extensions/gam/src/DotNet/GamUtilsNet/GamUtilsNet.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<AssemblyName>GamUtilsNetImpl</AssemblyName>
66
<Version>17.4.0</Version>
77
<NoWarn>CA1031, CA1801, SYSLIB0027</NoWarn>
8-
<PackageId>GeneXxus.Gam.Utils.Net</PackageId>
8+
<PackageId>Gam.Utils.Net</PackageId>
99
</PropertyGroup>
1010

1111
<PropertyGroup>
@@ -16,12 +16,14 @@
1616
<Compile Include="..\..\DotNetFramework\GamUtils\GamUtilsEO.cs" Link="GamUtilsEO.cs" />
1717
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Cryprography\Encryption.cs" Link="Utils\Cryptography\Encryption.cs" />
1818
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Cryprography\Hash.cs" Link="Utils\Cryptography\Hash.cs" />
19+
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\DynamicCall.cs" Link="Utils\DynamicCall.cs" />
1920
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Json\Jwk.cs" Link="Utils\Json\Jwk.cs" />
20-
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Json\Jwks.cs" Link="Utils\Json\Jwks.cs" />
2121
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Json\Jwt.cs" Link="Utils\Json\Jwt.cs" />
22-
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Keys\CertificateExt.cs" Link="Utils\Keys\CertificateExt.cs" />
22+
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Json\UnixTimestamp.cs" Link="Utils\Json\UnixTimestamp.cs" />
23+
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Keys\PublicKeyExt.cs" Link="Utils\Keys\PublicKeyExt.cs" />
2324
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Keys\PrivateKeyExt.cs" Link="Utils\Keys\PrivateKeyExt.cs" />
2425
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Random.cs" Link="Utils\Random.cs" />
26+
<Compile Include="..\..\DotNetFramework\GamUtils\Utils\Encoding.cs" Link="Utils\Encoding.cs" />
2527
</ItemGroup>
2628

2729
<ItemGroup>

dotnet/src/extensions/gam/src/DotNetFramework/GamUtils/GamUtils.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>net47</TargetFramework>
44
<AssemblyName>GamUtilsImpl</AssemblyName>
55
<NoWarn>CA1031, CA1801</NoWarn>
6-
<PackageId>Genexus.Gam.Utils</PackageId>
6+
<PackageId>Gam.Utils</PackageId>
77
</PropertyGroup>
88
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
99
<Optimize>True</Optimize>

dotnet/src/extensions/gam/src/DotNetFramework/GamUtils/GamUtilsEO.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Security;
23
using GamUtils.Utils;
34
using GamUtils.Utils.Cryprography;
@@ -14,7 +15,13 @@ public class GamUtilsEO
1415
[SecuritySafeCritical]
1516
public static string Sha512(string plainText)
1617
{
17-
return Hash.Sha512(plainText);
18+
return HashUtil.Hashing(plainText, Hash.SHA512);
19+
}
20+
21+
[SecuritySafeCritical]
22+
public static string Sha256(string plainText)
23+
{
24+
return HashUtil.Hashing(plainText, Hash.SHA256);
1825
}
1926

2027
//**ENCRYPTION**//
@@ -51,22 +58,23 @@ public static string RandomHexaBits(int bits)
5158
[SecuritySafeCritical]
5259
public static string GetPublicJwk(string jwkString) { return Jwk.GetPublic(jwkString); }
5360

61+
//**JWT**//
5462
[SecuritySafeCritical]
55-
public static string Jwk_createJwt(string jwkString, string payload, string header) { return Jwk.CreateJwt(jwkString, payload, header); }
63+
public static bool VerifyJwt(string path, string alias, string password, string token) { return Jwt.Verify(path, alias, password, token); }
5664

5765
[SecuritySafeCritical]
58-
public static bool Jwk_verifyJWT(string jwkString, string token) { return Jwk.VerifyJWT(jwkString, token); }
59-
60-
//**JWKS**//
66+
public static string CreateJwt(string path, string alias, string password, string payload, string header) { return Jwt.Create(path, alias, password, payload, header); }
6167

6268
[SecuritySafeCritical]
63-
public static bool Jwks_verifyJWT(string jwksString, string token, string kid) { return Jwks.VerifyJWT(jwksString, token, kid); }
69+
public static long CreateUnixTimestamp(DateTime date) { return UnixTimestamp.Create(date); }
6470

65-
//**JWT**//
6671
[SecuritySafeCritical]
67-
public static bool VerifyJWTWithFile(string path, string alias, string password, string token) { return Jwt.Verify(path, alias, password, token); }
72+
public static string GetJwtHeader(string token) { return Jwt.GetHeader(token); }
6873

6974
[SecuritySafeCritical]
70-
public static string CreateJWTWithFile(string path, string alias, string password, string payload, string header) { return Jwt.Create(path, alias, password, payload, header); }
75+
public static string GetJwtPayload(string token) { return Jwt.GetPayload(token); }
76+
77+
//**ENCODING**//
78+
public static string Base64ToBase64Url(string base64) { return Encoding.B64ToB64Url(base64); }
7179
}
7280
}

dotnet/src/extensions/gam/src/DotNetFramework/GamUtils/Utils/Cryprography/Encryption.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal class Encryption
1818
[SecuritySafeCritical]
1919
public static string AesGcm(string input, string key, string nonce, int macSize, bool toEncrypt)
2020
{
21-
return toEncrypt ? Base64.ToBase64String(Internal_AesGcm(Encoding.UTF8.GetBytes(input), key, nonce, macSize, toEncrypt)) : Encoding.UTF8.GetString(Internal_AesGcm(Base64.Decode(input), key, nonce, macSize, toEncrypt));
21+
return toEncrypt ? Base64.ToBase64String(Internal_AesGcm(System.Text.Encoding.UTF8.GetBytes(input), key, nonce, macSize, toEncrypt)) : System.Text.Encoding.UTF8.GetString(Internal_AesGcm(Base64.Decode(input), key, nonce, macSize, toEncrypt));
2222
}
2323

2424
[SecuritySafeCritical]

dotnet/src/extensions/gam/src/DotNetFramework/GamUtils/Utils/Cryprography/Hash.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,46 @@
99
namespace GamUtils.Utils
1010
{
1111
[SecuritySafeCritical]
12-
public class Hash
12+
internal enum Hash
1313
{
14+
NONE, SHA512, SHA256
15+
}
1416

15-
private static readonly ILog logger = LogManager.GetLogger(typeof(Hash));
17+
[SecuritySafeCritical]
18+
public class HashUtil
19+
{
20+
21+
private static readonly ILog logger = LogManager.GetLogger(typeof(HashUtil));
1622

1723
[SecuritySafeCritical]
18-
internal static string Sha512(string plainText)
24+
internal static string Hashing(string plainText, Hash hash)
1925
{
26+
switch (hash)
27+
{
28+
case Hash.SHA256:
29+
return InternalHash(new Sha256Digest(), plainText);
30+
case Hash.SHA512:
31+
return InternalHash(new Sha512Digest(), plainText);
32+
default:
33+
logger.Error("unrecognized hash");
34+
return "";
35+
}
36+
}
2037

38+
private static string InternalHash(IDigest digest, string plainText)
39+
{
40+
logger.Debug("InternalHash");
2141
if (String.IsNullOrEmpty(plainText))
2242
{
23-
logger.Error("sha512 plainText is empty");
43+
logger.Error("hash plainText is empty");
2444
return "";
2545
}
26-
byte[] inputBytes = Encoding.UTF8.GetBytes(plainText);
27-
IDigest alg = new Sha512Digest();
28-
byte[] retValue = new byte[alg.GetDigestSize()];
29-
alg.BlockUpdate(inputBytes, 0, inputBytes.Length);
30-
alg.DoFinal(retValue, 0);
46+
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
47+
byte[] retValue = new byte[digest.GetDigestSize()];
48+
digest.BlockUpdate(inputBytes, 0, inputBytes.Length);
49+
digest.DoFinal(retValue, 0);
3150
return Base64.ToBase64String(retValue);
3251
}
3352
}
3453
}
54+
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
using System;
2+
using System.Text;
3+
using System.Reflection;
4+
using System.IO;
5+
using System.Globalization;
6+
using GeneXus.Application;
7+
using log4net;
8+
9+
#if NETCORE
10+
using GxClasses.Helpers;
11+
using GeneXus.Utils;
12+
#endif
13+
14+
namespace GamUtils.Utils
15+
{
16+
public class DynamicCall
17+
{
18+
19+
private IGxContext mGxContext;
20+
private static readonly ILog logger = LogManager.GetLogger(typeof(DynamicCall));
21+
22+
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
23+
24+
public DynamicCall(IGxContext context)
25+
{
26+
mGxContext = context;
27+
}
28+
29+
public bool Execute(string assembly, string typeName, bool useContext, string method, string jsonParms, out string jsonOutput)
30+
{
31+
logger.Debug("Execute");
32+
return DoCall(assembly, typeName, useContext, method, false, "", jsonParms, out jsonOutput);
33+
}
34+
35+
public bool ExecuteEventHandler(string assembly, string typeName, bool useContext, string method, string eventType, string jsonInput, out string jsonOutput)
36+
{
37+
logger.Debug("ExecuteEventHandler");
38+
return DoCall(assembly, typeName, useContext, method, true, eventType, jsonInput, out jsonOutput);
39+
}
40+
41+
42+
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
43+
44+
45+
private bool DoCall(string assembly, string typeName, bool useContext, string method, bool isEventHandler, string input1, string input2, out string jsonOutput)
46+
{
47+
logger.Debug("DoCall");
48+
try
49+
{
50+
jsonOutput = "{}";
51+
Assembly assm;
52+
if (Path.IsPathRooted(assembly))
53+
assm = Assembly.LoadFrom(assembly);
54+
else
55+
assm = Assembly.LoadFrom(Path.Combine(GetStartupDirectory(), assembly));
56+
57+
if (assm != null)
58+
{
59+
Type type = assm.GetType(typeName);
60+
if (type != null)
61+
{
62+
object instance = null;
63+
if (useContext && (mGxContext != null))
64+
{
65+
ConstructorInfo constructorWithContext = type.GetConstructor(new Type[] { typeof(IGxContext) });
66+
if (constructorWithContext != null)
67+
{
68+
instance = constructorWithContext.Invoke(new object[] { mGxContext });
69+
}
70+
}
71+
if (instance == null)
72+
instance = assm.CreateInstance(typeName);
73+
74+
if (instance != null)
75+
{
76+
MethodInfo methodInfo;
77+
object[] parameters;
78+
79+
if (isEventHandler)
80+
{
81+
methodInfo = instance.GetType().GetMethod(method, new Type[] { input1.GetType(), input2.GetType(), jsonOutput.GetType().MakeByRefType() });
82+
parameters = new object[] { input1, input2, jsonOutput };
83+
}
84+
else
85+
{
86+
methodInfo = instance.GetType().GetMethod(method, new Type[] { input2.GetType(), jsonOutput.GetType().MakeByRefType() });
87+
parameters = new object[] { input2, jsonOutput };
88+
}
89+
90+
if (methodInfo != null)
91+
{
92+
object result = methodInfo.Invoke(instance, parameters);
93+
if (methodInfo.ReturnType == typeof(void) && (parameters.Length > 0))
94+
jsonOutput = (string)parameters[parameters.Length - 1];
95+
else
96+
jsonOutput = result.ToString();
97+
98+
return true;
99+
}
100+
else
101+
{
102+
logger.Error("error: " + method + "(String, out String)" + " in " + assembly + " not found");
103+
jsonOutput = "{\"error\":\"" + method + "(String, out String)" + " in " + assembly + " not found" + "\"}";
104+
return false;
105+
}
106+
}
107+
else
108+
{
109+
logger.Error("error: " + "constructor for " + typeName + " in " + assembly + " not found");
110+
jsonOutput = "{\"error\":\"" + "constructor for " + typeName + " in " + assembly + " not found" + "\"}";
111+
return false;
112+
}
113+
}
114+
else
115+
{
116+
logger.Error("error: " + typeName + " in " + assembly + " not found");
117+
jsonOutput = "{\"error\":\"" + typeName + " in " + assembly + " not found" + "\"}";
118+
return false;
119+
}
120+
}
121+
else
122+
{
123+
logger.Error("error: " + assembly + " not found");
124+
jsonOutput = "{\"error\":\"" + assembly + " not found" + "\"}";
125+
return false;
126+
}
127+
}
128+
catch (Exception ex)
129+
{
130+
StringBuilder str = new StringBuilder();
131+
str.Append(ex.Message);
132+
while (ex.InnerException != null)
133+
{
134+
str.Append(ex.InnerException.Message);
135+
ex = ex.InnerException;
136+
}
137+
logger.Error("error: " + Enquote(str.ToString()));
138+
jsonOutput = "{\"error\":" + Enquote(str.ToString()) + "}";
139+
return false;
140+
}
141+
}
142+
143+
private static string GetStartupDirectory()
144+
{
145+
logger.Debug("GetStartupDirectory");
146+
#if NETCORE
147+
return FileUtil.GetStartupDirectory();
148+
#else
149+
string dir = Path.GetDirectoryName(Assembly.GetCallingAssembly().GetName().CodeBase); ;
150+
if (dir.StartsWith("file:\\"))
151+
dir = dir.Substring(6);
152+
return dir;
153+
#endif
154+
}
155+
156+
157+
private static string Enquote(string s)
158+
{
159+
logger.Debug("Enquote)");
160+
if (s == null || s.Length == 0)
161+
return "\"\"";
162+
163+
int length = s.Length;
164+
StringBuilder sb = new StringBuilder(length + 4);
165+
166+
sb.Append('"');
167+
168+
for (int index = 0; index < length; index++)
169+
{
170+
char ch = s[index];
171+
172+
if ((ch == '\\') || (ch == '"') || (ch == '>'))
173+
{
174+
sb.Append('\\');
175+
sb.Append(ch);
176+
}
177+
else if (ch == '\b')
178+
sb.Append("\\b");
179+
else if (ch == '\t')
180+
sb.Append("\\t");
181+
else if (ch == '\n')
182+
sb.Append("\\n");
183+
else if (ch == '\f')
184+
sb.Append("\\f");
185+
else if (ch == '\r')
186+
sb.Append("\\r");
187+
else
188+
{
189+
if (ch < ' ')
190+
{
191+
//t = "000" + Integer.toHexString(c);
192+
//string tmp = new string(ch, 1);
193+
string t = "000" + ((int)ch).ToString(CultureInfo.InvariantCulture);
194+
sb.Append("\\u" + t.Substring(t.Length - 4));
195+
}
196+
else
197+
{
198+
sb.Append(ch);
199+
}
200+
}
201+
}
202+
203+
sb.Append('"');
204+
return sb.ToString();
205+
}
206+
}
207+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using Org.BouncyCastle.Utilities.Encoders;
3+
using log4net;
4+
using System.Security;
5+
6+
namespace GamUtils.Utils
7+
{
8+
[SecuritySafeCritical]
9+
internal class Encoding
10+
{
11+
private static readonly ILog logger = LogManager.GetLogger(typeof(Encoding));
12+
13+
[SecuritySafeCritical]
14+
internal static string B64ToB64Url(string input)
15+
{
16+
logger.Debug("B64ToB64Url");
17+
try
18+
{
19+
return System.Text.Encoding.UTF8.GetString(UrlBase64.Encode(Base64.Decode(input)));
20+
}
21+
catch (Exception e)
22+
{
23+
logger.Error("B64ToB64Url", e);
24+
return "";
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)