Class: AppIdentity::FaradayMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/app_identity/faraday_middleware.rb

Overview

A Faraday middleware that generates an app identity proof header for a request.

The ‘options` provided has the following parameters:

  • ‘app`: (required) An AppIdentity::App or object that can be coerced into an AppIdentity::app with AppIdentity#new.

  • ‘disallowed`: A list of algorithm versions that are not allowed when processing received identity proofs. See AppIdentity::Versions.allowed?.

  • ‘header`: (required) The header to use for sending the app identity proof.

  • ‘on_failure`: (optional) The action to take when an app identity proof cannot be generated for any reason. May be one of the following values:

    - `:fail`: Throws an exception. This is the default if `on_failure` is
      not specified.
    
    - `:pass`: Sets the header to the empty value returned. The request will
      probably fail on the receiving server side.
    
    - `:skip`: Does not add the header, as if the request were not made
      using an application.
    

‘on_failure` may also be provided a callable object that expects three parameters:

  • ‘env`: The Faraday middleware `env` value;

  • ‘app`: The identity app value provided to the middleware; and

  • ‘header`: The header name provided to the middleware.

The callable may return either the ‘env`, `:fail`, `:skip`, or `:pass`. Any other value will be treated as `:fail`.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ FaradayMiddleware

:nodoc:



41
42
43
44
45
46
47
48
# File 'lib/app_identity/faraday_middleware.rb', line 41

def initialize(app, options = {}) # :nodoc:
  super(app)

  @identity_app = AppIdentity::App.new(options.fetch(:app))
  @header = options.fetch(:header).downcase
  @on_failure = options.fetch(:on_failure, :fail)
  @disallowed = options.fetch(:disallowed, nil)
end

Instance Method Details

#call(env) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/app_identity/faraday_middleware.rb', line 50

def call(env) # :nodoc:
  proof = AppIdentity.generate_proof(@identity_app, disallowed: @disallowed)

  if proof.nil?
    handle_failure(@on_failure)
  else
    env[:request_headers][@header] = proof
  end

  @app.call(env)
end