Class: BigSession::FaradayBigSession

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/big_session/faraday.rb

Overview

This middleware will add BigSession header to request_headers of outgoing connections done by Faraday

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, header_secret = nil) ⇒ FaradayBigSession

Returns a new instance of FaradayBigSession.



16
17
18
19
# File 'lib/big_session/faraday.rb', line 16

def initialize(app, header_secret = nil)
  @app = app
  @header_secret = header_secret
end

Class Method Details

.activateObject



11
12
13
# File 'lib/big_session/faraday.rb', line 11

def activate
  ::Faraday::Middleware.register_middleware big_session: ::BigSession::FaradayBigSession
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/big_session/faraday.rb', line 21

def call(env)
  if SessionId.current
    headers = { ::BigSession::BIG_SESSION_HEADER_NAME => SessionId.current, }

    if @header_secret
      sig = OpenSSL::HMAC.hexdigest('sha256', @header_secret, SessionId.current)
      headers[::BigSession::BIG_SESSION_SIGNATURE_HEADER_NAME] = sig
    end

    env[:request_headers].merge!(headers)
  end
  @app.call(env)
end