Class: Nimbu::Auth
- Inherits:
-
Object
show all
- Extended by:
- Helpers
- Defined in:
- lib/nimbu/ssh.rb,
lib/nimbu/auth.rb
Class Attribute Summary collapse
Class Method Summary
collapse
Methods included from Helpers
action, ask, confirm, confirm_billing, confirm_command, create_git_remote, deprecate, disable_error_capture, display, display_header, display_object, display_row, display_table, enable_error_capture, error, error_with_failure, error_with_failure=, extended, extended_into, fail, format_bytes, format_date, format_error, format_with_bang, get_terminal_environment, git, has_git?, home_directory, hprint, hputs, included, included_into, json_decode, json_encode, launchy, line_formatter, longest, output, output_with_arrow, output_with_bang, quantify, redisplay, retry_on_exception, run_command, running_on_a_mac?, running_on_windows?, set_buffer, shell, spinner, status, string_distance, styled_array, styled_error, styled_hash, styled_header, suggestion, time_ago, truncate, with_tty
#browser_launcher, #command?, #osx?, #tmp_dir, #which, #windows?
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
12
13
14
|
# File 'lib/nimbu/auth.rb', line 12
def configuration
@configuration
end
|
.credentials ⇒ Object
Returns the value of attribute credentials.
11
12
13
|
# File 'lib/nimbu/auth.rb', line 11
def credentials
@credentials
end
|
Class Method Details
.admin_host ⇒ Object
57
58
59
|
# File 'lib/nimbu/auth.rb', line 57
def admin_host
@admin_host ||= host.gsub(/https?\:\/\/api\./,'')
end
|
.ask_for_and_save_configuration ⇒ Object
81
82
83
84
85
|
# File 'lib/nimbu/auth.rb', line 81
def ask_for_and_save_configuration
@configuration = ask_for_configuration
write_configuration
@configuration
end
|
.ask_for_and_save_credentials ⇒ Object
281
282
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/nimbu/auth.rb', line 281
def ask_for_and_save_credentials
display "Please authenticate with Nimbu.io:"
begin
@credentials = ask_for_credentials
write_credentials
check
rescue Exception => e
delete_credentials
raise e
end
@credentials
end
|
.ask_for_configuration ⇒ Object
96
97
98
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
148
149
150
151
152
153
154
155
156
|
# File 'lib/nimbu/auth.rb', line 96
def ask_for_configuration
subdomain = nil
sites = client.sites.list
unless sites.respond_to?(:any?) && sites.any?
display "You don't have access to any Nimbu sites you can edit yet..."
display ""
display "Please visit http://nimbu.io, start your 30-day trial and discover our amazing platform!"
exit(1)
else
print_separator
display "\nLet's first setup the configuration for this directory..."
display "\nYou have access to following sites:\n"
sites.each_with_index do |site,i|
display " #{i+1}) #{site.name.white.bold} => http://#{site.domain}"
end
site_number = 0
retry_site = false
while site_number < 1 || site_number > sites.length
unless retry_site
print "\nOn which site would you like to work? "
else
print "\nPlease enter the number of your site (between 1-#{sites.length}): "
end
site_number_string = ask
site_number = site_number_string.to_i rescue 0
retry_site = true
end
puts ""
site = sites[site_number-1]
display "Site chosen => #{site.name.white.bold} (http://#{site.domain})"
subdomain = site.subdomain
@site = subdomain
end
themes = client.themes(:subdomain => subdomain).list
current_theme = if themes.length > 1
theme_number = 0
retry_theme = false
while theme_number < 1 || theme_number > themes.length
unless retry_theme
print "\nOn which theme would you like to work in this directory? "
else
print "\nPlease enter the number of your theme (between 1-#{themes.length}): "
end
theme_number_string = ask
theme_number = theme_number_string.to_i rescue 0
retry_theme = true
end
puts ""
display "Theme chosen => #{themes[theme_number-1].name}"
themes[theme_number-1]
else
themes.first
end
@theme = current_theme.short
print_separator
{ "site" => subdomain, "theme" => current_theme.short }
end
|
.ask_for_credentials ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/nimbu/auth.rb', line 227
def ask_for_credentials
print "Login: "
user = ask
print "Password: "
password = running_on_windows? ? ask_for_password_on_windows : ask_for_password
begin
basic_client = Nimbu::Client.new(
:basic_auth => "#{user}:#{password}",
:endpoint => host,
:user_agent => self.user_agent
)
basic_client.authenticate.token
rescue Exception => e
if e.respond_to?(:http_status_code) && e.http_status_code == 401
display " => could not login... please check your username and/or password!\n\n"
else
display " => hmmmm... an error occurred: #{e}. \n\n\nIf this continues to occur, please report \nthe error at https://github.com/nimbu/nimbu/issues.\n\n"
end
nil
end
end
|
.ask_for_password ⇒ Object
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/nimbu/auth.rb', line 269
def ask_for_password
echo_off
trap("INT") do
echo_on
exit
end
password = ask
puts
echo_on
return password
end
|
.ask_for_password_on_windows ⇒ Object
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/nimbu/auth.rb', line 251
def ask_for_password_on_windows
require "Win32API"
char = nil
password = ''
while char = Win32API.new("msvcrt", "_getch", [ ], "L").Call do
break if char == 10 || char == 13 if char == 127 || char == 8 password.slice!(-1, 1)
else
(password << char.chr) rescue RangeError
end
end
puts
return password
end
|
.associate_key(key) ⇒ Object
50
51
52
53
|
# File 'lib/nimbu/ssh.rb', line 50
def associate_key(key)
display "Uploading SSH public key #{key}"
client.add_key(File.read(key).force_encoding('UTF-8'))
end
|
.associate_or_generate_ssh_key ⇒ Object
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
|
# File 'lib/nimbu/ssh.rb', line 15
def associate_or_generate_ssh_key
public_keys = Dir.glob("#{home_directory}/.ssh/*.pub").sort
case public_keys.length
when 0 then
display "Could not find an existing public key."
display "Would you like to generate one? [Yn] ", false
unless ask.strip.downcase == "n"
display "Generating new SSH public key."
generate_ssh_key("id_rsa")
associate_key("#{home_directory}/.ssh/id_rsa.pub")
end
when 1 then
display "Found existing public key: #{public_keys.first}"
associate_key(public_keys.first)
else
display "Found the following SSH public keys:"
public_keys.each_with_index do |key, index|
display "#{index+1}) #{File.basename(key)}"
end
display "Which would you like to use with your Nimbu account? ", false
chosen = public_keys[ask.to_i-1] rescue error("Invalid choice")
associate_key(chosen)
end
end
|
.check ⇒ Object
just a stub; will raise if not authenticated
45
46
47
|
# File 'lib/nimbu/auth.rb', line 45
def check
client.sites.list
end
|
.check_for_associated_ssh_key ⇒ Object
10
11
12
13
|
# File 'lib/nimbu/ssh.rb', line 10
def check_for_associated_ssh_key
return unless client.keys.empty?
associate_or_generate_ssh_key
end
|
.client ⇒ Object
21
22
23
24
25
|
# File 'lib/nimbu/auth.rb', line 21
def client
@client ||= begin
Nimbu::Client.new(:oauth_token => token, :endpoint => host, :user_agent => self.user_agent, :auto_pagination => true)
end
end
|
.configuration_file ⇒ Object
87
88
89
|
# File 'lib/nimbu/auth.rb', line 87
def configuration_file
"#{Dir.pwd}/nimbu.yml"
end
|
.credentials_file ⇒ Object
182
183
184
185
186
187
188
|
# File 'lib/nimbu/auth.rb', line 182
def credentials_file
if host == default_host
"#{home_directory}/.nimbu/credentials"
else
"#{home_directory}/.nimbu/credentials.#{CGI.escape(host.gsub(/https?\:\/\//,''))}"
end
end
|
.default_host ⇒ Object
53
54
55
|
# File 'lib/nimbu/auth.rb', line 53
def default_host
"https://api.nimbu.io"
end
|
.delete_configuration ⇒ Object
91
92
93
94
|
# File 'lib/nimbu/auth.rb', line 91
def delete_configuration
FileUtils.rm_f(configuration_file)
@host = nil
end
|
.delete_credentials ⇒ Object
194
195
196
197
|
# File 'lib/nimbu/auth.rb', line 194
def delete_credentials
FileUtils.rm_f(credentials_file)
@client, @credentials = nil, nil
end
|
.echo_off ⇒ Object
215
216
217
218
219
|
# File 'lib/nimbu/auth.rb', line 215
def echo_off
with_tty do
system "stty -echo"
end
end
|
.echo_on ⇒ Object
221
222
223
224
225
|
# File 'lib/nimbu/auth.rb', line 221
def echo_on
with_tty do
system "stty echo"
end
end
|
.generate_ssh_key(keyfile) ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'lib/nimbu/ssh.rb', line 41
def generate_ssh_key(keyfile)
ssh_dir = File.join(home_directory, ".ssh")
unless File.exists?(ssh_dir)
FileUtils.mkdir_p ssh_dir
File.chmod(0700, ssh_dir)
end
`ssh-keygen -t rsa -N "" -f \"#{home_directory}/.ssh/#{keyfile}\" 2>&1`
end
|
.get_configuration ⇒ Object
77
78
79
|
# File 'lib/nimbu/auth.rb', line 77
def get_configuration
@configuration ||= (read_configuration || ask_for_and_save_configuration)
end
|
.get_credentials ⇒ Object
190
191
192
|
# File 'lib/nimbu/auth.rb', line 190
def get_credentials @credentials ||= (read_credentials || ask_for_and_save_credentials)
end
|
.get_nimbu_site ⇒ Object
69
70
71
|
# File 'lib/nimbu/auth.rb', line 69
def get_nimbu_site
get_configuration["site"]
end
|
.get_nimbu_theme ⇒ Object
73
74
75
|
# File 'lib/nimbu/auth.rb', line 73
def get_nimbu_theme
get_configuration["theme"] || "default-theme"
end
|
.host ⇒ Object
49
50
51
|
# File 'lib/nimbu/auth.rb', line 49
def host
ENV['NIMBU_HOST'] || "https://api.nimbu.io"
end
|
.invalid_access! ⇒ Object
306
307
308
|
# File 'lib/nimbu/auth.rb', line 306
def invalid_access!
puts invalid_access_message.bold.red
end
|
.invalid_access_message ⇒ Object
310
311
312
313
|
# File 'lib/nimbu/auth.rb', line 310
def invalid_access_message
"Error! You do not have access to #{Nimbu::Auth.site}.#{Nimbu::Auth.admin_host}! " +
"Please check your site id or request access to your site owner."
end
|
.login ⇒ Object
31
32
33
34
|
# File 'lib/nimbu/auth.rb', line 31
def login
delete_credentials
get_credentials
end
|
.logout ⇒ Object
36
37
38
|
# File 'lib/nimbu/auth.rb', line 36
def logout
delete_credentials
end
|
.print_separator ⇒ Object
300
301
302
303
304
|
# File 'lib/nimbu/auth.rb', line 300
def print_separator
print "\n"
60.times { print "#"}
print "\n"
end
|
.read_configuration ⇒ Object
158
159
160
161
162
163
164
165
|
# File 'lib/nimbu/auth.rb', line 158
def read_configuration
existing_config = YAML::load(File.open( configuration_file )) if File.exists?(configuration_file)
if existing_config && ! existing_config["site"].nil?
existing_config
else
nil
end
end
|
.read_credentials ⇒ Object
199
200
201
202
203
204
205
206
|
# File 'lib/nimbu/auth.rb', line 199
def read_credentials
credentials = File.read(credentials_file).force_encoding('UTF-8') if File.exists?(credentials_file)
if credentials && credentials =~ /^(bearer|oauth2|token) ([\w]+)$/i
$2
else
nil
end
end
|
.reauthorize ⇒ Object
174
175
176
|
# File 'lib/nimbu/auth.rb', line 174
def reauthorize
@credentials = ask_for_and_save_credentials
end
|
.retry_login? ⇒ Boolean
294
295
296
297
298
|
# File 'lib/nimbu/auth.rb', line 294
def retry_login?
@login_attempts ||= 0
@login_attempts += 1
@login_attempts < 3
end
|
.simulator_id ⇒ Object
14
15
16
17
18
19
|
# File 'lib/nimbu/auth.rb', line 14
def simulator_id
return @simulator_id if defined? @simulator_id
ranges = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten
@simulator_id ||= (1..40).map{ ranges[rand(ranges.length)] }.join
end
|
.site ⇒ Object
61
62
63
|
# File 'lib/nimbu/auth.rb', line 61
def site
@site ||= ENV['NIMBU_SITE'] || get_nimbu_site
end
|
.theme ⇒ Object
65
66
67
|
# File 'lib/nimbu/auth.rb', line 65
def theme
@theme ||= ENV['NIMBU_THEME'] || get_nimbu_theme
end
|
.token ⇒ Object
178
179
180
|
# File 'lib/nimbu/auth.rb', line 178
def token ENV['NIMBU_API_KEY'] || get_credentials
end
|
.user_agent ⇒ Object
27
28
29
|
# File 'lib/nimbu/auth.rb', line 27
def user_agent
"nimbu-toolbelt/#{Nimbu::VERSION} (#{RUBY_PLATFORM}) ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}".freeze
end
|
.whoami ⇒ Object
40
41
42
|
# File 'lib/nimbu/auth.rb', line 40
def whoami
client.users.me
end
|
.write_configuration ⇒ Object
167
168
169
170
171
172
|
# File 'lib/nimbu/auth.rb', line 167
def write_configuration
FileUtils.mkdir_p(File.dirname(configuration_file))
File.open(configuration_file, 'w') {|credentials| credentials.puts(YAML.dump(self.configuration))}
FileUtils.chmod(0700, File.dirname(configuration_file))
FileUtils.chmod(0600, configuration_file)
end
|
.write_credentials ⇒ Object
208
209
210
211
212
213
|
# File 'lib/nimbu/auth.rb', line 208
def write_credentials
FileUtils.mkdir_p(File.dirname(credentials_file))
File.open(credentials_file, 'w') {|credentials| credentials.print("token #{self.credentials}")}
FileUtils.chmod(0700, File.dirname(credentials_file))
FileUtils.chmod(0600, credentials_file)
end
|