Class: Strongspace::Command::Auth
- Inherits:
-
Base
show all
- Defined in:
- lib/strongspace/commands/auth.rb
Instance Attribute Summary collapse
Attributes inherited from Base
#args
Instance Method Summary
collapse
Methods inherited from Base
#initialize, #strongspace
#base_command, #command, included
Methods included from Helpers
#ask, #backup_space?, #bin_folder, #command_name, #computername, #confirm, #confirm_command, #create_pid_file, #credentials_file, #credentials_folder, #delete_pid_file, #display, #error, #format_date, #gui_ssh_key, home_directory, #home_directory, #kill_via_pidfile, #launchd_agents_folder, #logs_folder, #pid_file_path, #pid_from_pid_file, #pids_folder, #plugins_folder, #process_running?, #redisplay, running_on_a_mac?, #running_on_a_mac?, #running_on_windows?, running_on_windows?, #shell, #space_exist?, #ssh_binary, #ssh_keygen_binary, support_directory, #support_directory
Instance Attribute Details
#credentials ⇒ Object
Returns the value of attribute credentials.
3
4
5
|
# File 'lib/strongspace/commands/auth.rb', line 3
def credentials
@credentials
end
|
Instance Method Details
#ask_for_credentials ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/strongspace/commands/auth.rb', line 89
def ask_for_credentials
if ENV["STRONGSPACE_DISPLAY"] == "silent"
return [nil, nil]
end
puts "Enter your Strongspace credentials."
print "Username or Email: "
user = ask
print "Password: "
password = running_on_windows? ? ask_for_password_on_windows : ask_for_password
r = Strongspace::Client.auth(user, password, host)
[r['username'], r['api_token']]
end
|
#ask_for_password ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/strongspace/commands/auth.rb', line 133
def ask_for_password
echo_off
password = ask
puts
echo_on
return password
end
|
#ask_for_password_on_windows ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/strongspace/commands/auth.rb', line 115
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
|
#authenticated_login ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/strongspace/commands/auth.rb', line 41
def authenticated_login
if args.blank?
url = "#{host}/login/#{client.login_token['login_token']}"
else
to = URI.escape(args[0][0..1]) + URI.escape(URI.escape(args[0][2..-1])).gsub('&', '%26')
url = "#{host}/login/#{client.login_token['login_token']}?to=#{to}"
end
if running_on_a_mac?
`open "#{url}"`
elsif running_on_windows?
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
shell.shellExecute(url, '', '', 'open', 3)
end
end
|
#authorize! ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/strongspace/commands/auth.rb', line 28
def authorize!
@credentials = [args.first, args[1]]
r = Strongspace::Client.auth(@credentials[0], @credentials[1], host)
if r
@credentials[0] = r['username']
@credentials[1] = r['api_token']
write_credentials
return true
end
return false
end
|
just a stub; will raise if not authenticated
15
16
17
|
# File 'lib/strongspace/commands/auth.rb', line 15
def check
client.spaces
end
|
5
6
7
|
# File 'lib/strongspace/commands/auth.rb', line 5
def client
@client ||= init_strongspace
end
|
#delete_credentials ⇒ Object
191
192
193
|
# File 'lib/strongspace/commands/auth.rb', line 191
def delete_credentials
FileUtils.rm_f(credentials_file)
end
|
81
82
83
|
# File 'lib/strongspace/commands/auth.rb', line 81
def echo_off
system "stty -echo"
end
|
85
86
87
|
# File 'lib/strongspace/commands/auth.rb', line 85
def echo_on
system "stty echo"
end
|
#get_credentials ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/strongspace/commands/auth.rb', line 68
def get_credentials return if @credentials
unless @credentials = read_credentials
@credentials = ask_for_credentials
save_credentials
end
@credentials
end
|
19
20
21
|
# File 'lib/strongspace/commands/auth.rb', line 19
def host
ENV['STRONGSPACE_HOST'] || 'https://www.strongspace.com'
end
|
#init_strongspace ⇒ Object
9
10
11
12
|
# File 'lib/strongspace/commands/auth.rb', line 9
def init_strongspace
client = Strongspace::Client.new(user, password, host)
client
end
|
63
64
65
66
|
# File 'lib/strongspace/commands/auth.rb', line 63
def password get_credentials
@credentials[1]
end
|
#read_credentials ⇒ Object
77
78
79
|
# File 'lib/strongspace/commands/auth.rb', line 77
def read_credentials
File.exists?(credentials_file) and File.read(credentials_file).split("\n")
end
|
#reauthorize_interactve ⇒ Object
23
24
25
26
|
# File 'lib/strongspace/commands/auth.rb', line 23
def reauthorize_interactve
@credentials = ask_for_credentials
write_credentials
end
|
#retry_login? ⇒ Boolean
167
168
169
170
171
|
# File 'lib/strongspace/commands/auth.rb', line 167
def retry_login?
@login_attempts ||= 0
@login_attempts += 1
@login_attempts < 3
end
|
#save_credentials ⇒ Object
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
|
# File 'lib/strongspace/commands/auth.rb', line 141
def save_credentials
if args[0] and args[1]
@credentials = []
@credentials[0] = args[0]
@credentials[1] = args[1]
end
begin
write_credentials
command = 'auth:check'
Strongspace::Command.run_internal(command, args)
rescue RestClient::Unauthorized => e
delete_credentials
raise e unless retry_login?
display "\nAuthentication failed"
@credentials = ask_for_credentials
@client = init_strongspace
retry
rescue Exception => e
delete_credentials
raise e
end
end
|
#set_credentials_permissions ⇒ Object
186
187
188
189
|
# File 'lib/strongspace/commands/auth.rb', line 186
def set_credentials_permissions
FileUtils.chmod 0700, File.dirname(credentials_file)
FileUtils.chmod 0600, credentials_file
end
|
58
59
60
61
|
# File 'lib/strongspace/commands/auth.rb', line 58
def user get_credentials
@credentials[0]
end
|
#valid_saved_credentials? ⇒ Boolean
106
107
108
109
110
111
112
113
|
# File 'lib/strongspace/commands/auth.rb', line 106
def valid_saved_credentials?
if File.exists?(credentials_file)
credentials = read_credentials
r = Strongspace::Client.auth(credentials[0], credentials[1], host)
return !r.blank?
end
return false
end
|
#write_credentials ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/strongspace/commands/auth.rb', line 173
def write_credentials
begin
FileUtils.mkdir_p(credentials_folder)
rescue Errno::EEXIST => e
end
File.open(credentials_file, 'w') do |f|
f.puts self.credentials
end
set_credentials_permissions
end
|