Skip to content

Commit 6be5b78

Browse files
Erik Bylundkirre-bylund
authored andcommitted
Move out Obfuscator from serverapi class
1 parent 137eae1 commit 6be5b78

File tree

3 files changed

+192
-177
lines changed

3 files changed

+192
-177
lines changed

Runtime/Client/LootLockerServerApi.cs

Lines changed: 4 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
using System.Text;
77
using LootLocker.LootLockerEnums;
88
using UnityEditor;
9-
#if LOOTLOCKER_USE_NEWTONSOFTJSON
10-
using Newtonsoft.Json;
11-
using Newtonsoft.Json.Linq;
12-
#else
13-
using LLlibs.ZeroDepJson;
14-
#endif
159
using LootLocker.Requests;
1610

1711
namespace LootLocker.LootLockerEnums
@@ -110,13 +104,13 @@ IEnumerator coroutine()
110104

111105
try
112106
{
113-
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Verbose)("Server Response: " + webRequest.responseCode + " " + request.endpoint + " completed in " + (Time.time - startTime).ToString("n4") + " secs.\nResponse: " + ObfuscateJsonStringForLogging(webRequest.downloadHandler.text));
107+
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Verbose)("Server Response: " + webRequest.responseCode + " " + request.endpoint + " completed in " + (Time.time - startTime).ToString("n4") + " secs.\nResponse: " + LootLockerObfuscator.ObfuscateJsonStringForLogging(webRequest.downloadHandler.text));
114108
}
115109
catch
116110
{
117111
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(request.httpMethod.ToString());
118112
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(request.endpoint);
119-
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(ObfuscateJsonStringForLogging(webRequest.downloadHandler.text));
113+
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(LootLockerObfuscator.ObfuscateJsonStringForLogging(webRequest.downloadHandler.text));
120114
}
121115

122116
if (WebRequestSucceeded(webRequest))
@@ -198,7 +192,7 @@ IEnumerator coroutine()
198192
errorData.message = "Unknown error.";
199193
break;
200194
}
201-
errorData.message += " " + ObfuscateJsonStringForLogging(webRequest.downloadHandler.text);
195+
errorData.message += " " + LootLockerObfuscator.ObfuscateJsonStringForLogging(webRequest.downloadHandler.text);
202196
}
203197
response.errorData = errorData;
204198
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(response.errorData.message + (!string.IsNullOrEmpty(response.errorData.doc_url) ? " -- " + response.errorData.doc_url : ""));
@@ -410,7 +404,7 @@ private UnityWebRequest CreateWebRequest(string url, LootLockerServerRequest req
410404
{
411405
string json = (request.payload != null && request.payload.Count > 0) ? LootLockerJson.SerializeObject(request.payload) : request.jsonPayload;
412406
#if UNITY_EDITOR
413-
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Verbose)("REQUEST BODY = " + ObfuscateJsonStringForLogging(json));
407+
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Verbose)("REQUEST BODY = " + LootLockerObfuscator.ObfuscateJsonStringForLogging(json));
414408
#endif
415409
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(string.IsNullOrEmpty(json) ? "{}" : json);
416410
webRequest = UnityWebRequest.Put(url, bytes);
@@ -461,173 +455,6 @@ private UnityWebRequest CreateWebRequest(string url, LootLockerServerRequest req
461455
return webRequest;
462456
}
463457

464-
private struct ObfuscationDetails
465-
{
466-
public string key { get; set; }
467-
public char replacementChar { get; set; }
468-
public int visibleCharsFromBeginning { get; set; }
469-
public int visibleCharsFromEnd { get; set; }
470-
public bool hideCharactersForShortStrings { get; set; }
471-
472-
public ObfuscationDetails(string key, char replacementChar = '*', int visibleCharsFromBeginning = 3, int visibleCharsFromEnd = 3, bool hideCharactersForShortStrings = true)
473-
{
474-
this.key = key;
475-
this.replacementChar = replacementChar;
476-
this.visibleCharsFromBeginning = visibleCharsFromBeginning;
477-
this.visibleCharsFromEnd = visibleCharsFromEnd;
478-
this.hideCharactersForShortStrings = hideCharactersForShortStrings;
479-
}
480-
}
481-
482-
static readonly List<ObfuscationDetails> FieldsToObfuscate = new List<ObfuscationDetails>
483-
{
484-
new ObfuscationDetails("game_key", '*', 4, 3, false),
485-
new ObfuscationDetails("email"),
486-
new ObfuscationDetails("password", '*', 0, 0),
487-
new ObfuscationDetails("domain_key"),
488-
new ObfuscationDetails("session_token"),
489-
new ObfuscationDetails("token")
490-
};
491-
492-
private static string ObfuscateJsonStringForLogging(string json)
493-
{
494-
#if LOOTLOCKER_USE_NEWTONSOFTJSON
495-
if (string.IsNullOrEmpty(json))
496-
{
497-
return json;
498-
}
499-
500-
JObject jsonObject;
501-
try
502-
{
503-
jsonObject = JObject.Parse(json);
504-
}
505-
catch (JsonReaderException)
506-
{
507-
return json;
508-
}
509-
;
510-
if (jsonObject.HasValues)
511-
{
512-
foreach (ObfuscationDetails obfuscationInfo in FieldsToObfuscate)
513-
{
514-
string valueToObfuscate;
515-
try
516-
{
517-
JToken jsonValue;
518-
jsonObject.TryGetValue(obfuscationInfo.key, StringComparison.Ordinal, out jsonValue);
519-
if (jsonValue == null || (jsonValue.Type != JTokenType.String && jsonValue.Type != JTokenType.Integer))
520-
continue;
521-
valueToObfuscate = jsonValue.ToString();
522-
}
523-
catch (KeyNotFoundException)
524-
{
525-
continue;
526-
}
527-
528-
if (string.IsNullOrEmpty(valueToObfuscate))
529-
continue;
530-
531-
if (valueToObfuscate.Equals("null", StringComparison.Ordinal))
532-
continue;
533-
534-
int replaceFrom = 0;
535-
int replaceTo = valueToObfuscate.Length;
536-
537-
// Deal with short strings
538-
if (valueToObfuscate.Length <= obfuscationInfo.visibleCharsFromBeginning + obfuscationInfo.visibleCharsFromEnd)
539-
{
540-
if (!obfuscationInfo.hideCharactersForShortStrings) // Hide nothing, else hide everything
541-
continue;
542-
}
543-
// Replace in
544-
else
545-
{
546-
replaceFrom += obfuscationInfo.visibleCharsFromBeginning;
547-
replaceTo -= obfuscationInfo.visibleCharsFromEnd;
548-
}
549-
550-
StringBuilder replacement = new StringBuilder();
551-
replacement.Append(obfuscationInfo.replacementChar, replaceTo - replaceFrom);
552-
StringBuilder obfuscatedValue = new StringBuilder(valueToObfuscate);
553-
obfuscatedValue.Remove(replaceFrom, replacement.Length);
554-
obfuscatedValue.Insert(replaceFrom, replacement.ToString());
555-
jsonObject[obfuscationInfo.key] = obfuscatedValue.ToString();
556-
}
557-
}
558-
559-
return LootLockerJson.SerializeObject(jsonObject);
560-
561-
#else //LOOTLOCKER_USE_NEWTONSOFTJSON
562-
if (string.IsNullOrEmpty(json) || json.Equals("{}"))
563-
{
564-
return json;
565-
}
566-
567-
Dictionary<string, object> jsonObject = null;
568-
try
569-
{
570-
jsonObject = Json.Deserialize(json) as Dictionary<string, object>;
571-
}
572-
catch (JsonException)
573-
{
574-
return json;
575-
}
576-
577-
if (jsonObject != null && jsonObject.Count > 0)
578-
{
579-
foreach (ObfuscationDetails obfuscationInfo in FieldsToObfuscate)
580-
{
581-
string valueToObfuscate;
582-
try
583-
{
584-
if (!jsonObject.ContainsKey(obfuscationInfo.key))
585-
{
586-
continue;
587-
}
588-
589-
valueToObfuscate = Json.Serialize(jsonObject[obfuscationInfo.key]);
590-
}
591-
catch (KeyNotFoundException)
592-
{
593-
continue;
594-
}
595-
596-
if (string.IsNullOrEmpty(valueToObfuscate))
597-
continue;
598-
599-
if (valueToObfuscate.Equals("null", StringComparison.Ordinal))
600-
continue;
601-
602-
int replaceFrom = 0;
603-
int replaceTo = valueToObfuscate.Length;
604-
605-
// Deal with short strings
606-
if (valueToObfuscate.Length <= obfuscationInfo.visibleCharsFromBeginning + obfuscationInfo.visibleCharsFromEnd)
607-
{
608-
if (!obfuscationInfo.hideCharactersForShortStrings) // Hide nothing, else hide everything
609-
continue;
610-
}
611-
// Replace in
612-
else
613-
{
614-
replaceFrom += obfuscationInfo.visibleCharsFromBeginning;
615-
replaceTo -= obfuscationInfo.visibleCharsFromEnd;
616-
}
617-
618-
StringBuilder replacement = new StringBuilder();
619-
replacement.Append(obfuscationInfo.replacementChar, replaceTo - replaceFrom);
620-
StringBuilder obfuscatedValue = new StringBuilder(valueToObfuscate);
621-
obfuscatedValue.Remove(replaceFrom, replacement.Length);
622-
obfuscatedValue.Insert(replaceFrom, replacement.ToString());
623-
jsonObject[obfuscationInfo.key] = obfuscatedValue.ToString();
624-
}
625-
}
626-
627-
return LootLockerJson.SerializeObject(jsonObject);
628-
#endif //LOOTLOCKER_USE_NEWTONSOFTJSON
629-
}
630-
631458
private string BuildUrl(string endpoint, Dictionary<string, string> queryParams = null, LootLockerCallerRole callerRole = LootLockerCallerRole.User)
632459
{
633460
string ep = endpoint.StartsWith("/") ? endpoint.Trim() : "/" + endpoint.Trim();

Runtime/Game/LootLockerObfuscator.cs

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
using LLlibs.ZeroDepJson;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace LootLocker
7+
{
8+
public class LootLockerObfuscator
9+
{
10+
private struct ObfuscationDetails
11+
{
12+
public string key { get; set; }
13+
public char replacementChar { get; set; }
14+
public int visibleCharsFromBeginning { get; set; }
15+
public int visibleCharsFromEnd { get; set; }
16+
public bool hideCharactersForShortStrings { get; set; }
17+
18+
public ObfuscationDetails(string key, char replacementChar = '*', int visibleCharsFromBeginning = 3, int visibleCharsFromEnd = 3, bool hideCharactersForShortStrings = true)
19+
{
20+
this.key = key;
21+
this.replacementChar = replacementChar;
22+
this.visibleCharsFromBeginning = visibleCharsFromBeginning;
23+
this.visibleCharsFromEnd = visibleCharsFromEnd;
24+
this.hideCharactersForShortStrings = hideCharactersForShortStrings;
25+
}
26+
}
27+
28+
private static readonly List<ObfuscationDetails> FieldsToObfuscate = new List<ObfuscationDetails>
29+
{
30+
new ObfuscationDetails("game_key", '*', 4, 3, false),
31+
new ObfuscationDetails("email"),
32+
new ObfuscationDetails("password", '*', 0, 0),
33+
new ObfuscationDetails("domain_key"),
34+
new ObfuscationDetails("session_token"),
35+
new ObfuscationDetails("token")
36+
};
37+
38+
public static string ObfuscateJsonStringForLogging(string json)
39+
{
40+
#if LOOTLOCKER_USE_NEWTONSOFTJSON
41+
if (string.IsNullOrEmpty(json))
42+
{
43+
return json;
44+
}
45+
46+
JObject jsonObject;
47+
try
48+
{
49+
jsonObject = JObject.Parse(json);
50+
}
51+
catch (JsonReaderException)
52+
{
53+
return json;
54+
}
55+
;
56+
if (jsonObject.HasValues)
57+
{
58+
foreach (ObfuscationDetails obfuscationInfo in FieldsToObfuscate)
59+
{
60+
string valueToObfuscate;
61+
try
62+
{
63+
JToken jsonValue;
64+
jsonObject.TryGetValue(obfuscationInfo.key, StringComparison.Ordinal, out jsonValue);
65+
if (jsonValue == null || (jsonValue.Type != JTokenType.String && jsonValue.Type != JTokenType.Integer))
66+
continue;
67+
valueToObfuscate = jsonValue.ToString();
68+
}
69+
catch (KeyNotFoundException)
70+
{
71+
continue;
72+
}
73+
74+
if (string.IsNullOrEmpty(valueToObfuscate))
75+
continue;
76+
77+
if (valueToObfuscate.Equals("null", StringComparison.Ordinal))
78+
continue;
79+
80+
int replaceFrom = 0;
81+
int replaceTo = valueToObfuscate.Length;
82+
83+
// Deal with short strings
84+
if (valueToObfuscate.Length <= obfuscationInfo.visibleCharsFromBeginning + obfuscationInfo.visibleCharsFromEnd)
85+
{
86+
if (!obfuscationInfo.hideCharactersForShortStrings) // Hide nothing, else hide everything
87+
continue;
88+
}
89+
// Replace in
90+
else
91+
{
92+
replaceFrom += obfuscationInfo.visibleCharsFromBeginning;
93+
replaceTo -= obfuscationInfo.visibleCharsFromEnd;
94+
}
95+
96+
StringBuilder replacement = new StringBuilder();
97+
replacement.Append(obfuscationInfo.replacementChar, replaceTo - replaceFrom);
98+
StringBuilder obfuscatedValue = new StringBuilder(valueToObfuscate);
99+
obfuscatedValue.Remove(replaceFrom, replacement.Length);
100+
obfuscatedValue.Insert(replaceFrom, replacement.ToString());
101+
jsonObject[obfuscationInfo.key] = obfuscatedValue.ToString();
102+
}
103+
}
104+
105+
return LootLockerJson.SerializeObject(jsonObject);
106+
107+
#else //LOOTLOCKER_USE_NEWTONSOFTJSON
108+
if (string.IsNullOrEmpty(json) || json.Equals("{}"))
109+
{
110+
return json;
111+
}
112+
113+
Dictionary<string, object> jsonObject = null;
114+
try
115+
{
116+
jsonObject = Json.Deserialize(json) as Dictionary<string, object>;
117+
}
118+
catch (JsonException)
119+
{
120+
return json;
121+
}
122+
123+
if (jsonObject != null && jsonObject.Count > 0)
124+
{
125+
foreach (ObfuscationDetails obfuscationInfo in FieldsToObfuscate)
126+
{
127+
string valueToObfuscate;
128+
try
129+
{
130+
if (!jsonObject.ContainsKey(obfuscationInfo.key))
131+
{
132+
continue;
133+
}
134+
135+
valueToObfuscate = Json.Serialize(jsonObject[obfuscationInfo.key]);
136+
}
137+
catch (KeyNotFoundException)
138+
{
139+
continue;
140+
}
141+
142+
if (string.IsNullOrEmpty(valueToObfuscate))
143+
continue;
144+
145+
if (valueToObfuscate.Equals("null", StringComparison.Ordinal))
146+
continue;
147+
148+
int replaceFrom = 0;
149+
int replaceTo = valueToObfuscate.Length;
150+
151+
// Deal with short strings
152+
if (valueToObfuscate.Length <= obfuscationInfo.visibleCharsFromBeginning + obfuscationInfo.visibleCharsFromEnd)
153+
{
154+
if (!obfuscationInfo.hideCharactersForShortStrings) // Hide nothing, else hide everything
155+
continue;
156+
}
157+
// Replace in
158+
else
159+
{
160+
replaceFrom += obfuscationInfo.visibleCharsFromBeginning;
161+
replaceTo -= obfuscationInfo.visibleCharsFromEnd;
162+
}
163+
164+
StringBuilder replacement = new StringBuilder();
165+
replacement.Append(obfuscationInfo.replacementChar, replaceTo - replaceFrom);
166+
StringBuilder obfuscatedValue = new StringBuilder(valueToObfuscate);
167+
obfuscatedValue.Remove(replaceFrom, replacement.Length);
168+
obfuscatedValue.Insert(replaceFrom, replacement.ToString());
169+
jsonObject[obfuscationInfo.key] = obfuscatedValue.ToString();
170+
}
171+
}
172+
173+
return LootLockerJson.SerializeObject(jsonObject);
174+
#endif //LOOTLOCKER_USE_NEWTONSOFTJSON
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)