Created
October 29, 2013 06:16
-
-
Save komasaru/7209862 to your computer and use it in GitHub Desktop.
Rails controller to receive feeds from the Hub of PubSubHubbub.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JmxController < ApplicationController | |
DATA_DIR = "/path/to/jmx/data/" | |
VERIFY_TOKEN = "hoge" | |
# application_controller 内の | |
# "protect_from_forgery with: :exception" | |
# を無効にする設定 | |
# (ワーニングが出力されないようにするため) | |
skip_before_filter :verify_authenticity_token | |
def index | |
req_method = request.request_method | |
# HTTP リクエスト別に振り分け | |
if req_method == "GET" | |
# 各パラメータの取得 | |
p_mode = params['hub.mode'] | |
p_topic = params['hub.topic'] | |
p_challenge = params['hub.challenge'] | |
p_verify_token = params['hub.verify_token'] | |
# hub.mode チェック | |
if p_mode == "subscribe" || p_mode == "unsubscribe" | |
# hub.verify_token チェック | |
if p_verify_token == VERIFY_TOKEN | |
# Content-type に "text/plain" を指定し、 | |
# challenge コードをそのまま返却 | |
response.headers['Content-Type'] = "text/plain" | |
render text: p_challenge.chomp, status: 200 | |
else | |
render nothing: true, status: 404 | |
end | |
else | |
render nothing: true, status: 404 | |
end | |
elsif req_method == "POST" | |
# リクエストボディの取得 | |
req_body = request.body.read | |
# ヘッダ HTTP_X_HUB_SIGNATURE の値を取得 | |
hub_sig = request.env['HTTP_X_HUB_SIGNATURE'] | |
# HMAC-SHA1 の計算 | |
sha1 = OpenSSL::HMAC::hexdigest(OpenSSL::Digest::SHA1.new, VERIFY_TOKEN, req_body) | |
logger.info "#### hub_sig = #{hub_sig}" | |
logger.info "#### sha1 = #{sha1}" | |
# ファイルとして保存 | |
# ## 実際は、HTTP_X_HUB_SIGNATURE の値と | |
# ## と verigy_token から計算した HMAC-SHA1 が等しい場合のみ処理を行う | |
file_name = "#{Time.now.strftime("%Y%m%d%H%M%S")}_atom.xml" | |
File.open("#{DATA_DIR}#{file_name}", 'wb') { |f| f.write req_body } | |
render nothing: true, status: 200 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good! However, I am not sure the VERIFY_TOKEN usage is the one intended by the spec. As a matter of facts it has been deprecated from the 0.4 version of the PubsubHubbub spec.