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.



25
26
27
28
# File 'lib/pechkin/auth.rb', line 25

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.



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

def htpasswd
  @htpasswd
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  authorize(env)
  @app.call(env)
rescue AuthError => e
  body = { status: 'error', reason: e.message }.to_json
  ['401', { 'Content-Type' => 'application/json' }, [body]]
rescue StandardError => e
  body = { status: 'error', reason: e.message }.to_json
  ['503', { 'Content-Type' => 'application/json' }, [body]]
end