Class: Faraday::RbacAuthToken

Inherits:
Middleware
  • Object
show all
Defined in:
lib/scooter/middleware/rbac_auth_token.rb

Overview

This middleware checks for tokens and adds them to the request when found. To include this correctly, you will need to pass the dispatcher itself to the initialize method so it can read if the dispatcher has tokens or not. Beyond just the token itself, it also checks the dispatcher if the instance variable send_auth_token_as_query_param is set to true. Otherwise, it will always attach it as an X-Authentication header.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, dispatcher) ⇒ RbacAuthToken

Returns a new instance of RbacAuthToken.

Parameters:

  • app()

    Structural requirement for Faraday::Middleware initialization

  • dispatcher (Scooter::HttpDispatchers::ConsoleDispatcher required)

    Required param so that the middleware can check to see if there is a token set in the implementing dispatcher class. Will work with ConsoleDispatcher.



17
18
19
20
# File 'lib/scooter/middleware/rbac_auth_token.rb', line 17

def initialize(app, dispatcher)
  super(app)
  @dispatcher = dispatcher
end

Instance Attribute Details

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



9
10
11
# File 'lib/scooter/middleware/rbac_auth_token.rb', line 9

def dispatcher
  @dispatcher
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/scooter/middleware/rbac_auth_token.rb', line 22

def call(env)
  if dispatcher.token && dispatcher.send_auth_token_as_query_param
    query_array = [['token', dispatcher.token]]
    URI.decode_www_form(env.url.query).each {|tuple| query_array << tuple} if env.url.query
    env.url.query = URI.encode_www_form(query_array)
  elsif dispatcher.token
    env.request_headers['X-Authentication'] = dispatcher.token
  end
  @app.call env
end