Class: Zillabyte::Auth

Inherits:
Object
  • 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

.credentialsObject

Returns the value of attribute credentials.



5
6
7
# File 'lib/zillabyte/auth.rb', line 5

def credentials
  @credentials
end

Class Method Details

.api_keyObject



43
44
45
# File 'lib/zillabyte/auth.rb', line 43

def api_key
  get_credentials[1]
end

.ask_for_and_save_credentialsObject



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
    
    # Get the email address
    print "Email: " 
    $stdout.flush()
    email = ask
    if email?(email) == false
      display "invalid email"
      exit 1 
    end
    
    # Get the auth token
    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
        
        # Success... 
        write_credentials(email, auth_token)
        
        # Maybe add ssh keys while we're at it... 
        # possible_keys = [
        #   "~/.ssh/id_rsa.pub", 
        #   "~/.ssh/id_dsa.pub"
        # ]
        
        # key_added = false
        # possible_keys.each do |keypath|
        #   if File.exists?(File.expand_path(keypath))
          
        #     display "Register SSH key #{keypath}? (y/N)", false
        #     break if (ask() || "").strip.downcase[0] != "y"
              
        #     api = Zillabyte::API.new(:api_key => auth_token, :session => self)
        #     begin
        #       key = File.binread(File.expand_path(keypath))
        #     rescue => e
        #       error(e.message, type)
        #       break
        #     end
        #     message = api.keys.add("default", key)
        #     display("SSH keys added.  Use 'zillabyte keys' to manage.")
        #     key_added = true
        #     break

        #   end
        # end
        
        # unless key_added
        #   display "Please run zillabyte keys:add to add your ssh keys."
        # end
        
        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_hostObject

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_credentialsObject



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_offObject



139
140
141
142
143
# File 'lib/zillabyte/auth.rb', line 139

def echo_off
  with_tty do
    system "stty -echo"
  end
end

.echo_onObject



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

Returns:

  • (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_credentialsObject

:nodoc:



47
48
49
# File 'lib/zillabyte/auth.rb', line 47

def get_credentials   # :nodoc:
  @credentials ||= (read_credentials || ask_for_and_save_credentials)
end

.git_hostObject



31
32
33
# File 'lib/zillabyte/auth.rb', line 31

def git_host
  ENV['ZILLABYTE_GIT_HOST'] || 'git.zillabyte.com'
end

.hostObject



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 (email, auth_token)
  if valid_credentials?(email, auth_token)
    write_credentials(email, auth_token)
    display "Authentication complete."
  end
end

.logoutObject



14
15
16
# File 'lib/zillabyte/auth.rb', line 14

def logout
  delete_credentials
end

.netrcObject

:nodoc:



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zillabyte/auth.rb', line 63

def netrc   # :nodoc:
  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_pathObject



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_credentialsObject



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
    # read netrc credentials if they exist
    if netrc
      if netrc[host] && email?(netrc[host][0])
        # Make sure we have the email address
        netrc[host]
      else
        nil
      end
    end
  end
end

.retry_login?Boolean

Returns:

  • (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

.userObject

:nodoc:



35
36
37
# File 'lib/zillabyte/auth.rb', line 35

def user    # :nodoc:
  get_credentials[0]
end

.valid_credentials?(email, auth_token) ⇒ Boolean

Returns:

  • (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