Class: Zillabyte::Auth
- Inherits:
-
Object
show all
- Extended by:
- Helpers
- Defined in:
- lib/zillabyte/auth.rb
Defined Under Namespace
Classes: InvalidCredentialsException
Class Attribute Summary collapse
Class Method Summary
collapse
Methods included from Helpers
app, ask, command, create_git_remote, display, error, extract_app_from_git_config, extract_app_in_dir, format_with_bang, friendly_dir, get_flow_ui_link, get_info, get_rich_info, git, handle_downloading_manifest, has_git?, longest, read_multiline, truncate_message, version_okay?, with_tty
Class Attribute Details
.credentials ⇒ Object
Returns the value of attribute credentials.
5
6
7
|
# File 'lib/zillabyte/auth.rb', line 5
def credentials
@credentials
end
|
Class Method Details
.api_key ⇒ Object
52
53
54
|
# File 'lib/zillabyte/auth.rb', line 52
def api_key
get_credentials[1]
end
|
.api_key_or_nil ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/zillabyte/auth.rb', line 43
def api_key_or_nil
credentials = read_credentials
if credentials.nil?
return nil
else
return credentials[1]
end
end
|
.ask_for_and_save_credentials ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/zillabyte/auth.rb', line 171
def ask_for_and_save_credentials
if not read_credentials.nil?
display "Your credentials already exist. Re-enter them? [y/N] ", false
input = ask
if !(input.downcase == "y" || input.downcase == "yes")
exit 1
end
end
begin
current_command = Zillabyte::Command.current_command
msg = "Enter your Zillabyte credentials"
msg += " (or press enter to continue as an anonymous user)." if current_command == "relations"
puts msg
print "Email: "
$stdout.flush()
email = ask
if email?(email) == false
display "invalid email"
exit 1
end
print "Auth Token: "
$stdout.flush()
auth_token = ask
print "\n"
if current_command == "relations" and auth_token == ""
display "No auth token provided, continuing as anonymous user. Note that you will only be shown publicly available listings."
else
if not valid_credentials?(email, auth_token)
raise Zillabyte::Auth::InvalidCredentialsException
else
write_credentials(email, auth_token)
display "Authentication complete."
end
end
rescue Zillabyte::Auth::InvalidCredentialsException => e
display "Authentication failed."
retry if retry_login?
exit 1
end
[email, auth_token]
end
|
.default_host ⇒ Object
just a stub; will raise if not authenticated def check
api.get_user
end
23
24
25
|
# File 'lib/zillabyte/auth.rb', line 23
def default_host
ENV['ZILLABYTE_API_HOST'] || 'api.zillabyte.com'
end
|
.delete_credentials ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/zillabyte/auth.rb', line 126
def delete_credentials
begin
netrc.delete(host)
rescue Netrc::Error => error
display 'Netrc Delete Error'
display error.message
exit 1
end
tries = 0
begin
netrc.save
rescue Netrc::Error => error
tries +=1
display 'netrc save error'
error.message
retry if tries <= 3
exit 1
end
end
|
.echo_off ⇒ Object
148
149
150
151
152
|
# File 'lib/zillabyte/auth.rb', line 148
def echo_off
with_tty do
system "stty -echo"
end
end
|
.echo_on ⇒ Object
154
155
156
157
158
|
# File 'lib/zillabyte/auth.rb', line 154
def echo_on
with_tty do
system "stty echo"
end
end
|
.email?(s) ⇒ Boolean
87
88
89
|
# File 'lib/zillabyte/auth.rb', line 87
def email?(s)
!!(s =~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i)
end
|
.get_credentials ⇒ Object
56
57
58
|
# File 'lib/zillabyte/auth.rb', line 56
def get_credentials @credentials ||= (read_credentials || ask_for_and_save_credentials)
end
|
.git_host ⇒ Object
31
32
33
|
# File 'lib/zillabyte/auth.rb', line 31
def git_host
ENV['ZILLABYTE_GIT_HOST'] || 'git.zillabyte.com'
end
|
.host ⇒ Object
27
28
29
|
# File 'lib/zillabyte/auth.rb', line 27
def host
default_host
end
|
.login(email, auth_token) ⇒ Object
7
8
9
10
11
12
|
# File 'lib/zillabyte/auth.rb', line 7
def login(email, auth_token)
if valid_credentials?(email, auth_token)
write_credentials(email, auth_token)
display "Authentication complete."
end
end
|
.logout ⇒ Object
14
15
16
|
# File 'lib/zillabyte/auth.rb', line 14
def logout
delete_credentials
end
|
.netrc ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/zillabyte/auth.rb', line 72
def netrc require("netrc")
@netrc ||= begin
Netrc.read(netrc_path)
rescue Netrc::Error => 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
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/zillabyte/auth.rb', line 61
def netrc_path
require("netrc")
default = Netrc.default_path
encrypted = default + ".gpg"
if File.exists?(encrypted)
encrypted
else
default
end
end
|
.read_credentials ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/zillabyte/auth.rb', line 91
def read_credentials
if ENV['ZILLABYTE_API_KEY']
ary = ENV['ZILLABYTE_API_KEY'].split(",")
if ary.size != 2 or !email?(ary[0])
abort("ZILLABYTE_API_KEY must be comma delimited: [email protected],api_key")
end
ary
else
if netrc
if netrc[host] && email?(netrc[host][0])
netrc[host]
else
nil
end
end
end
end
|
.retry_login? ⇒ Boolean
257
258
259
260
261
|
# File 'lib/zillabyte/auth.rb', line 257
def retry_login?
@login_attempts ||= 0
@login_attempts += 1
@login_attempts < 3
end
|
.user ⇒ Object
35
36
37
|
# File 'lib/zillabyte/auth.rb', line 35
def user get_credentials[0]
end
|
.valid_credentials?(email, auth_token) ⇒ Boolean
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/zillabyte/auth.rb', line 160
def valid_credentials?(email, auth_token)
require("zillabyte/api")
api = Zillabyte::API.new(:api_key => auth_token, :session => self)
response = api.request(
:expects => 200,
:method => :get,
:path => "/cli/login"
)
response.body != "authentication error"
end
|
.write_credentials(email, auth_token) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/zillabyte/auth.rb', line 111
def write_credentials(email, auth_token)
begin
require("fileutils")
FileUtils.mkdir_p(File.dirname(netrc_path))
FileUtils.touch(netrc_path)
FileUtils.chmod 0600, netrc_path
netrc[host] = [email, auth_token]
netrc.save
rescue Netrc::Error => error
display 'Netrc Read or Save Error'
display error.message
exit 1
end
end
|