Class: Kontena::Cli::MasterCodeExchanger

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/cli/master_code_exchanger.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

REDIRECT_URI =
'http://localhost/'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(url, ssl_verify_peer: false) ⇒ MasterCodeExchanger

Returns a new instance of MasterCodeExchanger.

Parameters:

  • url (String)


16
17
18
# 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

Instance Method Details

#exchange_code(authorization_code) ⇒ Object



20
21
22
23
24
25
26
27
# 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_authHash

Returns:

  • (Hash)


30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Hash

Parameters:

  • code (String)
  • state (String)

Returns:

  • (Hash)


44
45
46
47
48
49
50
51
52
53
54
# 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