Class: FmRest::V1::TokenSession

Inherits:
Faraday::Middleware
  • Object
show all
Includes:
TokenStore
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"
LOGOUT_PATH_MATCHER =
%r{\A(#{FmRest::V1::Connection::DATABASES_PATH}/[^/]+/sessions/)[^/]+\Z}.freeze

Constants included from TokenStore

FmRest::V1::TokenStore::ActiveRecord, FmRest::V1::TokenStore::Memory

Instance Method Summary collapse

Constructor Details

#initialize(app, settings) ⇒ TokenSession

Returns a new instance of TokenSession.

Parameters:



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

def initialize(app, settings)
  super(app)
  @settings = settings
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
42
43
44
# 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
      delete_token_store_key

      if @settings.autologin
        env[:body] = request_body
        set_auth_header(env)
        return @app.call(env)
      end
    end
  end
end