class ResoTransport::Authentication::Middleware

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.

Constants

AUTH_HEADER

Public Class Methods

new(app, auth) click to toggle source
Calls superclass method
# File lib/reso_transport/authentication/middleware.rb, line 9
def initialize(app, auth)
  super(app)
  @auth = auth
end

Public Instance Methods

call(request_env) click to toggle source
# File lib/reso_transport/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(request_env, response_env)
    end
  rescue ResoTransport::AccessDenied
    raise if retries.zero?

    @auth.reset
    retries -= 1
    retry
  end
end

Private Instance Methods

authorize_request(request_env) click to toggle source
# File lib/reso_transport/authentication/middleware.rb, line 34
def authorize_request(request_env)
  @auth.ensure_valid_access!

  request_env[:request_headers].merge!(
    AUTH_HEADER => "#{@auth.access.token_type} #{@auth.access.access_token}"
  )
end
raise_if_unauthorized(request_env, response_env) click to toggle source
# File lib/reso_transport/authentication/middleware.rb, line 42
def raise_if_unauthorized(request_env, response_env)
  raise ResoTransport::AccessDenied.new(request_env.to_h, response_env) if response_env[:status] == 401
end