Class: Ridley::Middleware::ChefAuth

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/ridley/middleware/chef_auth.rb

Overview

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, client_name, client_key) ⇒ ChefAuth

Returns a new instance of ChefAuth.



10
11
12
13
14
# File 'lib/ridley/middleware/chef_auth.rb', line 10

def initialize(app, client_name, client_key)
  super(app)
  @client_name = client_name
  @client_key = OpenSSL::PKey::RSA.new(File.read(client_key))
end

Instance Attribute Details

#client_keyObject (readonly)

Returns the value of attribute client_key.



8
9
10
# File 'lib/ridley/middleware/chef_auth.rb', line 8

def client_key
  @client_key
end

#client_nameObject (readonly)

Returns the value of attribute client_name.



7
8
9
# File 'lib/ridley/middleware/chef_auth.rb', line 7

def client_name
  @client_name
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ridley/middleware/chef_auth.rb', line 16

def call(env)
  sign_obj = Mixlib::Authentication::SignedHeaderAuth.signing_object(
    http_method: env[:method],
    host: env[:url].host,
    path: env[:url].path,
    body: env[:body] || '',
    timestamp: Time.now.utc.iso8601,
    user_id: client_name
  )
  authentication_headers = sign_obj.sign(client_key)
  env[:request_headers] = default_headers.merge(env[:request_headers]).merge(authentication_headers)
  env[:request_headers] = env[:request_headers].merge('Content-Length' => env[:body].bytesize.to_s) if env[:body]

  Ridley.log.debug(env)

  @app.call(env)
end