public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { try { var body = base.ReadBodyFromStream(readStream, content); if (type == typeof(Bundle)) { if (XmlSignatureHelper.IsSigned(body)) { if (!XmlSignatureHelper.VerifySignature(body)) { throw Error.BadRequest("Digital signature in body failed verification"); } } } if (typeof(Resource).IsAssignableFrom(type)) { Resource resource = _parser.Parse <Resource>(body); return(Task.FromResult <object>(resource)); } else { throw Error.Internal("The type {0} expected by the controller can not be deserialized", type.Name); } } catch (FormatException exc) { throw Error.BadRequest("Body parsing failed: " + exc.Message); } }
public void TestSigning() { Bundle b = new Bundle(); b.Title = "Updates to resource 233"; b.Id = new Uri("urn:uuid:0d0dcca9-23b9-4149-8619-65002224c3"); b.LastUpdated = new DateTimeOffset(2012, 11, 2, 14, 17, 21, TimeSpan.Zero); b.AuthorName = "Ewout Kramer"; ResourceEntry <Patient> p = new ResourceEntry <Patient>(); p.Id = new ResourceIdentity("http://test.com/fhir/Patient/233"); p.Resource = new Patient(); p.Resource.Name = new List <HumanName> { HumanName.ForFamily("Kramer").WithGiven("Ewout") }; b.Entries.Add(p); var myAssembly = typeof(TestXmlSignature).Assembly; var stream = myAssembly.GetManifestResourceStream("Spark.Tests.spark.pfx"); var data = new byte[stream.Length]; stream.Read(data, 0, (int)stream.Length); var certificate = new X509Certificate2(data); var bundleData = FhirSerializer.SerializeBundleToXmlBytes(b); var bundleXml = Encoding.UTF8.GetString(bundleData); var bundleSigned = XmlSignatureHelper.Sign(bundleXml, certificate); Assert.IsTrue(XmlSignatureHelper.IsSigned(bundleSigned)); Assert.IsTrue(XmlSignatureHelper.VerifySignature(bundleSigned)); var changedBundle = bundleSigned.Replace("<name>Ewout", "<name>Ewald"); Assert.AreEqual(bundleSigned.Length, changedBundle.Length); Assert.IsFalse(XmlSignatureHelper.VerifySignature(changedBundle)); }
public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { return(System.Threading.Tasks.Task.Factory.StartNew <object>(() => { try { var body = ReadBodyFromStream(readStream, content); if (type == typeof(Bundle)) { if (XmlSignatureHelper.IsSigned(body)) { if (!XmlSignatureHelper.VerifySignature(body)) { throw Error.BadRequest("Digital signature in body failed verification"); } } } if (!typeof(Resource).IsAssignableFrom(type)) { throw Error.Internal("The type {0} expected by the controller can not be deserialized", type.Name); } //var fhirparser = new FhirJsonParser(); //var resource = fhirparser.Parse(body, type); var fhirXmlParser = new FhirXmlParser(); var resource = fhirXmlParser.Parse(body, type); return resource; } catch (FormatException exc) { throw Error.BadRequest("Body parsing failed: " + exc.Message); } })); }