Class: ForestAdminRpcAgent::Middleware::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/forest_admin_rpc_agent/middleware/authentication.rb

Constant Summary collapse

ALLOWED_TIME_DIFF =
300
SIGNATURE_REUSE_WINDOW =
5
@@used_signatures =
{}
@@signatures_mutex =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Authentication



11
12
13
# File 'lib/forest_admin_rpc_agent/middleware/authentication.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/forest_admin_rpc_agent/middleware/authentication.rb', line 15

def call(env)
  request = Rack::Request.new(env)
  signature = request.get_header('HTTP_X_SIGNATURE')
  timestamp = request.get_header('HTTP_X_TIMESTAMP')

  unless valid_signature?(signature, timestamp)
    return [401, { 'Content-Type' => 'application/json' }, [{ error: 'Unauthorized' }.to_json]]
  end

  status, headers, response = @app.call(env)

  if request.get_header('HTTP_FOREST_CALLER')
    caller = ForestAdminDatasourceToolkit::Components::Caller.new(
      **(JSON.parse(request.get_header('HTTP_FOREST_CALLER')).symbolize_keys)
    )
    headers = headers.merge({ caller: caller })
  end

  [status, headers, response]
end