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
45
46
47
|
# File 'lib/nimbu/auth.rb', line 45
def admin_host
@admin_host ||= host.gsub(/https?\:\/\/api\./,'')
end
|
.ask_for_and_save_configuration ⇒ Object
69
70
71
72
73
|
# File 'lib/nimbu/auth.rb', line 69
def ask_for_and_save_configuration
@configuration = ask_for_configuration
write_configuration
@configuration
end
|
.ask_for_and_save_credentials ⇒ Object
269
270
271
272
273
274
275
276
277
278
279
280
|
# File 'lib/nimbu/auth.rb', line 269
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
84
85
86
87
88
89
90
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
|
# File 'lib/nimbu/auth.rb', line 84
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/nimbu/auth.rb', line 215
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
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/nimbu/auth.rb', line 257
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/nimbu/auth.rb', line 239
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
33
34
35
|
# File 'lib/nimbu/auth.rb', line 33
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
13
14
15
16
17
|
# File 'lib/nimbu/auth.rb', line 13
def client
@client ||= begin
Nimbu::Client.new(:oauth_token => token, :endpoint => host, :user_agent => self.user_agent)
end
end
|
.configuration_file ⇒ Object
75
76
77
|
# File 'lib/nimbu/auth.rb', line 75
def configuration_file
"#{Dir.pwd}/nimbu.yml"
end
|
.credentials_file ⇒ Object
170
171
172
173
174
175
176
|
# File 'lib/nimbu/auth.rb', line 170
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
41
42
43
|
# File 'lib/nimbu/auth.rb', line 41
def default_host
"https://api.nimbu.io"
end
|
.delete_configuration ⇒ Object
79
80
81
82
|
# File 'lib/nimbu/auth.rb', line 79
def delete_configuration
FileUtils.rm_f(configuration_file)
@host = nil
end
|
.delete_credentials ⇒ Object
182
183
184
185
|
# File 'lib/nimbu/auth.rb', line 182
def delete_credentials
FileUtils.rm_f(credentials_file)
@client, @credentials = nil, nil
end
|
.echo_off ⇒ Object
203
204
205
206
207
|
# File 'lib/nimbu/auth.rb', line 203
def echo_off
with_tty do
system "stty -echo"
end
end
|
.echo_on ⇒ Object
209
210
211
212
213
|
# File 'lib/nimbu/auth.rb', line 209
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
65
66
67
|
# File 'lib/nimbu/auth.rb', line 65
def get_configuration
@configuration ||= (read_configuration || ask_for_and_save_configuration)
end
|
.get_credentials ⇒ Object
178
179
180
|
# File 'lib/nimbu/auth.rb', line 178
def get_credentials @credentials ||= (read_credentials || ask_for_and_save_credentials)
end
|
.get_nimbu_site ⇒ Object
57
58
59
|
# File 'lib/nimbu/auth.rb', line 57
def get_nimbu_site
get_configuration["site"]
end
|
.get_nimbu_theme ⇒ Object
61
62
63
|
# File 'lib/nimbu/auth.rb', line 61
def get_nimbu_theme
get_configuration["theme"] || "default-theme"
end
|
.host ⇒ Object
37
38
39
|
# File 'lib/nimbu/auth.rb', line 37
def host
ENV['NIMBU_HOST'] || "https://api.nimbu.io"
end
|
.login ⇒ Object
23
24
25
26
|
# File 'lib/nimbu/auth.rb', line 23
def login
delete_credentials
get_credentials
end
|
.logout ⇒ Object
28
29
30
|
# File 'lib/nimbu/auth.rb', line 28
def logout
delete_credentials
end
|
.print_separator ⇒ Object
288
289
290
291
292
|
# File 'lib/nimbu/auth.rb', line 288
def print_separator
print "\n"
60.times { print "#"}
print "\n"
end
|
.read_configuration ⇒ Object
146
147
148
149
150
151
152
153
|
# File 'lib/nimbu/auth.rb', line 146
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
187
188
189
190
191
192
193
194
|
# File 'lib/nimbu/auth.rb', line 187
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
162
163
164
|
# File 'lib/nimbu/auth.rb', line 162
def reauthorize
@credentials = ask_for_and_save_credentials
end
|
.retry_login? ⇒ Boolean
282
283
284
285
286
|
# File 'lib/nimbu/auth.rb', line 282
def retry_login?
@login_attempts ||= 0
@login_attempts += 1
@login_attempts < 3
end
|
.site ⇒ Object
49
50
51
|
# File 'lib/nimbu/auth.rb', line 49
def site
@site ||= ENV['NIMBU_SITE'] || get_nimbu_site
end
|
.theme ⇒ Object
53
54
55
|
# File 'lib/nimbu/auth.rb', line 53
def theme
@theme ||= ENV['NIMBU_THEME'] || get_nimbu_theme
end
|
.token ⇒ Object
166
167
168
|
# File 'lib/nimbu/auth.rb', line 166
def token get_credentials
end
|
.user_agent ⇒ Object
19
20
21
|
# File 'lib/nimbu/auth.rb', line 19
def user_agent
"nimbu-toolbelt/#{Nimbu::VERSION} (#{RUBY_PLATFORM}) ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}".freeze
end
|
.write_configuration ⇒ Object
155
156
157
158
159
160
|
# File 'lib/nimbu/auth.rb', line 155
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
196
197
198
199
200
201
|
# File 'lib/nimbu/auth.rb', line 196
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
|