Module: HammerCLIForeman::Api::BasicAuth

Included in:
InteractiveBasicAuth, InteractiveBasicAuthExternal
Defined in:
lib/hammer_cli_foreman/api/basic_auth.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(request, args) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/hammer_cli_foreman/api/basic_auth.rb', line 4

def authenticate(request, args)
  if HammerCLI.interactive?
    get_user
    get_password
  end
  super
end

#clearObject



48
49
50
# File 'lib/hammer_cli_foreman/api/basic_auth.rb', line 48

def clear
  set_credentials(nil, nil)
end

#error(ex) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hammer_cli_foreman/api/basic_auth.rb', line 12

def error(ex)
  return unless ex.is_a?(RestClient::Unauthorized)

  clear
  default_message = _('Invalid username or password.')
  message = begin
    response_msg = JSON.parse(ex.response.body)['error']
    response_msg.is_a?(Hash) ? response_msg['message'] : response_msg
  rescue
  end
  return UnauthorizedError.new(default_message) unless message

  UnauthorizedError.new("#{message}\n#{default_message}")
end

#password(ask = nil) ⇒ Object



39
40
41
# File 'lib/hammer_cli_foreman/api/basic_auth.rb', line 39

def password(ask = nil)
  @password ||= ask && get_password
end

#set_credentials(user, password) ⇒ Object



43
44
45
46
# File 'lib/hammer_cli_foreman/api/basic_auth.rb', line 43

def set_credentials(user, password)
  @user = user
  @password = password
end

#statusObject



27
28
29
30
31
32
33
# File 'lib/hammer_cli_foreman/api/basic_auth.rb', line 27

def status
  unless @user.nil? || @password.nil?
    _("Using configured credentials for user '%s'.") % @user
  else
    _('Credentials are not configured.')
  end
end

#user(ask = nil) ⇒ Object



35
36
37
# File 'lib/hammer_cli_foreman/api/basic_auth.rb', line 35

def user(ask = nil)
  @user ||= ask && get_user
end