Module: EndPointBlank::Commands::GenerateAccessTokenMethods::ClassMethods

Defined in:
lib/end_point_blank/commands/generate_access_token.rb

Instance Method Summary collapse

Instance Method Details

#configurationObject



11
12
13
# File 'lib/end_point_blank/commands/generate_access_token.rb', line 11

def configuration
  EndPointBlank::Configuration.instance
end

#token(hostname) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/end_point_blank/commands/generate_access_token.rb', line 15

def token(hostname)
  body = {hostname: hostname}
  if configuration.token_ttl
    body[:token_ttl] = configuration.token_ttl
  end
  auth = Authorization.header
  response = Excon.post(configuration.access_token_url,
    headers: {'Authorization' => auth, 'Content-Type' => 'application/json'},
    body: body.to_json,
    **EndPointBlank::Commands::Http::TIMEOUT_OPTIONS
  )
  EndPointBlank.logger.info "Authentication response: #{response.status} - #{response.body}"
  parsed_body = response.body.is_a?(String) ? JSON.parse(response.body) : response.body
  parsed_body.transform_keys(&:to_sym)
rescue => e
  EndPointBlank.logger.error "Error occurred during authentication: #{e.message}\n #{e.backtrace.join("\n")}"
  nil
end