class ServiceTemplate::Middleware::Authentication

Public Class Methods

new(app) click to toggle source
# File lib/service_template/middleware/authentication.rb, line 4
def initialize(app)
  @app = app

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

Public Instance Methods

authenticated_request?(env) click to toggle source
# File lib/service_template/middleware/authentication.rb, line 27
def authenticated_request?(env)
  @allowed_passwords.include? env['HTTP_PASSWORD'] unless @allowed_passwords.nil?
end
call(env) click to toggle source
# File lib/service_template/middleware/authentication.rb, line 12
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