Class: Strongspace::Command::Auth

Inherits:
Base show all
Defined in:
lib/strongspace/commands/auth.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#args

Instance Method Summary collapse

Methods inherited from Base

#initialize, #strongspace

Methods included from PluginInterface

#base_command, #command, included

Methods included from Helpers

#ask, #backup_space?, #bin_folder, #command_name, #computername, #confirm, #confirm_command, #create_pid_file, #credentials_file, #credentials_folder, #delete_pid_file, #display, #error, #format_date, #gui_ssh_key, home_directory, #home_directory, #kill_via_pidfile, #launchd_agents_folder, #logs_folder, #pid_file_path, #pid_from_pid_file, #pids_folder, #plugins_folder, #process_running?, #redisplay, running_on_a_mac?, #running_on_a_mac?, #running_on_windows?, running_on_windows?, #shell, #space_exist?, support_directory, #support_directory

Constructor Details

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

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

Instance Method Details

#ask_for_credentialsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/strongspace/commands/auth.rb', line 82

def ask_for_credentials
  if ENV["STRONGSPACE_DISPLAY"] == "silent"
    return [nil, nil]
  end

  puts "Enter your Strongspace credentials."

  print "Username or Email: "
  user = ask

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

  r = Strongspace::Client.auth(user, password, host)
  [r['username'], r['api_token']]
end

#ask_for_passwordObject



126
127
128
129
130
131
132
# File 'lib/strongspace/commands/auth.rb', line 126

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

#ask_for_password_on_windowsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/strongspace/commands/auth.rb', line 108

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

#authenticated_loginObject



41
42
43
44
45
46
47
48
49
# File 'lib/strongspace/commands/auth.rb', line 41

def 
  if args.blank?
    url = "#{host}/login/#{client.['login_token']}"
  else
    to = URI.escape(args[0][0..1]) + URI.escape(URI.escape(args[0][2..-1])).gsub('&', '%26')
    url = "#{host}/login/#{client.['login_token']}?to=#{to}"
  end
  `open "#{url}"`
end

#authorize!Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/strongspace/commands/auth.rb', line 28

def authorize!
  @credentials = [args.first, args[1]]
  r = Strongspace::Client.auth(@credentials[0], @credentials[1], host)
  if r
    @credentials[0] = r['username']
    @credentials[1] = r['api_token']
    write_credentials
    return true
  end

  return false
end

#checkObject

just a stub; will raise if not authenticated



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

def check
  client.spaces
end

#clientObject



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

def client
  @client ||= init_strongspace
end

#delete_credentialsObject



184
185
186
# File 'lib/strongspace/commands/auth.rb', line 184

def delete_credentials
  FileUtils.rm_f(credentials_file)
end

#echo_offObject



74
75
76
# File 'lib/strongspace/commands/auth.rb', line 74

def echo_off
  system "stty -echo"
end

#echo_onObject



78
79
80
# File 'lib/strongspace/commands/auth.rb', line 78

def echo_on
  system "stty echo"
end

#get_credentialsObject

:nodoc:



61
62
63
64
65
66
67
68
# File 'lib/strongspace/commands/auth.rb', line 61

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

#hostObject



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

def host
  ENV['STRONGSPACE_HOST'] || 'https://www.strongspace.com'
end

#init_strongspaceObject



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

def init_strongspace
  client = Strongspace::Client.new(user, password, host)
  client
end

#passwordObject

:nodoc:



56
57
58
59
# File 'lib/strongspace/commands/auth.rb', line 56

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

#read_credentialsObject



70
71
72
# File 'lib/strongspace/commands/auth.rb', line 70

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

#reauthorize_interactveObject



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

def reauthorize_interactve
  @credentials = ask_for_credentials
  write_credentials
end

#retry_login?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
# File 'lib/strongspace/commands/auth.rb', line 160

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

#save_credentialsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/strongspace/commands/auth.rb', line 134

def save_credentials

  if args[0] and args[1]
    @credentials = []
    @credentials[0] = args[0]
    @credentials[1] = args[1]
  end

  begin
    write_credentials
    command = 'auth:check'
    Strongspace::Command.run_internal(command, args)
  rescue RestClient::Unauthorized => e
    delete_credentials
    raise e unless retry_login?

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

#set_credentials_permissionsObject



179
180
181
182
# File 'lib/strongspace/commands/auth.rb', line 179

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

#userObject

:nodoc:



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

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

#valid_saved_credentials?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'lib/strongspace/commands/auth.rb', line 99

def valid_saved_credentials?
  if File.exists?(credentials_file)
    credentials = read_credentials
    r = Strongspace::Client.auth(credentials[0], credentials[1], host)
    return !r.blank?
  end
  return false
end

#write_credentialsObject



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/strongspace/commands/auth.rb', line 166

def write_credentials
  begin
    FileUtils.mkdir_p(credentials_folder)
  rescue Errno::EEXIST => e

  end

  File.open(credentials_file, 'w') do |f|
    f.puts self.credentials
  end
  set_credentials_permissions
end