Class: Wod::Command::Auth

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

Instance Attribute Summary collapse

Attributes inherited from Base

#args

Instance Method Summary collapse

Methods inherited from Base

#initialize, #wod

Methods included from Helpers

#ask, #display_formatted, #error, #home_directory, #last_page_file, #wod_directory

Constructor Details

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

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

Instance Method Details

#ask_for_and_save_credentialsObject



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

def ask_for_and_save_credentials
  begin
   @credentials = ask_for_credentials
   write_credentials
   check
  rescue ::Wod::InvalidCredentials
    delete_credentials
    @client = nil
    @credentials = nil
    puts "Authentication failed."
    return if retry_login?
    exit 1
  end
end

#ask_for_credentialsObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wod/commands/auth.rb', line 62

def ask_for_credentials
  puts "Enter your apple credentials"
  
  print "Apple ID: "
  user = ask
  
  print "Password: "
  password = ask_for_password

  [user, password]
end

#ask_for_passwordObject



74
75
76
77
78
79
80
# File 'lib/wod/commands/auth.rb', line 74

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

#checkObject

just a stub; will raise if not authenticated



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

def check
  client.logged_in?
end

#clientObject



7
8
9
10
# File 'lib/wod/commands/auth.rb', line 7

def client
  @client = Wod::Client.new :collect_login_deets => lambda { [user, password] },
                            :collect_team_selection => lambda { |team_names| team || collect_team_selection(team_names) }
end

#collect_team_selection(team_names) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/wod/commands/auth.rb', line 97

def collect_team_selection team_names
  puts "This account belongs to the following teams:"
  team_names.each_with_index do |team, i|
    puts "#{i+1}. #{team}"
  end
  print "Select team (1): "
  selection = ask
  selection = "1" if selection.empty? || selection.to_i == 0
  @credentials[2] = team_names[selection.to_i-1]
  write_credentials
  @credentials[2]
end

#credential(index) ⇒ Object



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

def credential index
  get_credentials
  @credentials && @credentials.size > index ? @credentials[index] : nil
end

#credentials_fileObject



38
39
40
# File 'lib/wod/commands/auth.rb', line 38

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

#delete_credentialsObject



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

def delete_credentials
  FileUtils.rm_rf "#{home_directory}/.wod/"
end

#echo_offObject



54
55
56
# File 'lib/wod/commands/auth.rb', line 54

def echo_off
  system "stty -echo"
end

#echo_onObject



58
59
60
# File 'lib/wod/commands/auth.rb', line 58

def echo_on
  system "stty echo"
end

#get_credentialsObject



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

def get_credentials
  return if @credentials
  unless @credentials = read_credentials
    ask_for_and_save_credentials
  end
  @credentials
end

#passwordObject



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

def password
  credential 1
end

#read_credentialsObject



50
51
52
# File 'lib/wod/commands/auth.rb', line 50

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

#reauthorizeObject



17
18
19
# File 'lib/wod/commands/auth.rb', line 17

def reauthorize
  @credentials = ask_for_and_save_credentials
end

#retry_login?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
# File 'lib/wod/commands/auth.rb', line 110

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

#set_credentials_permissionsObject



125
126
127
128
# File 'lib/wod/commands/auth.rb', line 125

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

#teamObject



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

def team
  credential 2
end

#userObject



21
22
23
# File 'lib/wod/commands/auth.rb', line 21

def user
  credential 0
end

#write_credentialsObject



116
117
118
119
120
121
122
123
# File 'lib/wod/commands/auth.rb', line 116

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