Class: Morpheus::Cli::AccessTokenCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::AccessTokenCommand
show all
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/commands/access_token_command.rb
Overview
This provides commands for authentication This also includes credential management.
Instance Attribute Summary
Attributes included from CliCommand
#no_prompt
Instance Method Summary
collapse
Methods included from CliCommand
#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands
Constructor Details
14
15
16
|
# File 'lib/morpheus/cli/commands/access_token_command.rb', line 14
def initialize()
end
|
Instance Method Details
#connect(options) ⇒ Object
connect overridden to skip login and return an exit_code
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/morpheus/cli/commands/access_token_command.rb', line 19
def connect(options)
@api_client = establish_remote_appliance_connection(options.merge({:no_prompt => true, :skip_verify_access_token => true, :skip_login => true}))
if !@appliance_name
raise_command_error "#{command_name} requires a remote to be specified, use -r [remote] or set the active remote with `remote use`"
end
if !@appliance_url
unless options[:quiet]
print red,"Unable to determine remote appliance url. Review your remote configuration.#{reset}\n"
end
return 1
end
if @wallet.nil? || @wallet['access_token'].nil?
unless options[:quiet]
print_error yellow,"You are not currently logged in to #{display_appliance(@appliance_name, @appliance_url)}",reset,"\n"
print_error yellow,"Use `login` to authenticate or try passing the --username option.",reset,"\n"
end
return 1
end
return 0
end
|
#details(args) ⇒ Object
51
52
53
54
55
56
57
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
|
# File 'lib/morpheus/cli/commands/access_token_command.rb', line 51
def details(args)
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
build_common_options(opts, options, [:remote, :quiet])
opts. = "Print your current authentication credentials.\n" +
"This contains tokens that should be kept secret. Be careful."
end
optparse.parse!(args)
if args.count != 0
raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
end
connect_result = connect(options)
return connect_result if (connect_result.is_a?(Numeric) && connect_result != 0)
print_h1 "Morpheus Credentials", options
description_cols = {
"Username" => lambda {|wallet| wallet['username'] },
"Access Token" => lambda {|wallet| wallet['access_token'] },
"Refresh Token" => lambda {|wallet| wallet['refresh_token'] },
"Login Date" => lambda {|wallet| format_local_dt(wallet['login_date']) },
"Expire Date" => lambda {|wallet| wallet['expire_date'] ? format_local_dt(wallet['expire_date']) : "" },
"Remote" => lambda {|wallet| display_appliance(@appliance_name, @appliance_url) },
}
print cyan
puts as_description_list(@wallet, description_cols)
print reset
return 0
end
|
#handle(args) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/morpheus/cli/commands/access_token_command.rb', line 42
def handle(args)
if args.empty? || (args[0] && args[0] =~ /^\-/ && !['-h', '--help'].include?(args[0]))
print_access_token(args)
else
handle_subcommand(args)
end
end
|
#print_access_token(args) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/morpheus/cli/commands/access_token_command.rb', line 86
def print_access_token(args)
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
build_common_options(opts, options, [:remote, :quiet])
opts. = "Print your current access token.\n" +
"This token should be kept secret. Be careful."
end
optparse.parse!(args)
if args.count != 0
raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
end
connect_result = connect(options)
return connect_result if (connect_result.is_a?(Numeric) && connect_result != 0)
unless options[:quiet]
print cyan
puts @wallet['access_token']
print reset
end
return @wallet['access_token'] ? 0 : 1
end
|
#use_refresh_token(args) ⇒ Object
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/morpheus/cli/commands/access_token_command.rb', line 109
def use_refresh_token(args)
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
opts.on( '-T', '--token TOKEN', "Refresh Token to use. The saved credentials are used by default." ) do |val|
options[:refresh_token] = val
end
opts.on( '--token-file FILE', String, "Refresh Token File, read a file containing the refresh token." ) do |val|
token_file = File.expand_path(val)
if !File.exist?(token_file) || !File.file?(token_file)
raise ::OptionParser::InvalidOption.new("File not found: #{token_file}")
end
options[:refresh_token] = File.read(token_file).to_s.split("\n").first.strip
end
build_common_options(opts, options, [:auto_confirm, :remote, :dry_run, :json, :quiet], [:remote_username, :remote_password, :remote_token])
opts. = "Use the refresh token in your saved credentials, or a provided token.\n" +
"This will replace your current access and refresh tokens with a new values.\n" +
"Your current access token will be invalidated\n" +
"All other users or applications with access to your token will need to update to the new token."
end
optparse.parse!(args)
if args.count != 0
raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
end
connect_result = connect(options)
return connect_result if (connect_result.is_a?(Numeric) && connect_result != 0)
refresh_token_value = nil
if options[:refresh_token]
refresh_token_value = options[:refresh_token]
else
if @wallet['refresh_token']
refresh_token_value = @wallet['refresh_token']
else
unless options[:quiet]
print_error yellow,"No refresh token found for appliance #{display_appliance(@appliance_name, @appliance_url)}",reset,"\n"
print_error yellow,"Use the 'login' command.",reset,"\n"
end
return 1
end
end
if options[:dry_run]
print_dry_run @api_client.auth.dry.use_refresh_token(refresh_token_value)
return 0
end
unless options[:quiet]
access_token = @wallet['access_token'].to_s
visible_part = access_token[0..7]
if visible_part
masked_access_token = visible_part + access_token[8..-1].gsub(/[^-]/, '*')
else
masked_access_token = access_token.gsub(/[^-]/, '*')
end
print cyan,"#{bold}WARNING!#{reset}#{cyan} You are about to invalidate your current access token '#{masked_access_token}'.",reset,"\n"
print cyan, "You will need to update everywhere this token is used.",reset, "\n"
end
unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to refresh your access token?")
return 9, "aborted command"
end
refresh_result = Morpheus::Cli::Credentials.new(@appliance_name, @appliance_url).use_refresh_token(refresh_token_value, options)
new_wallet = refresh_result
if options[:json]
puts as_json(refresh_result, options)
return new_wallet ? 0 : 1
end
if new_wallet
unless options[:quiet]
print_green_success "Access token refreshed: #{new_wallet['access_token']}"
details([])
end
return 0
else
print_red_alert "Failed to use refresh token."
return 1
end
end
|