Class: Aws::SNS::MessageVerifier

Inherits:
Object
  • Object
show all
Defined in:
gems/aws-sdk-sns/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

Instance Method Summary collapse

Constructor Details

#initialize(http_options = {}) ⇒ MessageVerifier

Returns a new instance of MessageVerifier.

Parameters:

  • http_options (Hash) (defaults to: {})

    Supported options to be passed to Net::HTTP.

Options Hash (http_options):



46
47
48
49
# File 'gems/aws-sdk-sns/lib/aws-sdk-sns/message_verifier.rb', line 46

def initialize(http_options = {})
  @cached_pems = {}
  @http_proxy = http_options[:http_proxy]
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.



54
55
56
57
58
# File 'gems/aws-sdk-sns/lib/aws-sdk-sns/message_verifier.rb', line 54

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:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'gems/aws-sdk-sns/lib/aws-sdk-sns/message_verifier.rb', line 65

def authenticate!(message_body)
  msg = Json.load(message_body)
  msg = convert_lambda_msg(msg) if is_from_lambda(msg)

  case msg['SignatureVersion']
  when '1'
    verify!(msg, sha1)
  when '2'
    verify!(msg, sha256)
  else
    error_msg = 'Invalid SignatureVersion'
    raise VerificationError, error_msg
  end
end