Class: SeleniumShots::Command::Auth

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

Instance Attribute Summary collapse

Attributes inherited from Base

#args

Instance Method Summary collapse

Methods inherited from Base

#ask, #ask_for_config_file, #config_file, #display, #home_directory, #initialize, #make_config_file, #running_on_windows?, #selenium_shots, #selenium_shots_api_key, #shell

Constructor Details

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

Instance Attribute Details

#api_key_hashObject

Returns the value of attribute api_key_hash.



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

def api_key_hash
  @api_key_hash
end

Instance Method Details

#api_keyObject



13
14
15
# File 'lib/selenium_shots/cli/commands/auth.rb', line 13

def api_key
  get_api_key
end

#api_key_fileObject



22
23
24
# File 'lib/selenium_shots/cli/commands/auth.rb', line 22

def api_key_file
  "#{home_directory}/.selenium_shots/api_key"
end

#ask_for_api_keyObject



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

def ask_for_api_key
  puts "Enter your SeleniumShots Account"

  print "Email: "
  user = ask

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

  [ user, password ]
end

#ask_for_passwordObject



78
79
80
81
82
83
84
# File 'lib/selenium_shots/cli/commands/auth.rb', line 78

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

#ask_for_password_on_windowsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/selenium_shots/cli/commands/auth.rb', line 61

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/selenium_shots/cli/commands/auth.rb', line 5

def client
  @client ||= init_selenium_shots
end

#delete_api_keyObject



122
123
124
# File 'lib/selenium_shots/cli/commands/auth.rb', line 122

def delete_api_key
  FileUtils.rm_f(api_key_file)
end

#echo_offObject



41
42
43
# File 'lib/selenium_shots/cli/commands/auth.rb', line 41

def echo_off
  system "stty -echo"
end

#echo_onObject



45
46
47
# File 'lib/selenium_shots/cli/commands/auth.rb', line 45

def echo_on
  system "stty echo"
end

#get_api_keyObject



26
27
28
29
30
31
32
33
# File 'lib/selenium_shots/cli/commands/auth.rb', line 26

def get_api_key
  return if @api_key_hash
  unless @api_key_hash = read_api_key
    @api_key_hash = ask_for_api_key
    save_api_key
  end
  @api_key_hash
end

#get_api_key_from_hostObject



17
18
19
20
# File 'lib/selenium_shots/cli/commands/auth.rb', line 17

def get_api_key_from_host
  RestClient.post 'http://www.seleniumshots.com/selenium_tests/get_api_key',  :user_session => { :email => @api_key_hash[0],
                                                                              :password => @api_key_hash[1]}
end

#init_selenium_shotsObject



9
10
11
# File 'lib/selenium_shots/cli/commands/auth.rb', line 9

def init_selenium_shots
  SeleniumShots::Client.new(api_key)
end

#read_api_keyObject



35
36
37
38
39
# File 'lib/selenium_shots/cli/commands/auth.rb', line 35

def read_api_key
  if File.exists? api_key_file
    return File.read(api_key_file).split("\n")
  end
end

#retry_login?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/selenium_shots/cli/commands/auth.rb', line 103

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

#save_api_keyObject



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

def save_api_key
  begin
    @api_key_hash = get_api_key_from_host
    write_api_key
  rescue RestClient::Unauthorized => e
    delete_api_key
    raise e unless retry_login?
    display "\nAuthentication failed"
    @api_key_hash = ask_for_api_key
    @client = init_selenium_shots
    retry
  rescue Exception => e
    delete_api_key
    raise e
  end
end

#set_api_key_permissionsObject



117
118
119
120
# File 'lib/selenium_shots/cli/commands/auth.rb', line 117

def set_api_key_permissions
  FileUtils.chmod 0700, File.dirname(api_key_file)
  FileUtils.chmod 0600, api_key_file
end

#write_api_keyObject



109
110
111
112
113
114
115
# File 'lib/selenium_shots/cli/commands/auth.rb', line 109

def write_api_key
  FileUtils.mkdir_p(File.dirname(api_key_file))
  File.open(api_key_file, 'w') do |f|
    f.puts self.api_key_hash
  end
  set_api_key_permissions
end