Sha256: 7466abc28b1e616af3b5fe3470691e600a00e8aa4f5c74fceb868624cfbce30e

Contents?: true

Size: 902 Bytes

Versions: 3

Compression:

Stored size: 902 Bytes

Contents

module ServiceTemplate
  class Middleware
    class Authentication
      def initialize(app)
        @app = app

        if ENV['HEADER_PASSWORDS']
          @allowed_passwords = ENV['HEADER_PASSWORDS'].split(',').map { |pw| pw.strip }.freeze
        end
      end

      def call(env)
        if authenticated_request?(env)
          @app.call(env)
        else
          if @allowed_passwords
            error_response = ServiceTemplate::JsonError.new('bad_password', 'bad password').to_json
          else
            error_response = ServiceTemplate::JsonError.new('not_configured', 'password not configured').to_json
          end

          [401, { 'Content-type' => 'application/json' }, Array.wrap(error_response)]
        end

      end

      def authenticated_request?(env)
        @allowed_passwords.include? env['HTTP_PASSWORD'] unless @allowed_passwords.nil?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
service_template-0.5.2 lib/service_template/middleware/authentication.rb
service_template-0.5.1 lib/service_template/middleware/authentication.rb
service_template-0.5.0 lib/service_template/middleware/authentication.rb