Skip to content

Commit c42cfa2

Browse files
claudiamurialdoclaudiamurialdo
andauthored
Handle large string values with SHA256 in JWT tokens (#1237)
https://github.com/genexuslabs/DotNetClasses/pull/1157/files Co-authored-by: claudiamurialdo <[email protected]>
1 parent d5dfdd5 commit c42cfa2

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

dotnet/src/dotnetframework/GxClasses/Middleware/GXHttp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ protected string GetSecureSignedToken(String cmpCtx, GxUserType Value, IGxContex
18761876

18771877
protected string GetSecureSignedToken(string cmpCtx, string value, IGxContext context)
18781878
{
1879-
return WebSecurityHelper.Sign(PgmInstanceId(cmpCtx), string.Empty, value, SecureTokenHelper.SecurityMode.Sign, context);
1879+
return GetSecureSignedHashedToken(cmpCtx, SecureTokenHelper.GetTokenValue(value), context);
18801880
}
18811881
private string GetSecureSignedHashedToken(string cmpCtx, TokenValue tokenValue, IGxContext context)
18821882
{

dotnet/src/dotnetframework/GxClasses/Security/WebSecurity.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,17 @@ private static string GetSecretKey(IGxContext context)
5555
}
5656

5757
public static bool Verify(string pgmName, string issuer, string value, string jwtToken, IGxContext context)
58-
{
59-
WebSecureToken token;
60-
return WebSecurityHelper.Verify(pgmName, issuer, value, jwtToken, out token, context);
58+
{
59+
WebSecureToken token;
60+
WebSecureToken jwtTokenObj = SecureTokenHelper.getWebSecureToken(jwtToken, GetSecretKey(context));
61+
if (jwtTokenObj != null && jwtTokenObj.ValueType == ValueTypeHash)
62+
{
63+
return Verify(pgmName, issuer, GetHash(value), jwtToken, out token, context);
64+
}
65+
else
66+
{
67+
return Verify(pgmName, issuer, value, jwtToken, out token, context);
68+
}
6169
}
6270
public static bool Verify(string pgmName, string issuer, string value, string jwtToken, out WebSecureToken token, IGxContext context)
6371
{
@@ -255,20 +263,22 @@ internal static bool Verify(string jwtToken, WebSecureToken outToken, string sec
255263
}
256264
internal static TokenValue GetTokenValue(IGxJSONSerializable obj)
257265
{
258-
259-
string jsonString = obj.ToJSonString();
266+
return GetTokenValue(obj.ToJSonString());
267+
}
268+
internal static TokenValue GetTokenValue(string value)
269+
{
260270

261-
if (jsonString.Length > MaxTokenValueLength)
271+
if (value!=null && value.Length > MaxTokenValueLength)
262272
{
263-
string hash = GetHash(jsonString);
273+
string hash = GetHash(value);
264274
GXLogging.Debug(_log, $"GetTokenValue: TokenValue is too long, using hash: {hash} instead of original value.");
265-
GXLogging.Debug(_log, $"Server TokenOriginalValue:" + jsonString);
275+
GXLogging.Debug(_log, $"Server TokenOriginalValue:" + value);
266276
return new TokenValue() { Value = hash, ValueType = ValueTypeHash };
267277
}
268278
else
269279
{
270-
GXLogging.Debug(_log, $"GetTokenValue:" + jsonString);
271-
return new TokenValue() { Value = jsonString };
280+
GXLogging.Debug(_log, $"GetTokenValue:" + value);
281+
return new TokenValue() { Value = value };
272282
}
273283
}
274284
internal static string GetHash(string jsonString)

0 commit comments

Comments
 (0)