Class: Heroku::Command::Auth

Inherits:
Base
  • Object
show all
Defined in:
lib/heroku/commands/auth.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#args, #autodetected_app

Instance Method Summary collapse

Methods inherited from Base

#app_urls, #ask, #display, #error, #escape, #extract_app, #extract_app_in_dir, #extract_option, #format_date, #git_remotes, #git_url, #heroku, #initialize, #shell, #web_url

Methods included from Helpers

#home_directory, #running_on_a_mac?, #running_on_windows?

Constructor Details

This class inherits a constructor from Heroku::Command::Base

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



3
4
5
# File 'lib/heroku/commands/auth.rb', line 3

def credentials
  @credentials
end

Instance Method Details

#ask_for_credentialsObject



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/heroku/commands/auth.rb', line 59

def ask_for_credentials
  puts "Enter your Heroku credentials."

  print "Email: "
  user = ask

  print "Password: "
  password = running_on_windows? ? ask_for_password_on_windows : ask_for_password

  [ user, password ]
end

#ask_for_passwordObject



88
89
90
91
92
93
94
# File 'lib/heroku/commands/auth.rb', line 88

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

#ask_for_password_on_windowsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/heroku/commands/auth.rb', line 71

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
      password << char.chr
    end
  end
  puts
  return password
end

#clientObject



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

def client
  @client ||= init_heroku
end

#credentials_fileObject



34
35
36
# File 'lib/heroku/commands/auth.rb', line 34

def credentials_file
  "#{home_directory}/.heroku/credentials"
end

#delete_credentialsObject



133
134
135
# File 'lib/heroku/commands/auth.rb', line 133

def delete_credentials
  FileUtils.rm_f(credentials_file)
end

#echo_offObject



51
52
53
# File 'lib/heroku/commands/auth.rb', line 51

def echo_off
  system "stty -echo"
end

#echo_onObject



55
56
57
# File 'lib/heroku/commands/auth.rb', line 55

def echo_on
  system "stty echo"
end

#get_credentialsObject

:nodoc:



38
39
40
41
42
43
44
45
# File 'lib/heroku/commands/auth.rb', line 38

def get_credentials    # :nodoc:
  return if @credentials
  unless @credentials = read_credentials
    @credentials = ask_for_credentials
    save_credentials
  end
  @credentials
end

#hostObject



15
16
17
# File 'lib/heroku/commands/auth.rb', line 15

def host
  ENV['HEROKU_HOST'] || 'heroku.com'
end

#init_herokuObject



9
10
11
12
13
# File 'lib/heroku/commands/auth.rb', line 9

def init_heroku
  client = Heroku::Client.new(user, password, host)
  client.on_warning { |msg| self.display("\n#{msg}\n\n") }
  client
end

#passwordObject

:nodoc:



29
30
31
32
# File 'lib/heroku/commands/auth.rb', line 29

def password    # :nodoc:
  get_credentials
  @credentials[1]
end

#read_credentialsObject



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

def read_credentials
  File.exists?(credentials_file) and File.read(credentials_file).split("\n")
end

#reauthorizeObject



19
20
21
22
# File 'lib/heroku/commands/auth.rb', line 19

def reauthorize
  @credentials = ask_for_credentials
  write_credentials
end

#retry_login?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
# File 'lib/heroku/commands/auth.rb', line 114

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

#save_credentialsObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/heroku/commands/auth.rb', line 96

def save_credentials
  begin
    write_credentials
    Heroku::Command.run_internal('keys:add', args)
  rescue RestClient::Unauthorized => e
    delete_credentials
    raise e unless retry_login?

    display "\nAuthentication failed"
    @credentials = ask_for_credentials
    @client = init_heroku
    retry
  rescue Exception => e
    delete_credentials
    raise e
  end
end

#set_credentials_permissionsObject



128
129
130
131
# File 'lib/heroku/commands/auth.rb', line 128

def set_credentials_permissions
  FileUtils.chmod 0700, File.dirname(credentials_file)
  FileUtils.chmod 0600, credentials_file
end

#userObject

:nodoc:



24
25
26
27
# File 'lib/heroku/commands/auth.rb', line 24

def user    # :nodoc:
  get_credentials
  @credentials[0]
end

#write_credentialsObject



120
121
122
123
124
125
126
# File 'lib/heroku/commands/auth.rb', line 120

def write_credentials
  FileUtils.mkdir_p(File.dirname(credentials_file))
  File.open(credentials_file, 'w') do |f|
    f.puts self.credentials
  end
  set_credentials_permissions
end