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
43
44
45
|
# File 'lib/zillabyte/auth.rb', line 43
def api_key
get_credentials[1]
end
|
.ask_for_and_save_credentials ⇒ Object
162
163
164
165
166
167
168
169
170
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
|
# File 'lib/zillabyte/auth.rb', line 162
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/zillabyte/auth.rb', line 117
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
139
140
141
142
143
|
# File 'lib/zillabyte/auth.rb', line 139
def echo_off
with_tty do
system "stty -echo"
end
end
|
.echo_on ⇒ Object
145
146
147
148
149
|
# File 'lib/zillabyte/auth.rb', line 145
def echo_on
with_tty do
system "stty echo"
end
end
|
.email?(s) ⇒ Boolean
78
79
80
|
# File 'lib/zillabyte/auth.rb', line 78
def email?(s)
!!(s =~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i)
end
|
.get_credentials ⇒ Object
47
48
49
|
# File 'lib/zillabyte/auth.rb', line 47
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
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/zillabyte/auth.rb', line 63
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
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/zillabyte/auth.rb', line 52
def netrc_path
require("netrc")
default = Netrc.default_path
encrypted = default + ".gpg"
if File.exists?(encrypted)
encrypted
else
default
end
end
|
.read_credentials ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/zillabyte/auth.rb', line 82
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
248
249
250
251
252
|
# File 'lib/zillabyte/auth.rb', line 248
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
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/zillabyte/auth.rb', line 151
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/zillabyte/auth.rb', line 102
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
|