Class: Jiveapps::Command::Auth

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

Instance Attribute Summary collapse

Attributes inherited from Base

#args, #autodetected_app

Instance Method Summary collapse

Methods inherited from Base

#extract_app, #extract_app_from_git_config, #extract_app_in_dir, #extract_option, #git_remotes, #initialize, #jiveapps

Methods included from Helpers

#ask, #catch_args, #check_git_version, #confirm, #confirm_command, #debug, #debug_mode?, #display, #display_oauth_services, #error, #get_app_prop_with_default, #get_or_set_git_prop, #git_version, #has_program?, #home_directory, #run, #running_on_a_mac?, #running_on_windows?, #sh, #usage, #user_git_version

Constructor Details

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

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

Instance Method Details

#ask_for_credentialsObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jiveapps/commands/auth.rb', line 64

def ask_for_credentials
  puts "Enter your Jive Apps Developer Community credentials. (https://developers.jivesoftware.com)"

  print "Username: "
  user = ask

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

  [ user, password ]
end

#ask_for_passwordObject



94
95
96
97
98
99
100
# File 'lib/jiveapps/commands/auth.rb', line 94

def ask_for_password
  echo_off
  password = ask
  puts
  echo_on
  return password
end

#ask_for_password_on_windowsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jiveapps/commands/auth.rb', line 76

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

#checkObject

just a stub; will raise if not authenticated



16
17
18
# File 'lib/jiveapps/commands/auth.rb', line 16

def check
  client.list
end

#clientObject



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

def client
  @client ||= init_jiveapps
end

#credentials_fileObject



39
40
41
# File 'lib/jiveapps/commands/auth.rb', line 39

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

#delete_credentialsObject



140
141
142
# File 'lib/jiveapps/commands/auth.rb', line 140

def delete_credentials
  FileUtils.rm_f(credentials_file)
end

#echo_offObject



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

def echo_off
  system "stty -echo"
end

#echo_onObject



60
61
62
# File 'lib/jiveapps/commands/auth.rb', line 60

def echo_on
  system "stty echo"
end

#get_credentialsObject

:nodoc:



43
44
45
46
47
48
49
50
# File 'lib/jiveapps/commands/auth.rb', line 43

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

#hostObject



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

def host
  ENV['JIVEAPPS_HOST'] || Jiveapps::WEBHOST
end

#init_jiveappsObject



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

def init_jiveapps
  client = Jiveapps::Client.new(user, password, host)
  client.on_warning { |msg| self.display("\n#{msg}\n\n") }
  client
end

#passwordObject

:nodoc:



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

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

#read_credentialsObject



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

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

#reauthorizeObject



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

def reauthorize
  @credentials = ask_for_credentials
  write_credentials
end

#retry_login?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'lib/jiveapps/commands/auth.rb', line 121

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

#save_credentialsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/jiveapps/commands/auth.rb', line 102

def save_credentials
  begin
    write_credentials
    command = args.any? { |a| a == '--ignore-keys' } ? 'auth:check' : 'keys:add'
    Jiveapps::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_jiveapps
    retry
  rescue Exception => e
    delete_credentials
    raise e
  end
end

#set_credentials_permissionsObject



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

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

#userObject

:nodoc:



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

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

#write_credentialsObject



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

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