public static void Validate(HPPResponse resp, string secret) { if (!resp.IsHashValid(secret)) { throw new RealexValidationException("HppResponse contains an invalid security hash"); } }
public HPPResponse ResponseFromJson(string json, bool encoded) { HPPResponse resp = JsonConvert.DeserializeObject <HPPResponse>(json); // Decodeif necessary resp.IsEncoded = encoded; try { resp.Decode(); } catch (Exception e) { throw new RealexException("Exception decoding HPP response.", e); } // Check if valid ValidationUtils.Validate(resp, Secret); return(resp); }
public void TestDeserialiseHPPResponse() { string filename; filename = "response-enc4.json"; string json = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, @"Data", filename)); HPPResponse resp = JsonConvert.DeserializeObject <HPPResponse>(json); resp.IsEncoded = true; resp.Decode(); Console.WriteLine("===================="); Console.WriteLine(); Console.WriteLine("===================="); RealexHPP hpp = new RealexHPP() { Secret = "secret" }; filename = "response-fail-enc.json"; string json2 = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, @"Data", filename)); HPPResponse resp2 = hpp.ResponseFromJson(json2); Assert.Equal(resp2.Result, "101"); ResourceManager rm = global.cloudis.RealexHPP.HPPMessages.ResourceManager; Console.WriteLine(rm.GetString("hppResponse_message_unknown")); Assert.Null(MessageUtils.GetMessage("No message")); Assert.NotNull(MessageUtils.GetMessageForResult("101")); Assert.NotNull(MessageUtils.GetMessageForResult("508")); Console.WriteLine(MessageUtils.GetMessageForResult("508")); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var resp = new HPPResponse(); var attrType = typeof(JsonPropertyAttribute); var fields = new Dictionary <string, string>(); var supplementaryData = new Dictionary <string, string>(); foreach (PropertyInfo pi in typeof(HPPResponse).GetTypeInfo().DeclaredProperties.Where(p => p.GetCustomAttributes(attrType, false).Count() > 0)) { fields.Add(((JsonPropertyAttribute)(pi.GetCustomAttributes(attrType, false).First())).PropertyName, pi.Name); } while (reader.Read()) { if (reader.TokenType == JsonToken.EndObject) { continue; } var attribute = reader.Value.ToString(); if (fields.Keys.Contains(attribute)) { resp.GetType().GetTypeInfo().GetDeclaredProperty(fields[attribute]).SetValue(resp, reader.ReadAsString()); } else { supplementaryData.Add(attribute, reader.ReadAsString()); } } resp.SupplementaryData = supplementaryData; return(resp); }