Class: ServiceTemplate::Middleware::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/service_template/middleware/authentication.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Authentication

Returns a new instance of Authentication.



4
5
6
7
8
9
10
# 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

Instance Method Details

#authenticated_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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