Class: Blockspring::CLI::Auth
- Inherits:
-
Object
- Object
- Blockspring::CLI::Auth
show all
- Extended by:
- Helpers
- Defined in:
- lib/blockspring/cli/auth.rb
Class Method Summary
collapse
Methods included from Helpers
display, error, error_with_failure, error_with_failure=, format_with_bang, home_directory, longest, output_with_bang, running_on_a_mac?, running_on_windows?
Class Method Details
.api_key(login, password) ⇒ Object
83
84
85
86
87
88
89
90
|
# File 'lib/blockspring/cli/auth.rb', line 83
def api_key(login, password)
response = RestClient.post "#{base_url}/cli/login", { login: login, password: password }, user_agent: Blockspring::CLI.user_agent
if response.code == 200
response.to_str
else
raise 'login failed'
end
end
|
.ask ⇒ Object
38
39
40
|
# File 'lib/blockspring/cli/auth.rb', line 38
def ask
$stdin.gets.to_s.strip
end
|
.ask_for_and_save_credentials ⇒ Object
108
109
110
111
112
|
# File 'lib/blockspring/cli/auth.rb', line 108
def ask_for_and_save_credentials
@credentials = ask_for_credentials
write_credentials
@credentials
end
|
.ask_for_credentials ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/blockspring/cli/auth.rb', line 71
def ask_for_credentials
puts "Enter your Blockspring credentials."
print "Username or email: "
login = ask
print "Password (typing will be hidden): "
password = running_on_windows? ? ask_for_password_on_windows : ask_for_password
[login, api_key(login, password)]
end
|
.ask_for_password ⇒ Object
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/blockspring/cli/auth.rb', line 42
def ask_for_password
begin
echo_off
password = ask
puts
ensure
echo_on
end
return password
end
|
.ask_for_password_on_windows ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/blockspring/cli/auth.rb', line 53
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
|
.base_url ⇒ Object
12
13
14
15
|
# File 'lib/blockspring/cli/auth.rb', line 12
def base_url
protocol = ENV['BLOCKSPRING_API_PROTOCOL'] || 'http'
"#{protocol}://#{host}"
end
|
.delete_credentials ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/blockspring/cli/auth.rb', line 96
def delete_credentials
if netrc
netrc.delete(host)
netrc.save
end
@credentials = nil
end
|
.echo_off ⇒ Object
26
27
28
29
30
|
# File 'lib/blockspring/cli/auth.rb', line 26
def echo_off
with_tty do
system "stty -echo"
end
end
|
.echo_on ⇒ Object
32
33
34
35
36
|
# File 'lib/blockspring/cli/auth.rb', line 32
def echo_on
with_tty do
system "stty echo"
end
end
|
.get_credentials ⇒ Object
104
105
106
|
# File 'lib/blockspring/cli/auth.rb', line 104
def get_credentials
@credentials ||= (read_credentials || ask_for_and_save_credentials)
end
|
.host ⇒ Object
7
8
9
|
# File 'lib/blockspring/cli/auth.rb', line 7
def host
ENV['BLOCKSPRING_API_HOST'] || 'localhost:3000'
end
|
.netrc ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/blockspring/cli/auth.rb', line 134
def netrc
@netrc ||= begin
File.exists?(netrc_path) && Netrc.read(netrc_path)
rescue => error
if error.message =~ /^Permission bits for/
perm = File.stat(netrc_path).mode & 0777
abort("Permissions #{perm} for '#{netrc_path}' are too open. You should run `chmod 0600 #{netrc_path}` so that your credentials are NOT accessible by others.")
else
raise error
end
end
end
|
.netrc_path ⇒ Object
124
125
126
127
128
129
130
131
132
|
# File 'lib/blockspring/cli/auth.rb', line 124
def netrc_path
default = Netrc.default_path
encrypted = default + ".gpg"
if File.exists?(encrypted)
encrypted
else
default
end
end
|
.read_credentials ⇒ Object
147
148
149
150
151
152
153
154
155
|
# File 'lib/blockspring/cli/auth.rb', line 147
def read_credentials
if ENV['BLOCKSPRING_API_KEY']
['', ENV['BLOCKSPRING_API_KEY']]
else
if netrc
netrc[host]
end
end
end
|
.reauthorize ⇒ Object
92
93
94
|
# File 'lib/blockspring/cli/auth.rb', line 92
def reauthorize
@credentials = ask_for_and_save_credentials
end
|
.with_tty(&block) ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/blockspring/cli/auth.rb', line 17
def with_tty(&block)
return unless $stdin.isatty
begin
yield
rescue
end
end
|
.write_credentials ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'lib/blockspring/cli/auth.rb', line 114
def write_credentials
FileUtils.mkdir_p(File.dirname(netrc_path))
FileUtils.touch(netrc_path)
unless running_on_windows?
FileUtils.chmod(0600, netrc_path)
end
netrc[host] = @credentials
netrc.save
end
|