Class: AppSendr::Command::Auth

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

Instance Attribute Summary collapse

Attributes inherited from Base

#args, #autodetected_app

Instance Method Summary collapse

Methods inherited from Base

#appsendr, #ask, #confirm, #extract_app, #extract_option, #format_date, #initialize, #option_exists?

Methods included from Helpers

#credentials_file, #credentials_setup?, #display, #error, #has_project_droppr?, #home_directory, #in_project_dir?, #project_appsendr, #project_appsendr_app, #read_app, #read_app_id, #require_in_project_and_no_droppr, #require_project, #require_project_dir, #require_project_droppr, #running_on_a_mac?, #running_on_windows?

Constructor Details

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

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

Instance Method Details

#api_keyObject



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

def api_key
    get_credentials
    @credentials[0]
end

#ask_for_credentialsObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/appsendr/commands/auth.rb', line 73

def ask_for_credentials
  puts "Enter your AppSendr credentials."

  print "Username: "
  user = ask

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

  [ user, password ]
end

#ask_for_passwordObject



103
104
105
106
107
108
109
# File 'lib/appsendr/commands/auth.rb', line 103

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

#ask_for_password_on_windowsObject



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

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

#auth_credentials(user, pass) ⇒ Object



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

def auth_credentials(user,pass)
    AppSendr::Client.auth(user,pass,host)
end

#checkObject

just a stub; will raise if not authenticated



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

def check
  client.list
end

#clientObject



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

def client
  @client ||= init_for_credentials
end

#delete_credentialsObject



151
152
153
# File 'lib/appsendr/commands/auth.rb', line 151

def delete_credentials
  FileUtils.rm_f(credentials_file)
end

#echo_offObject



65
66
67
# File 'lib/appsendr/commands/auth.rb', line 65

def echo_off
  system "stty -echo"
end

#echo_onObject



69
70
71
# File 'lib/appsendr/commands/auth.rb', line 69

def echo_on
  system "stty echo"
end

#get_credentialsObject

def user # :nodoc:

get_credentials
@credentials[0]

end

def password # :nodoc:

get_credentials
@credentials[1]

end



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/appsendr/commands/auth.rb', line 49

def get_credentials    # :nodoc:
  return if @credentials
  unless @credentials = read_credentials
    user_pass = ask_for_credentials
    @credentials = auth_credentials(user_pass[0],user_pass[1])

    #@credentials = ask_for_credentials
    save_credentials
  end
  @credentials
end

#hostObject



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

def host
  ENV['APPDROPPR_HOST'] || 'appsendr.com' ##'appsendr.heroku.com' #
end

#init_for_credentialsObject



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

def init_for_credentials
  client = AppSendr::Client.new(api_key, host)
  #client.on_warning { |msg| self.display("\n#{msg}\n\n") }
  client
end

#read_credentialsObject



61
62
63
# File 'lib/appsendr/commands/auth.rb', line 61

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

#reauthorizeObject



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

def reauthorize
    user_pass = ask_for_credentials
    @credentials = auth_credentials(user_pass[0],user_pass[1])
    write_credentials
end

#retry_login?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
# File 'lib/appsendr/commands/auth.rb', line 132

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

#save_credentialsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/appsendr/commands/auth.rb', line 111

def save_credentials
  begin
    write_credentials
    #command = args.any? { |a| a == '--ignore-keys' } ? 'auth:check' : 'keys:add'
    command = 'auth:check'
    AppSendr::Command.run_internal(command, args)
  rescue RestClient::Unauthorized => e
    delete_credentials
    raise e unless retry_login?

    display "\nAuthentication failed"
    user_pass = ask_for_credentials
    @credentials = auth_credentials(user_pass[0],user_pass[1])
    @client = init_for_credentials
    retry
  rescue Exception => e
    delete_credentials
    raise e
  end
end

#set_credentials_permissionsObject



146
147
148
149
# File 'lib/appsendr/commands/auth.rb', line 146

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

#write_credentialsObject



138
139
140
141
142
143
144
# File 'lib/appsendr/commands/auth.rb', line 138

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