Class: HammerCLIForeman::Api::SessionAuthenticatorWrapper

Inherits:
ApipieBindings::Authenticators::Base
  • Object
show all
Defined in:
lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb

Constant Summary collapse

SESSION_STORAGE =
'~/.hammer/sessions/'

Instance Method Summary collapse

Constructor Details

#initialize(authenticator, url, storage_dir = nil) ⇒ SessionAuthenticatorWrapper

Returns a new instance of SessionAuthenticatorWrapper.



8
9
10
11
12
13
14
15
16
17
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 8

def initialize(authenticator, url, storage_dir = nil)
  @authenticator = authenticator
  @url = url

  @session_file = "#{uri.scheme}_#{uri.host}"
  @storage_dir = storage_dir || File.expand_path(SESSION_STORAGE)

  @permissions_ok = check_storage_permissions
  warn _("Can't use session auth due to invalid permissions on session files.") unless @permissions_ok
end

Instance Method Details

#authenticate(request, args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 40

def authenticate(request, args)
  load_session

  user = @authenticator.user

  @user_changed ||= (!user.nil? && user != @user)

  if !@user_changed && @permissions_ok && @session_id
    jar = HTTP::CookieJar.new
    jar.add(HTTP::Cookie.new('_session_id', @session_id, domain: uri.hostname.downcase, path: '/', for_domain: true))
    request['Cookie'] = HTTP::Cookie.cookie_value(jar.cookies)
    request
  else
    @authenticator.authenticate(request, args)
  end
end

#clearObject



19
20
21
22
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 19

def clear
  destroy_session
  @authenticator.clear if @authenticator.respond_to?(:clear)
end

#error(ex) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 57

def error(ex)
  load_session
  if ex.is_a?(RestClient::Unauthorized) && !@session_id.nil?
    if @user_changed
      return UnauthorizedError.new(_("Invalid username or password, continuing with session for '%s'") % @user)
    else
      destroy_session
      return SessionExpired.new(_("Session has expired"))
    end
  else
    return @authenticator.error(ex)
  end
end

#force_user_changeObject



32
33
34
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 32

def force_user_change
  @user_changed = true
end

#response(r) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 71

def response(r)
  @session_id = r.cookies['_session_id']
  if (@session_id && r.code != 401)
    save_session(@session_id, @authenticator.user)
  end
  @authenticator.response(r)
end

#set_credentials(*args) ⇒ Object



83
84
85
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 83

def set_credentials(*args)
  @authenticator.set_credentials(*args) if @authenticator.respond_to?(:set_credentials)
end

#statusObject



24
25
26
27
28
29
30
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 24

def status
  if load_session
    _("Session exists, currently logged in as '%s'") % @user
  else
    _("Using sessions, you are currently not logged in.")
  end
end

#user(ask = nil) ⇒ Object



79
80
81
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 79

def user(ask=nil)
  @authenticator.user(ask) if @authenticator.respond_to?(:user)
end

#user_changed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 36

def user_changed?
  !!@user_changed
end