Class: ResoWebApi::Authentication::Middleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/reso_web_api/authentication/middleware.rb

Overview

Authentication middleware Ensures that each request is made with proper `Authorization` header set and raises an exception if a request yields a `401 Access Denied` response.

Constant Summary collapse

AUTH_HEADER =
'Authorization'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, auth) ⇒ Middleware

Returns a new instance of Middleware.


9
10
11
12
# File 'lib/reso_web_api/authentication/middleware.rb', line 9

def initialize(app, auth)
  super(app)
  @auth = auth
end

Instance Method Details

#call(request_env) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/reso_web_api/authentication/middleware.rb', line 14

def call(request_env)
  retries = 1

  begin
    authorize_request(request_env)

    @app.call(request_env).on_complete do |response_env|
      raise_if_unauthorized(response_env)
    end
  rescue Errors::AccessDenied
    raise if retries == 0
    @auth.reset
    retries -= 1
    retry
  end
end