# File lib/heroku/auth.rb, line 54 def api_key Heroku::Client.auth(user, password)["api_key"] end
# File lib/heroku/auth.rb, line 192 def ask_for_and_save_credentials begin @credentials = ask_for_credentials write_credentials check rescue ::RestClient::Unauthorized, ::RestClient::ResourceNotFound => e delete_credentials display "Authentication failed." retry if retry_login? exit 1 rescue Exception => e delete_credentials raise e end check_for_associated_ssh_key unless Heroku::Command.current_command == "keys:add" @credentials end
# File lib/heroku/auth.rb, line 153 def ask_for_credentials puts "Enter your Heroku credentials." print "Email: " user = ask print "Password (typing will be hidden): " password = running_on_windows? ? ask_for_password_on_windows : ask_for_password api_key = Heroku::Client.auth(user, password)['api_key'] [user, api_key] end
# File lib/heroku/auth.rb, line 184 def ask_for_password echo_off password = ask puts echo_on return password end
# File lib/heroku/auth.rb, line 166 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 # received carriage return or newline if char == 127 || char == 8 # backspace and delete password.slice!(-1, 1) else # windows might throw a -1 at us so make sure to handle RangeError (password << char.chr) rescue RangeError end end puts return password end
# File lib/heroku/auth.rb, line 252 def associate_key(key) display "Uploading SSH public key #{key}" client.add_key(File.read(key)) end
# File lib/heroku/auth.rb, line 215 def associate_or_generate_ssh_key public_keys = Dir.glob("#{home_directory}/.ssh/*.pub").sort case public_keys.length when 0 then display "Could not find an existing public key." display "Would you like to generate one? [Yn] ", false unless ask.strip.downcase == "n" display "Generating new SSH public key." generate_ssh_key("id_rsa") associate_key("#{home_directory}/.ssh/id_rsa.pub") end when 1 then display "Found existing public key: #{public_keys.first}" associate_key(public_keys.first) else display "Found the following SSH public keys:" public_keys.each_with_index do |key, index| display "#{index+1}) #{File.basename(key)}" end display "Which would you like to use with your Heroku account? ", false chosen = public_keys[ask.to_i-1] rescue error("Invalid choice") associate_key(chosen) end end
just a stub; will raise if not authenticated
# File lib/heroku/auth.rb, line 30 def check client.list end
# File lib/heroku/auth.rb, line 210 def check_for_associated_ssh_key return unless client.keys.empty? associate_or_generate_ssh_key end
# File lib/heroku/auth.rb, line 12 def client @client ||= begin client = Heroku::Client.new(user, password, host) client.on_warning { |msg| self.display("\n#{msg}\n\n") } client end end
# File lib/heroku/auth.rb, line 34 def default_host "heroku.com" end
# File lib/heroku/auth.rb, line 62 def delete_credentials if File.exists?(legacy_credentials_path) FileUtils.rm_f(legacy_credentials_path) end if netrc netrc.delete("api.#{host}") netrc.delete("code.#{host}") netrc.save end @client, @credentials = nil, nil end
# File lib/heroku/auth.rb, line 141 def echo_off with_tty do system "stty -echo" end end
# File lib/heroku/auth.rb, line 147 def echo_on with_tty do system "stty echo" end end
# File lib/heroku/auth.rb, line 241 def generate_ssh_key(keyfile) ssh_dir = File.join(home_directory, ".ssh") unless File.exists?(ssh_dir) FileUtils.mkdir_p ssh_dir unless running_on_windows? File.chmod(0700, ssh_dir) end end %xssh-keygen -t rsa -N "" -f \"#{home_directory}/.ssh/#{keyfile}\" 2>&1` end
# File lib/heroku/auth.rb, line 38 def host ENV['HEROKU_HOST'] || default_host end
# File lib/heroku/auth.rb, line 74 def legacy_credentials_path if host == default_host "#{home_directory}/.heroku/credentials" else "#{home_directory}/.heroku/credentials.#{CGI.escape(host)}" end end
# File lib/heroku/auth.rb, line 20 def login delete_credentials get_credentials end
# File lib/heroku/auth.rb, line 25 def logout delete_credentials end
# File lib/heroku/auth.rb, line 82 def netrc_path if running_on_windows? "#{home_directory}/_netrc" else "#{home_directory}/.netrc" end end
# File lib/heroku/auth.rb, line 103 def read_credentials if ENV['HEROKU_API_KEY'] ['', ENV['HEROKU_API_KEY']] else # convert legacy credentials to netrc if File.exists?(legacy_credentials_path) @client = nil @credentials = File.read(legacy_credentials_path).split("\n") write_credentials FileUtils.rm_f(legacy_credentials_path) end # read netrc credentials if they exist if netrc # force migration of long api tokens (80 chars) to short ones (40) # #write_credentials rewrites both api.* and code.* credentials = netrc["api.#{host}"] if credentials && credentials[1].length > 40 @credentials = [ credentials[0], credentials[1][0,40] ] write_credentials end netrc["api.#{host}"] end end end
# File lib/heroku/auth.rb, line 257 def retry_login? @login_attempts ||= 0 @login_attempts += 1 @login_attempts < 3 end
# File lib/heroku/auth.rb, line 130 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["api.#{host}"] = self.credentials netrc["code.#{host}"] = self.credentials netrc.save end