Class: Kontena::Cli::Cloud::LoginCommand
Instance Attribute Summary
#arguments, #exit_code, #result
Instance Method Summary
collapse
#access_token=, #add_master, #any_key_to_continue, #any_key_to_continue_with_timeout, #api_url, #api_url=, #caret, #clear_current_grid, #client, #cloud_auth?, #cloud_client, #config, #confirm, #confirm_command, #current_grid, #current_master_index, #debug?, #display_account_login_info, #display_login_info, display_logo, #display_master_login_info, #error, exit_with_error, #kontena_account, #logger, #pastel, #print, #prompt, #puts, #require_api_url, #require_token, #reset_client, #reset_cloud_client, #running_quiet?, #running_silent?, #running_verbose?, #spin_if, #spinner, #sprint, #sputs, #stdin_input, #use_refresh_token, #vfakespinner, #vputs, #vspinner, #warning
banner, callback_matcher, #help_requested?, inherited, #instance, load_subcommand, requires_current_account_token, requires_current_account_token?, requires_current_grid, requires_current_grid?, requires_current_master, requires_current_master?, requires_current_master_token, requires_current_master_token?, #run, #run_callbacks, #verify_current_account_token, #verify_current_grid, #verify_current_master, #verify_current_master_token
Instance Method Details
#execute ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/kontena/cli/cloud/login_command.rb', line 13
def execute
if self.code && self.force?
exit_with_error "Can't use --code and --force together"
end
if self.token
exit_with_error "Can't use --token and --force together" if self.force?
exit_with_error "Can't use --token and --code together" if self.code
end
if !kontena_account.token || !kontena_account.token.access_token || self.token || self.force?
kontena_account.token = Kontena::Cli::Config::Token.new(access_token: self.token, parent_type: :account, parent_name: kontena_account.name)
end
use_authorization_code(self.code) if self.code
client = Kontena::Client.new(kontena_account.userinfo_endpoint, kontena_account.token, prefix: '')
if kontena_account.token.access_token
auth_ok = vspinner "Verifying current access token" do
client.authentication_ok?(kontena_account.userinfo_endpoint)
end
if auth_ok
finish and return
end
end
if remote?
remote_login
else
web_flow
end
finish
end
|
#finish ⇒ Object
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/kontena/cli/cloud/login_command.rb', line 47
def finish
update_userinfo unless kontena_account.username
config.current_account = kontena_account.name
config.write
config.reset_instance
reset_cloud_client
display_logo
display_login_info(only: :account)
true
end
|
#remote_login ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/kontena/cli/cloud/login_command.rb', line 58
def remote_login
client_id = kontena_account.client_id || Kontena::Client::CLIENT_ID
params = {
client_id: client_id
}
cloud_url = kontena_account.url
client = Kontena::Client.new(cloud_url, nil)
auth_request_response = client.post('/auth_requests', params, {}, { 'Content-Type' => 'application/x-www-form-urlencoded' }) rescue nil
if !auth_request_response.kind_of?(Hash)
exit_with_error "Remote login request failed"
elsif auth_request_response['error']
exit_with_error "Remote login request failed: #{auth_request_response['error']}"
end
begin
verification_uri = URI.parse(auth_request_response['verification_uri'])
rescue => e
exit_with_error "Parsing remote login URL failed."
end
puts "Please visit #{pastel.cyan(verification_uri.to_s)} and enter the code"
puts
puts "#{auth_request_response['user_code']}"
puts
puts "Once the authentication is complete you can close the browser"
puts "window or tab and return to this window to continue."
puts
code_request_params = {
client_id: client_id,
device_code: auth_request_response['device_code']
}
code_response = nil
spinner "Waiting for authentication" do
until code_response do
code_response = client.post("/auth_requests/code", code_request_params, {}, { 'Content-Type' => 'application/x-www-form-urlencoded' }) rescue nil
sleep 1
end
end
update_token(code_response)
end
|
#update_token(response) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/kontena/cli/cloud/login_command.rb', line 171
def update_token(response)
if !response.kind_of?(Hash)
raise TypeError, "Invalid authentication response, expected Hash, got #{response.class}"
elsif response['error']
exit_with_error "Authentication failed: #{response['error']}"
elsif response['code']
use_authorization_code(response['code'])
else
kontena_account.token.access_token = response['access_token']
kontena_account.token.refresh_token = response['refresh_token']
kontena_account.token.expires_at = response['expires_at']
true
end
end
|
#update_userinfo ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/kontena/cli/cloud/login_command.rb', line 149
def update_userinfo
uri = URI.parse(kontena_account.userinfo_endpoint)
path = uri.path
uri.path = '/'
response = Kontena::Client.new(uri.to_s, kontena_account.token).get(path)
if response.kind_of?(Hash) && response['data'] && response['data']['attributes']
kontena_account.username = response['data']['attributes']['username']
elsif response && response['error']
exit_with_error response['error']
else
exit_with_error "Userinfo request failed"
end
end
|
#use_authorization_code(code) ⇒ Object
164
165
166
167
168
169
|
# File 'lib/kontena/cli/cloud/login_command.rb', line 164
def use_authorization_code(code)
response = vspinner "Exchanging authorization code to access token" do
Kontena::Client.new(kontena_account.token_endpoint, kontena_account.token).exchange_code(code)
end
update_token(response)
end
|
#web_flow ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/kontena/cli/cloud/login_command.rb', line 99
def web_flow
if Kontena.browserless? && !force?
$stderr.puts "Your current environment does not seem to support opening a local graphical WWW browser. Using remote login instead."
$stderr.puts
remote_login
return
end
require_relative '../localhost_web_server'
require 'kontena/cli/browser_launcher'
uri = URI.parse(kontena_account.authorization_endpoint)
uri.host ||= kontena_account.url
web_server = Kontena::LocalhostWebServer.new
params = {
client_id: kontena_account.client_id || Kontena::Client::CLIENT_ID,
response_type: 'code',
redirect_uri: "http://localhost:#{web_server.port}/cb"
}
uri.query = URI.encode_www_form(params)
puts "Opening a browser to #{uri.scheme}://#{uri.host}"
puts
puts "Once the authentication is complete you can close the browser"
puts "window or tab and return to this window to continue."
puts
any_key_to_continue(10)
puts "If the browser does not open, try visiting this URL manually:"
puts "#{uri.to_s}"
puts
server_thread = Thread.new { Thread.main['response'] = web_server.serve_one }
Kontena::Cli::BrowserLauncher.open(uri.to_s)
spinner "Waiting for browser authorization response" do
server_thread.join
end
update_token(Thread.main['response'])
end
|