class Kontena::Cli::MasterCodeExchanger
Constants
- REDIRECT_URI
Public Class Methods
new(url, ssl_verify_peer: false)
click to toggle source
@param [String] url
# File lib/kontena/cli/master_code_exchanger.rb, line 16 def initialize(url, ssl_verify_peer: false) @api_client = Excon.new(url, { ssl_verify_peer: ssl_verify_peer }) end
Public Instance Methods
exchange_code(authorization_code)
click to toggle source
# File lib/kontena/cli/master_code_exchanger.rb, line 20 def exchange_code(authorization_code) master_auth_response = request_auth redirect_url = URI.parse(master_auth_response[:headers]['Location']) state = CGI.parse(redirect_url.query)['state'] master_code_response = request_code(authorization_code, state.first) redirect_url = URI.parse(master_code_response[:headers]['Location']) CGI.parse(redirect_url.query)['code'].first end
request_auth()
click to toggle source
@return [Hash]
# File lib/kontena/cli/master_code_exchanger.rb, line 30 def request_auth params = { redirect_uri: REDIRECT_URI } response = execute("/authenticate?#{URI.encode_www_form(params)}") unless response[:status] == 302 raise Error.new(response[:status], response[:data]) end response end
request_code(code, state)
click to toggle source
@param [String] code @param [String] state @return [Hash]
# File lib/kontena/cli/master_code_exchanger.rb, line 44 def request_code(code, state) params = { code: code, state: state } response = execute("/cb?#{URI.encode_www_form(params)}") unless response[:status] == 302 raise Error.new(response[:status], response[:data]) end response end
Private Instance Methods
execute(path, method = :get, payload = nil)
click to toggle source
# File lib/kontena/cli/master_code_exchanger.rb, line 58 def execute(path, method = :get, payload = nil) headers = {} headers['Content-Type'] = 'application/json' unless payload.nil? body = payload.to_json if payload response = @api_client.request(method: method, body: body, path: path, headers: headers) content_type = response.headers.dig('Content-Type') || '' content_length = response.headers.dig('Content-Length').to_i body = if content_type.include?('json') && content_length > 0 JSON.parse(response.body) rescue response.body else response.body end { status: response.status, headers: response.headers, data: body } end