Class: FmRest::V1::TokenSession

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/fmrest/v1/token_session.rb

Overview

FM Data API authentication middleware using the credentials strategy

Defined Under Namespace

Classes: NoSessionTokenSet

Constant Summary collapse

HEADER_KEY =
"Authorization".freeze
TOKEN_STORE_INTERFACE =
[:load, :store, :delete].freeze
LOGOUT_PATH_MATCHER =
%r{\A(#{FmRest::V1::Connection::BASE_PATH}/[^/]+/sessions/)[^/]+\Z}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, options = FmRest.default_connection_settings) ⇒ TokenSession

Returns a new instance of TokenSession.

Parameters:

  • app (#call)
  • options (Hash) (defaults to: FmRest.default_connection_settings)


19
20
21
22
# File 'lib/fmrest/v1/token_session.rb', line 19

def initialize(app, options = FmRest.default_connection_settings)
  super(app)
  @options = options
end

Instance Method Details

#call(env) ⇒ Object

Entry point for the middleware when sending a request



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fmrest/v1/token_session.rb', line 26

def call(env)
  return handle_logout(env) if is_logout_request?(env)

  set_auth_header(env)

  request_body = env[:body] # After failure env[:body] is set to the response body

  @app.call(env).on_complete do |response_env|
    if response_env[:status] == 401 # Unauthorized
      env[:body] = request_body
      token_store.delete(token_store_key)
      set_auth_header(env)
      return @app.call(env)
    end
  end
end