verify_authenticity_token
をオーバーライドしてトークンの検証をしてあげれば良い。
たぶん他のサービスのWebhookにも応用できる。
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
app/controllers/slack_controller.rb
class SlackController < ApplicationController
private
def slack_token
fail NoMethodError.new('please overwrite')
end
def valid_slack_token?
params[:token] == slack_token
end
def verify_authenticity_token
unless valid_slack_token?
logger.warn "WARNING: Can't verify Slack token authenticity" if logger
handle_unverified_request
end
end
end
app/controllers/slack/webhooks_controller.rb
class Slack::WebhooksController < SlackController
def create
head :ok
end
private
def slack_token
ENV['SLACK_TOKEN']
end
end