Method: Compliance::API.get_token_via_refresh_token

Defined in:
lib/bundles/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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/bundles/inspec-compliance/api.rb', line 127

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 = 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