* Twitch Webhook Registration Fix This fixes the broken twitch webhook registration. I previously added the `to_json` when I was switching to using HTTParty and working through WebMock. I apparently forgot to test this manually however after and relied on the specs that had some baked in bad assumptions. This removes the `to_json` and configures the mocking properly in the spec setup. I tested locally that this can now succesfully register for webhooks with Twitch! * Fix Webhook verification digesting as well, hardcode the specs for durability * Refactor to use shorter digest method * Add cache buster lines from Bens branch
42 lines
1 KiB
Ruby
42 lines
1 KiB
Ruby
class TwitchStreamUpdatesController < ApplicationController
|
|
skip_before_action :verify_authenticity_token
|
|
|
|
def show
|
|
if params["hub.mode"] == "denied"
|
|
airbrake_logger.error("Twitch Webhook was denied: #{params.permit('hub.mode', 'hub.reason', 'hub.topic').to_json}")
|
|
head :no_content
|
|
else
|
|
render plain: params["hub.challenge"]
|
|
end
|
|
end
|
|
|
|
def create
|
|
head :no_content
|
|
|
|
unless secret_verified?
|
|
airbrake_logger.warn("Twitch Webhook Recieved for which the webhook could not be verified")
|
|
return
|
|
end
|
|
|
|
user = User.find(params[:user_id])
|
|
|
|
if params[:data].first.present?
|
|
user.update!(currently_streaming_on: :twitch)
|
|
else
|
|
user.update!(currently_streaming_on: nil)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def airbrake_logger
|
|
Airbrake::AirbrakeLogger.new(Rails.logger)
|
|
end
|
|
|
|
def secret_verified?
|
|
twitch_sha = request.headers["x-hub-signature"]
|
|
digest = OpenSSL::HMAC.hexdigest("SHA256", ApplicationConfig["TWITCH_WEBHOOK_SECRET"], request.raw_post)
|
|
|
|
twitch_sha == "sha256=#{digest}"
|
|
end
|
|
end
|