Search code examples
authenticationmessageamazon-sns

How Do I Authenticate a AWS SNS Message Using C#?


I am sending SNS messages to a HTTP endpoint. I can pick up the message information from the JSON data but how do I authenticate the message and validate the message signature?

This is one of the messages:

{
  "Type": "Notification",
  "MessageId": "a1825ceb-aa86-531a-9712-09b49bb60b32",
  "TopicArn": "arn:aws:sns:us-west-2:xxxx:Test_Topic",
  "Message": "This is the message body",
  "Timestamp": "2019-05-22T11:13:52.513Z",
  "SignatureVersion": "1",
  "Signature": "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc",
  "SigningCertURL": "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-abcabcabc.pem",
  "UnsubscribeURL": "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:yyy",
  "MessageAttributes": {
    "String2": {
      "Type": "String",
      "Value": "This is the second string"
    },
    "String1": {
      "Type": "String",
      "Value": "This is the first string"
    }
  }
}

How do I check that the Message Signature is valid?


Solution

  • I'm using the Message class from the AWSSDK Utils, here is the code.

    var msg = await ReadBody();
    
    var message = Message.ParseMessage(msg);
    
    if (!message.IsMessageSignatureValid())
    

    this is the package Amazon.SimpleNotificationService.Util.Message.

    Hope this helps.