Class: Aws::SNS::MessageVerifier

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-sdk-sns/message_verifier.rb

Overview

A utility class that can be used to verify the authenticity of messages sent by Amazon SNS.

verifier = Aws::SNS::MessageVerifier.new

# returns true/false
verifier.authentic?(message_body)

# raises a Aws::SNS::MessageVerifier::VerificationError on failure
verifier.authenticate!(message_body)

You can re-use a single MessageVerifier instance to authenticate multiple SNS messages.

Defined Under Namespace

Classes: VerificationError

Constant Summary collapse

SIGNABLE_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  'Message',
  'MessageId',
  'Subject',
  'SubscribeURL',
  'Timestamp',
  'Token',
  'TopicArn',
  'Type',
].freeze
AWS_HOSTNAMES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[
  /^sns\.[a-zA-Z0-9\-]{3,}\.amazonaws\.com(\.cn)?$/
]

Instance Method Summary collapse

Constructor Details

#initializeMessageVerifier

Returns a new instance of MessageVerifier.



42
43
44
# File 'lib/aws-sdk-sns/message_verifier.rb', line 42

def initialize
  @cached_pems = {}
end

Instance Method Details

#authentic?(message_body) ⇒ Boolean

Returns ‘true` if the given message has been successfully verified. Returns `false` otherwise.

Parameters:

  • message_body (String<JSON>)

Returns:

  • (Boolean)

    Returns ‘true` if the given message has been successfully verified. Returns `false` otherwise.



49
50
51
52
53
# File 'lib/aws-sdk-sns/message_verifier.rb', line 49

def authentic?(message_body)
  authenticate!(message_body)
rescue VerificationError
  false
end

#authenticate!(message_body) ⇒ Boolean

Returns ‘true` when the given message has been successfully verified.

Parameters:

  • message_body (String<JSON>)

Returns:

  • (Boolean)

    Returns ‘true` when the given message has been successfully verified.

Raises:



60
61
62
63
64
65
66
67
68
69
# File 'lib/aws-sdk-sns/message_verifier.rb', line 60

def authenticate!(message_body)
  msg = Json.load(message_body)
  msg = convert_lambda_msg(msg) if is_from_lambda(msg)
  if public_key(msg).verify(sha1, signature(msg), canonical_string(msg))
    true
  else
    msg = 'the authenticity of the message cannot be verified'
    raise VerificationError, msg
  end
end