5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-04 00:25:46 +00:00
panel/app/controllers/api/v1/notices_controller.rb

47 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Api
module V1
# Recibe notificaciones desde Airbrake
#
# TODO: Autenticación
class NoticesController < BaseController
skip_before_action :verify_authenticity_token
# Generar un stacktrace en segundo plano y enviarlo por correo
# solo si la API key es verificable. Del otro lado siempre
# respondemos con lo mismo.
def create
if verify_api_key
BacktraceJob.perform_later site_id: params[:site_id],
errors: airbrake_params.map(&:permit!).map(&:to_h)
end
render status: 201, json: { id: 1, url: root_url }
end
private
# XXX: Por alguna razón Airbrake envía los datos con Content-Type:
# text/plain.
def airbrake_params
@airbrake_params ||= params.merge!(JSON.parse(request.raw_post) || {}).require(:errors)
end
def site
@site ||= Site.find(params[:site_id])
end
def verify_api_key
site.verifier.verify(airbrake_token, purpose: :airbrake) === Site::Api::AIRBRAKE_SECRET
rescue ActiveSupport::MessageVerifier::InvalidSignature
false
end
def airbrake_token
@airbrake_token ||= params[:key]
end
end
end
end