class Motor::Configs::SyncMiddleware

Constants

KeyNotSpecified
NotAuthenticated

Public Class Methods

new(app) click to toggle source
# File lib/motor/configs/sync_middleware.rb, line 9
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/motor/configs/sync_middleware.rb, line 13
def call(env)
  if env['PATH_INFO'] == Motor::Configs::SYNC_API_PATH
    authenticate!(env['HTTP_X_AUTHORIZATION'])

    case env['REQUEST_METHOD']
    when 'GET'
      respond_with_configs
    when 'POST'
      input = env['rack.input']
      input.rewind
      sync_configs(input.read)
    else
      @app.call(env)
    end
  else
    @app.call(env)
  end
rescue NotAuthenticated
  [403, {}, ['Invalid synchronization API key']]
rescue KeyNotSpecified
  [404, {}, ['Set `MOTOR_SYNC_API_KEY` environment variable in order to sync configs']]
end

Private Instance Methods

authenticate!(token) click to toggle source
# File lib/motor/configs/sync_middleware.rb, line 38
def authenticate!(token)
  raise KeyNotSpecified if Motor::Configs::SYNC_ACCESS_KEY.blank?
  raise NotAuthenticated if token.blank?

  is_token_valid =
    ActiveSupport::SecurityUtils.secure_compare(
      Digest::SHA256.hexdigest(token),
      Digest::SHA256.hexdigest(Motor::Configs::SYNC_ACCESS_KEY)
    )

  raise NotAuthenticated unless is_token_valid
end
respond_with_configs() click to toggle source
# File lib/motor/configs/sync_middleware.rb, line 51
def respond_with_configs
  [
    200,
    { 'Content-Type' => 'application/json' },
    [Motor::Configs::BuildConfigsHash.call.to_json]
  ]
rescue StandardError => e
  [500, {}, [e.message]]
end
sync_configs(body) click to toggle source
# File lib/motor/configs/sync_middleware.rb, line 61
def sync_configs(body)
  configs_hash = JSON.parse(body)

  Motor::Configs::SyncFromHash.call(configs_hash)

  [200, {}, []]
rescue StandardError => e
  [500, {}, [e.message]]
end