2020-12-07 16:21:46 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Site
|
|
|
|
module Api
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
encrypts :api_key
|
|
|
|
before_save :add_api_key_if_missing!
|
|
|
|
|
2021-01-25 19:05:35 +00:00
|
|
|
# Genera mensajes secretos que podemos usar para la API de cada
|
|
|
|
# sitio.
|
|
|
|
#
|
|
|
|
# XXX: Si no se configura una API key del sitio o genérica, no
|
|
|
|
# tenemos forma de verificar los mensajes, pero la generación de
|
|
|
|
# llaves no va a fallar.
|
2020-12-07 16:21:46 +00:00
|
|
|
def verifier
|
2021-01-25 19:05:35 +00:00
|
|
|
@verifier ||= ActiveSupport::MessageVerifier.new(api_key || Rails.application.credentials.api_key || SecureRandom.hex(64))
|
2020-12-07 16:21:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def airbrake_api_key
|
2021-01-25 19:05:35 +00:00
|
|
|
@airbrake_api_key ||= verifier.generate(airbrake_secret, purpose: :airbrake)
|
2020-12-07 16:21:46 +00:00
|
|
|
end
|
|
|
|
|
2021-02-02 21:52:54 +00:00
|
|
|
def airbrake_valid?(token)
|
|
|
|
ActiveSupport::SecurityUtils.secure_compare(verifier.verify(token, purpose: :airbrake), airbrake_secret)
|
|
|
|
rescue ActiveSupport::MessageVerifier::InvalidSignature
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-12-07 16:21:46 +00:00
|
|
|
private
|
|
|
|
|
2021-01-25 19:05:35 +00:00
|
|
|
def airbrake_secret
|
|
|
|
Rails.application.credentials.airbrake || SecureRandom.hex(64)
|
|
|
|
end
|
|
|
|
|
2020-12-07 16:21:46 +00:00
|
|
|
# Asegurarse que el sitio tenga una llave para la API
|
|
|
|
def add_api_key_if_missing!
|
|
|
|
self.api_key ||= SecureRandom.hex(64)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|