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?, #is_file_to_large?, #message, #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?, #size_of_file

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



36
37
38
39
# File 'lib/appsendr/commands/auth.rb', line 36

def api_key
    get_credentials
    @credentials[0]
end

#ask_for_credentialsObject



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

def ask_for_credentials
  puts "Enter your AppSendr credentials."

  print "Email: "
  user = ask

  print "Password: "
  password = ask_for_password

  [ user, password ]
end

#ask_for_passwordObject



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

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  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



135
136
137
# File 'lib/appsendr/commands/auth.rb', line 135

def delete_credentials
  FileUtils.rm_f(credentials_file)
end

#echo_offObject



67
68
69
# File 'lib/appsendr/commands/auth.rb', line 67

def echo_off
  system "stty -echo"
end

#echo_onObject



71
72
73
# File 'lib/appsendr/commands/auth.rb', line 71

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



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

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
27
28
# File 'lib/appsendr/commands/auth.rb', line 24

def host
  ENV['APPDROPPR_HOST'] || 'appsendr.com'
  #ENV['APPDROPPR_HOST'] || '0.0.0.0:3000'
  
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

#loginObject



30
31
32
33
34
# File 'lib/appsendr/commands/auth.rb', line 30

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

#read_credentialsObject



63
64
65
# File 'lib/appsendr/commands/auth.rb', line 63

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

#retry_login?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/appsendr/commands/auth.rb', line 116

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

#save_credentialsObject



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

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



130
131
132
133
# File 'lib/appsendr/commands/auth.rb', line 130

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

#write_credentialsObject



122
123
124
125
126
127
128
# File 'lib/appsendr/commands/auth.rb', line 122

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