Class SignedCloudEventExtensions
- Namespace
- Savvyio.EventDriven.Messaging.CloudEvents.Cryptography
- Assembly
- Savvyio.EventDriven.Messaging.dll
Extension methods for the ICloudEvent<T> interface.
public static class SignedCloudEventExtensions
- Inheritance
-
SignedCloudEventExtensions
Examples
This example shows how to verify a signed CloudEvent before deserializing it into application-level event processing.
using System;
using Savvyio;
using Savvyio.EventDriven;
using Savvyio.EventDriven.Messaging.CloudEvents;
using Savvyio.EventDriven.Messaging.CloudEvents.Cryptography;
using Savvyio.Messaging;
namespace ExampleApp;
using System;
using System.IO;
using System.Text;
using Savvyio;
public sealed class DemoMarshaller : IMarshaller
{
public Stream Serialize<TValue>(TValue value) => new MemoryStream(Encoding.UTF8.GetBytes(value?.ToString() ?? string.Empty));
public Stream Serialize(object value, Type inputType) => Serialize(value?.ToString() ?? string.Empty);
public TValue Deserialize<TValue>(Stream data) => throw new NotSupportedException();
public object Deserialize(Stream data, Type returnType) => throw new NotSupportedException();
}
public sealed class SignedCloudEventExtensionsExample
{
public void Verify()
{
var message = new Message<MemberCreatedEvent>("msg-42", new Uri("https://api.example.com/members"), "members.created", new MemberCreatedEvent("MEM-42"));
var signed = message.ToCloudEvent().SignCloudEvent(new DemoMarshaller(), options => options.SignatureSecret = new byte[] { 1, 2, 3 });
signed.CheckCloudEventSignature(new DemoMarshaller(), options => options.SignatureSecret = new byte[] { 1, 2, 3 });
}
}
public sealed record MemberCreatedEvent(string MemberId) : Request, IIntegrationEvent;
Methods
CheckCloudEventSignature<T>(ISignedCloudEvent<T>, IMarshaller, Action<SignedMessageOptions>)
Verifies the digital signature of the ISignedCloudEvent<T> message.
public static void CheckCloudEventSignature<T>(this ISignedCloudEvent<T> cloudEvent, IMarshaller marshaller, Action<SignedMessageOptions> setup) where T : IIntegrationEvent
Parameters
cloudEventISignedCloudEvent<T>The IRequest message to cryptographically verify.
marshallerIMarshallerThe IMarshaller that is used when converting the
cloudEventinto an ISignedCloudEvent<T>.setupAction<SignedMessageOptions>The SignedMessageOptions that must be configured.
Type Parameters
TThe type of the payload constraint to the IRequest interface.
Remarks
This method throws an ArgumentOutOfRangeException if the verification of the digital signature fails.
Exceptions
- ArgumentNullException
cloudEventcannot be null -or-marshallercannot be null.- ArgumentException
setupfailed to configure an instance of SignedMessageOptions in a valid state.- ArgumentOutOfRangeException
cloudEventsignature did not match the cryptographically calculated value.