Class: Morpheus::Cli::AccessTokenCommand
- Inherits:
-
Object
- Object
- Morpheus::Cli::AccessTokenCommand
show all
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/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
#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #parse_list_options, #parse_list_subtitles, #print, #print_error, #print_to_file, #puts, #puts_error, #raise_command_error, #render_with_format, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Constructor Details
14
15
16
|
# File 'lib/morpheus/cli/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
41
42
|
# File 'lib/morpheus/cli/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}))
if !@appliance_name
unless options[:quiet]
print yellow,"Please specify a Morpheus Appliance with -r or see the command `remote use`#{reset}\n"
end
return 1
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 the 'login' command.",reset,"\n"
end
return 1
end
end
|
#details(args) ⇒ Object
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
85
86
|
# File 'lib/morpheus/cli/access_token_command.rb', line 53
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'] },
"Remote" => lambda {|wallet| "#{display_appliance(@appliance_name, @appliance_url)}" },
"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']) : "" },
}
print cyan
puts as_description_list(@wallet, description_cols)
print reset
return 0
end
|
#handle(args) ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/morpheus/cli/access_token_command.rb', line 44
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/morpheus/cli/access_token_command.rb', line 88
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
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
|
# File 'lib/morpheus/cli/access_token_command.rb', line 111
def use_refresh_token(args)
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage()
build_common_options(opts, options, [:auto_confirm, :remote, :dry_run, :json, :quiet])
opts. = "Use your refresh 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(options)
if options[:dry_run]
print_dry_run Morpheus::AuthInterface.new(@appliance_url).setopts(options).use_refresh_token(@wallet['refresh_token'])
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(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
|