Method: TenableRuby::Client#authenticate

Defined in:
lib/tenable-ruby.rb

#authenticateObject

Tries to authenticate to the tenable.io REST JSON interface using username/password or API keys



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tenable-ruby.rb', line 84

def authenticate
  if @credentials[:username] and @credentials[:password]
    payload = {
      :username => @credentials[:username],
      :password => @credentials[:password],
      :json => 1,
      :authenticationmethod => true
    }
    response = http_post(:uri => "/session", :data => payload)
    if response['token']
      @token = "token=#{response['token']}"
      @header = {'X-Cookie' => @token}
    else
      raise TenableRuby::Error::AuthenticationError, "Authentication failed. Could not authenticate using
      username/password."
    end
  elsif @credentials[:access_key] and @credentials[:secret_key]
    @header = {'X-ApiKeys' => "accessKey=#{@credentials[:access_key]}; secretKey=#{@credentials[:secret_key]}"}
  else
    raise TenableRuby::Error::AuthenticationError, "Authentication credentials were not provided. You must " \
    "provide either a username and password or an API access key and secret key (these can be generated at " \
    "https://cloud.tenable.com/app.html#/settings/my-account/api-keys."
  end
end