Method: InspecPlugins::Compliance::API.get_token_via_refresh_token

Defined in:
lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb

.get_token_via_refresh_token(url, refresh_token, insecure) ⇒ Object

Use username and refresh_token to get an API access token



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 147

def self.get_token_via_refresh_token(url, refresh_token, insecure)
  uri = URI.parse("#{url}/login")
  req = Net::HTTP::Post.new(uri.path)
  req.body = { token: refresh_token }.to_json
  access_token = nil
  response = InspecPlugins::Compliance::HTTP.send_request(uri, req, insecure)
  data = response.body
  if response.code == '200'
    begin
      tokendata = JSON.parse(data)
      access_token = tokendata['access_token']
      msg = 'Successfully fetched API access token'
      success = true
    rescue JSON::ParserError => e
      success = false
      msg = e.message
    end
  else
    success = false
    msg = "Failed to authenticate to #{url} \n\
    Response code: #{response.code}\n  Body: #{response.body}"
  end

  [success, msg, access_token]
end