class AWS::SnsMessage

Attributes

origin[RW]
raw[RW]

Public Class Methods

new(sns) click to toggle source
# File lib/aws/sns_message.rb, line 29
def initialize sns
  if sns.is_a? String
    @raw = parse_from sns
  else
    @raw = sns
  end
  @origin = :sns
end

Public Instance Methods

[](key) click to toggle source
# File lib/aws/sns_message.rb, line 38
def [] key
  @raw[key]
end
authentic?() click to toggle source
# File lib/aws/sns_message.rb, line 42
def authentic?
  begin
    decoded_from_base64 = decode signature
    public_key = get_public_key_from signing_cert_url
    public_key.verify OpenSSL::Digest::SHA1.new, decoded_from_base64, canonical_string
  rescue MessageWasNotAuthenticError
    false
  end
end
message() click to toggle source
# File lib/aws/sns_message.rb, line 76
def message
  @raw['Message']
end
message_id() click to toggle source
# File lib/aws/sns_message.rb, line 64
def message_id
  @raw['MessageId']
end
parse_from(json) click to toggle source
# File lib/aws/sns_message.rb, line 108
def parse_from json
  JSON.parse json
end
signature() click to toggle source
# File lib/aws/sns_message.rb, line 84
def signature
  @raw['Signature']
end
signature_version() click to toggle source
# File lib/aws/sns_message.rb, line 88
def signature_version
  @raw['SignatureVersion']
end
signing_cert_url() click to toggle source
# File lib/aws/sns_message.rb, line 92
def signing_cert_url
  @raw['SigningCertURL']
end
subject() click to toggle source
# File lib/aws/sns_message.rb, line 72
def subject
  @raw['Subject']
end
subscribe_url() click to toggle source
# File lib/aws/sns_message.rb, line 100
def subscribe_url
  @raw['SubscribeURL']
end
timestamp() click to toggle source
# File lib/aws/sns_message.rb, line 80
def timestamp
  @raw['Timestamp']
end
token() click to toggle source
# File lib/aws/sns_message.rb, line 104
def token
  @raw['Token']
end
topic_arn() click to toggle source
# File lib/aws/sns_message.rb, line 68
def topic_arn
  @raw['TopicArn']
end
type() click to toggle source
# File lib/aws/sns_message.rb, line 52
def type
  case when @raw['Type'] =~ /SubscriptionConfirmation/i
    then :SubscriptionConfirmation
  when @raw['Type'] =~ /Notification/i
    then :Notification
  when @raw['Type'] =~ /UnsubscribeConfirmation/i
    then :UnsubscribeConfirmation
  else
    :unknown
  end
end
unsubscribe_url() click to toggle source
# File lib/aws/sns_message.rb, line 96
def unsubscribe_url
  @raw['UnsubscribeURL']
end

Protected Instance Methods

canonical_string() click to toggle source
# File lib/aws/sns_message.rb, line 123
def canonical_string
  if type == :SubscriptionConfirmation
    text = "Message\n#{message}\n"
    text += "MessageId\n#{message_id}\n"
    text += "SubscribeURL\n#{subscribe_url}\n"
    text += "Timestamp\n#{timestamp}\n"
    text += "Token\n#{token}\n"
    text += "TopicArn\n#{topic_arn}\n"
    text += "Type\n#{type}\n"         
  else
    text = "Message\n#{message}\n"
    text += "MessageId\n#{message_id}\n"
    text += "Subject\n#{subject}\n" unless subject.nil? or subject.empty?
    text += "Timestamp\n#{timestamp}\n"
    text += "TopicArn\n#{topic_arn}\n"
    text += "Type\n#{type}\n"
  end
  text
end
decode(raw) click to toggle source
# File lib/aws/sns_message.rb, line 113
def decode raw
  Base64.decode64 raw
end
download(url) click to toggle source
# File lib/aws/sns_message.rb, line 143
def download url
  raise MessageWasNotAuthenticError, "cert is not hosted at AWS URL (https): #{url}" unless url =~ /^https.*amazonaws\.com\/.*$/i
  tries = 0
  begin
    response = HTTParty.get url
    response.body
  rescue => msg
    tries += 1
    retry if tries <= 3
    raise StandardError, "SNS signing cert could not be retrieved after #{tries} tries.\n#{msg}"
  end
end
get_public_key_from(x509_pem_url) click to toggle source
# File lib/aws/sns_message.rb, line 117
def get_public_key_from(x509_pem_url)
  cert_pem = download x509_pem_url
  x509 = OpenSSL::X509::Certificate.new(cert_pem)
  OpenSSL::PKey::RSA.new(x509.public_key)
end