How to use get_token_via_refresh_token method of InspecPlugins.Compliance Package

Best Inspec_ruby code snippet using InspecPlugins.Compliance.get_token_via_refresh_token

api.rb

Source:api.rb Github

copy

Full Screen

...124 end125 [res.is_a?(Net::HTTPSuccess), res.body]126 end127 # Use username and refresh_token to get an API access token128 def self.get_token_via_refresh_token(url, refresh_token, insecure)129 uri = URI.parse("#{url}/login")130 req = Net::HTTP::Post.new(uri.path)131 req.body = { token: refresh_token }.to_json132 access_token = nil133 response = InspecPlugins::Compliance::HTTP.send_request(uri, req, insecure)134 data = response.body135 if response.code == '200'136 begin137 tokendata = JSON.parse(data)138 access_token = tokendata['access_token']139 msg = 'Successfully fetched API access token'140 success = true141 rescue JSON::ParserError => e142 success = false143 msg = e.message144 end145 else146 success = false147 msg = "Failed to authenticate to #{url} \n\148 Response code: #{response.code}\n Body: #{response.body}"149 end150 [success, msg, access_token]151 end152 # Use username and password to get an API access token153 def self.get_token_via_password(url, username, password, insecure)154 uri = URI.parse("#{url}/login")155 req = Net::HTTP::Post.new(uri.path)156 req.body = { userid: username, password: password }.to_json157 access_token = nil158 response = InspecPlugins::Compliance::HTTP.send_request(uri, req, insecure)159 data = response.body160 if response.code == '200'161 access_token = data162 msg = 'Successfully fetched an API access token valid for 12 hours'163 success = true164 else165 success = false166 msg = "Failed to authenticate to #{url} \n\167 Response code: #{response.code}\n Body: #{response.body}"168 end169 [success, msg, access_token]170 end171 def self.get_headers(config)172 token = get_token(config)173 if is_automate_server?(config) || is_automate2_server?(config)174 headers = { 'chef-delivery-enterprise' => config['automate']['ent'] }175 if config['automate']['token_type'] == 'dctoken'176 headers['x-data-collector-token'] = token177 else178 headers['chef-delivery-user'] = config['user']179 headers['chef-delivery-token'] = token180 end181 else182 headers = { 'Authorization' => "Bearer #{token}" }183 end184 headers185 end186 def self.get_token(config)187 return config['token'] unless config['refresh_token']188 _success, _msg, token = get_token_via_refresh_token(config['server'], config['refresh_token'], config['insecure'])189 token190 end191 def self.target_url(config, profile)192 owner, id, ver = profile_split(profile)193 return "#{config['server']}/compliance/profiles/tar" if is_automate2_server?(config)194 return "#{config['server']}/owners/#{owner}/compliance/#{id}/tar" unless is_automate_server?(config)195 if ver.nil?196 "#{config['server']}/profiles/#{owner}/#{id}/tar"197 else198 "#{config['server']}/profiles/#{owner}/#{id}/version/#{ver}/tar"199 end200 end201 def self.profile_split(profile)202 owner, id = profile.split('/')...

Full Screen

Full Screen

get_token_via_refresh_token

Using AI Code Generation

copy

Full Screen

1File.open('token.txt', 'w') { |f| f.write(cli_plugin.config[:token]) }2File.open('token.txt', 'w') { |f| f.write(cli_plugin.config[:token]) }3File.open('token.txt', 'w') { |f| f.write(cli_plugin.config[:token]) }4File.open('token.txt

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful