pubsubhubbub-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03e9b6b69ab33bb77de497c4f4ab2390c07e042a
4
+ data.tar.gz: cd017828e6c4251cdad04a1bf1a567d19b892004
5
+ SHA512:
6
+ metadata.gz: f479aa3bcbdb92e5f6ca7b0f63360d2c6b678b7f65d3f8f0831076264e90151cafcc77717581fd92d5cfeeb2f6f61ed2fc935ee8cc5be00bb2cf9454b76cd974
7
+ data.tar.gz: 70e2f01dc594db5732642cc78c9fd50f81c94a8abd54b44556cba23e7030c36159c5d25416230be3919b4a5b2de861d7d96586379a3c804ee3b619207214f249
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Eugen Rochko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Pubsubhubbub
2
+
3
+ This is a mountable PubSubHubbub server conforming to the v0.4 of the spec.
4
+
5
+ ## Usage
6
+
7
+ In `routes.rb`:
8
+
9
+ mount Pubsubhubbub::Engine => '/pubsubhubbub', as: :pubsubhubbub
10
+
11
+ In feed:
12
+
13
+ <link rel="hub" href="<%= pubsubhubbub_hub_url %>" />
14
+
15
+ ## Installation
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'pubsubhubbub-rails'
20
+ ```
21
+
22
+ And then execute:
23
+ ```bash
24
+ $ bundle
25
+ ```
26
+
27
+ Or install it yourself as:
28
+ ```bash
29
+ $ gem install pubsubhubbub-rails
30
+ ```
31
+
32
+ ## License
33
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Pubsubhubbub'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ module Pubsubhubbub
3
+ class ApplicationController < ActionController::Base
4
+ end
5
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+ require_dependency 'pubsubhubbub/application_controller'
3
+
4
+ module Pubsubhubbub
5
+ class SubscriptionsController < ApplicationController
6
+ def index
7
+ @callback = params['hub.callback']
8
+ @mode = params['hub.mode']
9
+ @topic = params['hub.topic']
10
+ @lease_seconds = params['hub.lease_seconds']
11
+ @secret = params['hub.secret']
12
+ @url = params['hub.url']
13
+
14
+ case @mode
15
+ when 'subscribe'
16
+ subscribe
17
+ when 'unsubscribe'
18
+ unsubscribe
19
+ when 'publish'
20
+ publish
21
+ else
22
+ render plain: 'Unknown mode', status: :unprocessable_entity
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def check_topic_hub
29
+ uri = Addressable::URI.parse(@topic)
30
+ response = HTTP.get(uri)
31
+
32
+ if response['Link']
33
+ link = LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
34
+ hub = link.find_link(%w(rel hub))
35
+ topic = link.find_link(%w(rel self))
36
+
37
+ return true if hub&.href == hub_url && topic&.href == @topic
38
+ end
39
+
40
+ xml = Nokogiri::XML(response.body)
41
+ xml.encoding = 'utf-8'
42
+
43
+ link = xml.at_xpath('//xmlns:link[@rel="hub"]')
44
+ topic = xml.at_xpath('//xmlns:link[@rel="self"]')
45
+
46
+ link['href'] == hub_url && topic['href'] == @topic
47
+ end
48
+
49
+ def subscribe
50
+ render(plain: 'Missing callback URL', status: :unprocessable_entity) && return if @callback.blank?
51
+ render(plain: 'Missing topic URL', status: :unprocessable_entity) && return if @topic.blank?
52
+ render(plain: 'Topic advertises a different hub or topic URL', status: :unprocessable_entity) && return unless check_topic_hub
53
+
54
+ @subscription = Subscription.where(topic: @topic, callback: @callback).first_or_initialize(topic: @topic, callback: @callback, mode: @mode, secret: @secret)
55
+ @subscription.lease_seconds = @lease_seconds
56
+ @subscription.save!
57
+
58
+ VerifyIntentJob.perform_later(@subscription.id)
59
+
60
+ head 202
61
+ end
62
+
63
+ def unsubscribe
64
+ render(plain: 'Missing callback URL', status: :unprocessable_entity) && return if @callback.blank?
65
+ render(plain: 'Missing topic URL', status: :unprocessable_entity) && return if @topic.blank?
66
+ render(plain: 'Topic advertises a different hub or topic URL', status: :unprocessable_entity) && return unless check_topic_hub
67
+
68
+ @subscription = Subscription.where(topic: @topic, callback: @callback).first
69
+
70
+ head 202 && return if @subscription.nil?
71
+ @subscription.destroy && head(202) && return unless @subscription.confirmed?
72
+
73
+ @subscription.update!(mode: @mode)
74
+ VerifyIntentJob.perform_later(@subscription.id)
75
+
76
+ head 202
77
+ end
78
+
79
+ def publish
80
+ render(plain: 'Missing URL', status: :unprocessable_entity) && return if @url.blank?
81
+ FetchTopicJob.perform_later(hub_url, @url)
82
+ head 202
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ module Pubsubhubbub
3
+ class ApplicationJob < ActiveJob::Base
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pubsubhubbub
4
+ class DeliverPayloadJob < ApplicationJob
5
+ queue_as :push
6
+
7
+ def perform(hub_url, subscription_id, current_payload)
8
+ subscription = Subscription.find(subscription_id)
9
+ link = LinkHeader.new([[hub_url, [%w(rel hub)]], [subscription.topic, [%w(rel self)]]])
10
+ headers = {}
11
+
12
+ headers['Link'] = link
13
+ headers['X-Hub-Signature'] = sign_payload(subscription.secret, current_payload) if subscription.secret
14
+
15
+ response = HTTP.timeout(:per_operation, write: 60, connect: 20, read: 60)
16
+ .headers(headers)
17
+ .post(subscription.callback, body: current_payload)
18
+
19
+ raise FailedDeliveryError unless response.code > 199 && response.code < 300
20
+ end
21
+
22
+ private
23
+
24
+ def sign_payload(secret, payload)
25
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), secret, payload)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ module Pubsubhubbub
3
+ class FetchTopicJob < ApplicationJob
4
+ queue_as :push
5
+
6
+ def perform(hub_url, url)
7
+ return unless Subscription.where(topic: url).active.any?
8
+ current_payload = HTTP.timeout(:per_operation, write: 60, connect: 20, read: 60).get(url).body.to_s
9
+
10
+ Subscription.where(topic: url).active.find_each do |subscription|
11
+ DeliverPayloadJob.perform_later(hub_url, subscription.id, current_payload)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ module Pubsubhubbub
3
+ class VerifyIntentJob < ApplicationJob
4
+ queue_as :push
5
+
6
+ def perform(subscription_id)
7
+ subscription = Subscription.find(subscription_id)
8
+ verified = verify_intent(subscription)
9
+
10
+ if verified && subscription.mode == 'subscribe'
11
+ subscription.update(confirmed: true)
12
+ else
13
+ subscription.destroy
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def verify_intent(subscription)
20
+ uri = Addressable::URI.parse(subscription.callback)
21
+ response = HTTP.timeout(:per_operation, write: 60, connect: 20, read: 60).get(uri, params: { 'hub.mode' => subscription.mode, 'hub.topic' => subscription.topic, 'hub.challenge' => subscription.challenge, 'hub.lease_seconds' => subscription.lease_seconds })
22
+ response.code > 199 && response.code < 300 && response.body.to_s == subscription.challenge
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module Pubsubhubbub
3
+ class ApplicationRecord < ActiveRecord::Base
4
+ self.abstract_class = true
5
+ end
6
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ module Pubsubhubbub
3
+ class Subscription < ApplicationRecord
4
+ validates :topic, :callback, :mode, presence: true
5
+
6
+ scope :active, -> { where(confirmed: true).where('expires_at > ?', Time.now.utc) }
7
+
8
+ def lease_seconds
9
+ (expires_at - Time.now.utc).to_i
10
+ end
11
+
12
+ def lease_seconds=(seconds)
13
+ self.expires_at = Time.now.utc + [[3600 * 24, seconds.to_i].max, 3600 * 24 * 30].min.seconds
14
+ end
15
+
16
+ before_validation :generate_challenge
17
+
18
+ private
19
+
20
+ def generate_challenge
21
+ self.challenge = SecureRandom.hex
22
+ end
23
+ end
24
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ Pubsubhubbub::Engine.routes.draw do
3
+ post '/', to: 'subscriptions#index', as: :hub
4
+ end
@@ -0,0 +1,17 @@
1
+ class CreatePubsubhubbubSubscriptions < ActiveRecord::Migration[5.0]
2
+ def change
3
+ create_table :pubsubhubbub_subscriptions do |t|
4
+ t.string :topic, null: false, default: ''
5
+ t.string :callback, null: false, default: ''
6
+ t.string :mode, null: false, default: ''
7
+ t.string :challenge, null: false, default: ''
8
+ t.string :secret, null: true, default: nil
9
+ t.boolean :confirmed, null: false, default: false
10
+ t.datetime :expires_at, null: false
11
+
12
+ t.timestamps
13
+ end
14
+
15
+ add_index :pubsubhubbub_subscriptions, [:topic, :callback], unique: true
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ require 'pubsubhubbub/engine'
3
+ require 'addressable/uri'
4
+ require 'nokogiri'
5
+ require 'http'
6
+ require 'link_header'
7
+ require 'httplog'
8
+
9
+ module Pubsubhubbub
10
+ class FailedDeliveryError < StandardError
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module Pubsubhubbub
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Pubsubhubbub
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Pubsubhubbub
3
+ VERSION = '0.1.0'
4
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pubsubhubbub-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eugen Rochko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.0.0.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 5.0.0
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.0.0.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: addressable
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.4'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.4'
47
+ - !ruby/object:Gem::Dependency
48
+ name: http
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: nokogiri
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.4'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.4'
75
+ - !ruby/object:Gem::Dependency
76
+ name: link_header
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: sqlite3
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: pry-rails
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: httplog
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ description: A PubSubHubbub server conforming to the v0.4 spec, mountable from a Rails
132
+ app
133
+ email:
134
+ - eugen@zeonfederated.com
135
+ executables: []
136
+ extensions: []
137
+ extra_rdoc_files: []
138
+ files:
139
+ - MIT-LICENSE
140
+ - README.md
141
+ - Rakefile
142
+ - app/controllers/pubsubhubbub/application_controller.rb
143
+ - app/controllers/pubsubhubbub/subscriptions_controller.rb
144
+ - app/jobs/pubsubhubbub/application_job.rb
145
+ - app/jobs/pubsubhubbub/deliver_payload_job.rb
146
+ - app/jobs/pubsubhubbub/fetch_topic_job.rb
147
+ - app/jobs/pubsubhubbub/verify_intent_job.rb
148
+ - app/models/pubsubhubbub/application_record.rb
149
+ - app/models/pubsubhubbub/subscription.rb
150
+ - config/routes.rb
151
+ - db/migrate/20161126203748_create_pubsubhubbub_subscriptions.rb
152
+ - lib/pubsubhubbub.rb
153
+ - lib/pubsubhubbub/engine.rb
154
+ - lib/pubsubhubbub/version.rb
155
+ homepage: https://github.com/Gargron/pubsubhubbub
156
+ licenses:
157
+ - MIT
158
+ metadata: {}
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubyforge_project:
175
+ rubygems_version: 2.5.1
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: A PubSubHubbub server conforming to the v0.4 spec, mountable from a Rails
179
+ app
180
+ test_files: []