aws_sns_subscription 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,13 @@
1
1
  module AWSSNSSubscription
2
2
  module Confirmer
3
3
  extend ActiveSupport::Concern
4
+ class MessageWasNotAuthentic < StandardError; end
4
5
 
5
6
  def respond_to_aws_sns_subscription_confirmations
6
7
  if request.headers["x-amz-sns-message-type"] == "SubscriptionConfirmation"
7
- HTTParty.get JSON.parse(request.raw_post)["SubscribeURL"]
8
+ sns = SNS.new(request.raw_post)
9
+ raise MessageWasNotAuthentic unless sns.authentic?
10
+ HTTParty.get sns.subscribe_url
8
11
  head :ok and return
9
12
  end
10
13
  end
@@ -0,0 +1,19 @@
1
+ module AWSSNSSubscription
2
+ class SNS
3
+
4
+ attr_reader :raw
5
+
6
+ def initialize(json)
7
+ @raw = json
8
+ end
9
+
10
+ def subscribe_url
11
+ JSON.parse(raw)["SubscribeURL"]
12
+ end
13
+
14
+ def authentic?
15
+ subscribe_url =~ /^https.*amazonaws\.com\//
16
+ end
17
+
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module AWSSNSSubscription
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require 'httparty'
2
2
  require 'active_support/all'
3
+ require 'aws-sdk'
3
4
  require 'require_all'
4
5
  require_rel 'aws_sns_subscription'
5
6
 
@@ -2,6 +2,7 @@ require 'spec_helper'
2
2
  require 'active_support/all'
3
3
  require 'httparty'
4
4
  require File.expand_path('../../lib/aws_sns_subscription/confirmer.rb', __FILE__)
5
+ require File.expand_path('../../lib/aws_sns_subscription/sns.rb', __FILE__)
5
6
 
6
7
  module AWSSNSSubscription
7
8
  class Controller
@@ -15,6 +16,9 @@ module AWSSNSSubscription
15
16
  let(:raw_post) do
16
17
  File.read(File.expand_path("../fixtures/raw_post.txt", __FILE__))
17
18
  end
19
+ let(:raw_bologna_post) do
20
+ File.read(File.expand_path("../fixtures/raw_bologna_post.txt", __FILE__))
21
+ end
18
22
  before(:each) do
19
23
  subject.stub(:request).and_return(mock("request", headers: { "x-amz-sns-message-type" => "SubscriptionConfirmation" }, raw_post: raw_post))
20
24
  end
@@ -22,6 +26,14 @@ module AWSSNSSubscription
22
26
  HTTParty.should_receive(:get).with("https://sns.us-east-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-east-1:123456789012:MyTopic&Token=2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736")
23
27
  subject.respond_to_aws_sns_subscription_confirmations
24
28
  end
29
+ describe "with a bologna endpoint" do
30
+ before(:each) do
31
+ subject.stub(:request).and_return(mock("request", headers: { "x-amz-sns-message-type" => "SubscriptionConfirmation" }, raw_post: raw_bologna_post))
32
+ end
33
+ it "should raise the right exception" do
34
+ expect { subject.respond_to_aws_sns_subscription_confirmations }.to raise_error AWSSNSSubscription::Confirmer::MessageWasNotAuthentic
35
+ end
36
+ end
25
37
  end
26
38
  describe "not receiving a subscription confirmation request" do
27
39
  before(:each) do
@@ -0,0 +1,12 @@
1
+ {
2
+ "Type" : "SubscriptionConfirmation",
3
+ "MessageId" : "165545c9-2a5c-472c-8df2-7ff2be2b3b1b",
4
+ "Token" : "2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736",
5
+ "TopicArn" : "arn:aws:sns:us-east-1:123456789012:MyTopic",
6
+ "Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-east-1:123456789012:MyTopic.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
7
+ "SubscribeURL" : "http://bologna.com",
8
+ "Timestamp" : "2012-04-26T20:45:04.751Z",
9
+ "SignatureVersion" : "1",
10
+ "Signature" : "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=",
11
+ "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem"
12
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_sns_subscription
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -92,8 +92,10 @@ files:
92
92
  - lib/aws_sns_subscription.rb
93
93
  - lib/aws_sns_subscription/confirmer.rb
94
94
  - lib/aws_sns_subscription/railtie.rb
95
+ - lib/aws_sns_subscription/sns.rb
95
96
  - lib/aws_sns_subscription/version.rb
96
97
  - spec/confirmer_spec.rb
98
+ - spec/fixtures/raw_bologna_post.txt
97
99
  - spec/fixtures/raw_post.txt
98
100
  - spec/spec_helper.rb
99
101
  homepage: ''
@@ -122,5 +124,6 @@ specification_version: 3
122
124
  summary: Auto responds to AWS SNS subscription confirmations
123
125
  test_files:
124
126
  - spec/confirmer_spec.rb
127
+ - spec/fixtures/raw_bologna_post.txt
125
128
  - spec/fixtures/raw_post.txt
126
129
  - spec/spec_helper.rb