Class: Pechkin::Auth::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/pechkin/auth.rb

Overview

Auth middleware to check if provided auth can be found in .htpasswd file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, auth_file:) ⇒ Middleware

Returns a new instance of Middleware.



22
23
24
25
# File 'lib/pechkin/auth.rb', line 22

def initialize(app, auth_file:)
  @htpasswd = HTAuth::PasswdFile.open(auth_file) if File.exist?(auth_file)
  @app = app
end

Instance Attribute Details

#htpasswdObject (readonly)

Returns the value of attribute htpasswd.



20
21
22
# File 'lib/pechkin/auth.rb', line 20

def htpasswd
  @htpasswd
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pechkin/auth.rb', line 27

def call(env)
  if authorized?(env)
    @app.call(env)
  else
    body = { status: 'error', reason: 'unathorized' }.to_json
    ['401', { 'Content-Type' => 'application/json' }, [body]]
  end
rescue StandardError => e
  puts e.backtrace.reverse.join('\n\t')
  body = { status: 'error', reason: e.message }.to_json
  ['503', { 'Content-Type' => 'application/json' }, [body]]
end