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.
11
12
13
|
# File 'lib/nimbu/auth.rb', line 11
def configuration
@configuration
end
|
.credentials ⇒ Object
Returns the value of attribute credentials.
10
11
12
|
# File 'lib/nimbu/auth.rb', line 10
def credentials
@credentials
end
|
Class Method Details
.admin_host ⇒ Object
52
53
54
|
# File 'lib/nimbu/auth.rb', line 52
def admin_host
@admin_host ||= host.gsub(/https?\:\/\/api\./,'')
end
|
.ask_for_and_save_configuration ⇒ Object
76
77
78
79
80
|
# File 'lib/nimbu/auth.rb', line 76
def ask_for_and_save_configuration
@configuration = ask_for_configuration
write_configuration
@configuration
end
|
.ask_for_and_save_credentials ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/nimbu/auth.rb', line 276
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
91
92
93
94
95
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
|
# File 'lib/nimbu/auth.rb', line 91
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/nimbu/auth.rb', line 222
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
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/nimbu/auth.rb', line 264
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/nimbu/auth.rb', line 246
def ask_for_password_on_windows
require "Win32API"
char = nil
password = ''
while char = Win32API.new("crtdll", "_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
40
41
42
|
# File 'lib/nimbu/auth.rb', line 40
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
20
21
22
23
24
|
# File 'lib/nimbu/auth.rb', line 20
def client
@client ||= begin
Nimbu::Client.new(:oauth_token => token, :endpoint => host, :user_agent => self.user_agent)
end
end
|
.configuration_file ⇒ Object
82
83
84
|
# File 'lib/nimbu/auth.rb', line 82
def configuration_file
"#{Dir.pwd}/nimbu.yml"
end
|
.credentials_file ⇒ Object
177
178
179
180
181
182
183
|
# File 'lib/nimbu/auth.rb', line 177
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
48
49
50
|
# File 'lib/nimbu/auth.rb', line 48
def default_host
"https://api.nimbu.io"
end
|
.delete_configuration ⇒ Object
86
87
88
89
|
# File 'lib/nimbu/auth.rb', line 86
def delete_configuration
FileUtils.rm_f(configuration_file)
@host = nil
end
|
.delete_credentials ⇒ Object
189
190
191
192
|
# File 'lib/nimbu/auth.rb', line 189
def delete_credentials
FileUtils.rm_f(credentials_file)
@client, @credentials = nil, nil
end
|
.echo_off ⇒ Object
210
211
212
213
214
|
# File 'lib/nimbu/auth.rb', line 210
def echo_off
with_tty do
system "stty -echo"
end
end
|
.echo_on ⇒ Object
216
217
218
219
220
|
# File 'lib/nimbu/auth.rb', line 216
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
72
73
74
|
# File 'lib/nimbu/auth.rb', line 72
def get_configuration
@configuration ||= (read_configuration || ask_for_and_save_configuration)
end
|
.get_credentials ⇒ Object
185
186
187
|
# File 'lib/nimbu/auth.rb', line 185
def get_credentials
@credentials ||= (read_credentials || ask_for_and_save_credentials)
end
|
.get_nimbu_site ⇒ Object
64
65
66
|
# File 'lib/nimbu/auth.rb', line 64
def get_nimbu_site
get_configuration["site"]
end
|
.get_nimbu_theme ⇒ Object
68
69
70
|
# File 'lib/nimbu/auth.rb', line 68
def get_nimbu_theme
get_configuration["theme"] || "default-theme"
end
|
.host ⇒ Object
44
45
46
|
# File 'lib/nimbu/auth.rb', line 44
def host
ENV['NIMBU_HOST'] || "https://api.nimbu.io"
end
|
.login ⇒ Object
30
31
32
33
|
# File 'lib/nimbu/auth.rb', line 30
def login
delete_credentials
get_credentials
end
|
.logout ⇒ Object
35
36
37
|
# File 'lib/nimbu/auth.rb', line 35
def logout
delete_credentials
end
|
.print_separator ⇒ Object
295
296
297
298
299
|
# File 'lib/nimbu/auth.rb', line 295
def print_separator
print "\n"
60.times { print "#"}
print "\n"
end
|
.read_configuration ⇒ Object
153
154
155
156
157
158
159
160
|
# File 'lib/nimbu/auth.rb', line 153
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
194
195
196
197
198
199
200
201
|
# File 'lib/nimbu/auth.rb', line 194
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
169
170
171
|
# File 'lib/nimbu/auth.rb', line 169
def reauthorize
@credentials = ask_for_and_save_credentials
end
|
.retry_login? ⇒ Boolean
289
290
291
292
293
|
# File 'lib/nimbu/auth.rb', line 289
def retry_login?
@login_attempts ||= 0
@login_attempts += 1
@login_attempts < 3
end
|
.simulator_id ⇒ Object
13
14
15
16
17
18
|
# File 'lib/nimbu/auth.rb', line 13
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
56
57
58
|
# File 'lib/nimbu/auth.rb', line 56
def site
@site ||= ENV['NIMBU_SITE'] || get_nimbu_site
end
|
.theme ⇒ Object
60
61
62
|
# File 'lib/nimbu/auth.rb', line 60
def theme
@theme ||= ENV['NIMBU_THEME'] || get_nimbu_theme
end
|
.token ⇒ Object
173
174
175
|
# File 'lib/nimbu/auth.rb', line 173
def token
get_credentials
end
|
.user_agent ⇒ Object
26
27
28
|
# File 'lib/nimbu/auth.rb', line 26
def user_agent
"nimbu-toolbelt/#{Nimbu::VERSION} (#{RUBY_PLATFORM}) ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}".freeze
end
|
.write_configuration ⇒ Object
162
163
164
165
166
167
|
# File 'lib/nimbu/auth.rb', line 162
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
203
204
205
206
207
208
|
# File 'lib/nimbu/auth.rb', line 203
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
|