Class: Pechkin::Auth::Middleware
- Inherits:
-
Object
- Object
- Pechkin::Auth::Middleware
- Defined in:
- lib/pechkin/auth.rb
Overview
Auth middleware to check if provided auth can be found in .htpasswd file
Instance Attribute Summary collapse
-
#htpasswd ⇒ Object
readonly
Returns the value of attribute htpasswd.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, auth_file:) ⇒ Middleware
constructor
A new instance of Middleware.
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
#htpasswd ⇒ Object (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 (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. }.to_json ['503', { 'Content-Type' => 'application/json' }, [body]] end |