Class: Zillabyte::Auth

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/zillabyte/auth.rb

Defined Under Namespace

Classes: InvalidCredentialsException, NetrcError

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Helpers

add_git_remote, app, ask, create_git_remote, display, error, extract_app_from_git_config, extract_app_in_dir, format_with_bang, get_flow_name, git, handle_downloading_manifest, has_git?, longest, read_multiline, with_tty

Class Attribute Details

.credentials ⇒ Object

Returns the value of attribute credentials.



10
11
12
# File 'lib/zillabyte/auth.rb', line 10

def credentials
  @credentials
end

Class Method Details

.api_key ⇒ Object

get_credentials end



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

def api_key
  get_credentials[1]
end

.ask_for_and_save_credentials ⇒ Object



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
247
# File 'lib/zillabyte/auth.rb', line 163

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
  auth_token
end

.default_host ⇒ Object

just a stub; will raise if not authenticated def check api.get_user end



28
29
30
# File 'lib/zillabyte/auth.rb', line 28

def default_host
  ENV['ZILLABYTE_API_HOST'] || 'api.zillabyte.com'
end

.delete_credentials ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/zillabyte/auth.rb', line 119

def delete_credentials
  begin 
    netrc.delete(host) 
  rescue Zillabyte::Auth::NetrcError => error
    display 'Netrc Delete Error'
    display error.message
    exit 1
  end

  tries = 0
  begin
    netrc.save
  rescue Zillabyte::Auth::NetrcError => error
    tries +=1
    display 'netrc save error'
    error.message
    retry if tries <= 3
      exit 1
  end
end

.echo_off ⇒ Object



141
142
143
144
145
# File 'lib/zillabyte/auth.rb', line 141

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

.echo_on ⇒ Object



147
148
149
150
151
# File 'lib/zillabyte/auth.rb', line 147

def echo_on
  with_tty do
    system "stty echo"
  end
end

.email?(s) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/zillabyte/auth.rb', line 81

def email?(s)
  !!(s =~ /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i)
end

.get_credentials ⇒ Object

:nodoc:



52
53
54
# File 'lib/zillabyte/auth.rb', line 52

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

.git_host ⇒ Object



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

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

.host ⇒ Object



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

def host
  default_host
end

.login(email, auth_token) ⇒ Object



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

def (email, auth_token)
  if valid_credentials?(email, auth_token)
    write_credentials(email, auth_token)
    display "Authentication complete."
  end
end

.logout ⇒ Object



19
20
21
# File 'lib/zillabyte/auth.rb', line 19

def logout
  delete_credentials
end

.netrc ⇒ Object

:nodoc:



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zillabyte/auth.rb', line 67

def netrc   # :nodoc:
  @netrc ||= begin
    Netrc.read(netrc_path)
  rescue Zillabyte::Auth::NetrcError => 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



57
58
59
60
61
62
63
64
65
# File 'lib/zillabyte/auth.rb', line 57

def netrc_path
  default = Netrc.default_path
  encrypted = default + ".gpg"
  if File.exists?(encrypted)
    encrypted
  else
    default
  end
end

.read_credentials ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zillabyte/auth.rb', line 85

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)


249
250
251
252
253
# File 'lib/zillabyte/auth.rb', line 249

def retry_login?
  @login_attempts ||= 0
  @login_attempts += 1
  @login_attempts < 3
end

.user ⇒ Object

:nodoc:



40
41
42
# File 'lib/zillabyte/auth.rb', line 40

def user    # :nodoc:
  get_credentials[0]
end

.valid_credentials?(email, auth_token) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
158
159
160
161
# File 'lib/zillabyte/auth.rb', line 153

def valid_credentials?(email, auth_token)
  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



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/zillabyte/auth.rb', line 105

def write_credentials(email, auth_token)
  begin
    FileUtils.mkdir_p(File.dirname(netrc_path))
    FileUtils.touch(netrc_path)
    FileUtils.chmod 0600, netrc_path
    netrc[host] = [email, auth_token] 
    netrc.save
  rescue Zillabyte::Auth::NetrcError => error
    display 'Netrc Read or Save Error'
    display error.message
    exit 1
  end
end