Sha256: 8148301d91ad39ac02328fdf0ea98ac92570112f1d74f1e5ab5816cd9f476bd1

Contents?: true

Size: 1.97 KB

Versions: 7

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require "flipper/cloud/message_verifier"

module Flipper
  module Cloud
    class Middleware
      # Internal: The path to match for webhook requests.
      WEBHOOK_PATH = %r{\A/webhooks\/?\Z}
      # Internal: The root path to match for requests.
      ROOT_PATH = %r{\A/\Z}

      def initialize(app, options = {})
        @app = app
        @env_key = options.fetch(:env_key, 'flipper')
      end

      def call(env)
        dup.call!(env)
      end

      def call!(env)
        request = Rack::Request.new(env)
        if request.post? && (request.path_info.match(ROOT_PATH) || request.path_info.match(WEBHOOK_PATH))
          status = 200
          headers = {
            Rack::CONTENT_TYPE => "application/json",
          }
          body = "{}"
          payload = request.body.read
          signature = request.env["HTTP_FLIPPER_CLOUD_SIGNATURE"]
          flipper = env.fetch(@env_key)

          begin
            message_verifier = MessageVerifier.new(secret: flipper.sync_secret)
            if message_verifier.verify(payload, signature)
              begin
                flipper.sync
                body = JSON.generate({
                  groups: Flipper.group_names.map { |name| {name: name}}
                })
              rescue Flipper::Adapters::Http::Error => error
                status = error.response.code.to_i == 402 ? 402 : 500
                headers["flipper-cloud-response-error-class"] = error.class.name
                headers["flipper-cloud-response-error-message"] = error.message
              rescue => error
                status = 500
                headers["flipper-cloud-response-error-class"] = error.class.name
                headers["flipper-cloud-response-error-message"] = error.message
              end
            end
          rescue MessageVerifier::InvalidSignature
            status = 400
          end

          [status, headers, [body]]
        else
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
flipper-1.3.2 lib/flipper/cloud/middleware.rb
flipper-1.3.1 lib/flipper/cloud/middleware.rb
flipper-1.3.0 lib/flipper/cloud/middleware.rb
flipper-1.3.0.pre lib/flipper/cloud/middleware.rb
flipper-1.2.2 lib/flipper/cloud/middleware.rb
flipper-1.2.1 lib/flipper/cloud/middleware.rb
flipper-1.2.0 lib/flipper/cloud/middleware.rb