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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SessionAuthenticatorWrapper.



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

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 Attribute Details

#session_idObject (readonly)

Returns the value of attribute session_id.



8
9
10
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 8

def session_id
  @session_id
end

Instance Method Details

#authenticate(request, args) ⇒ Object



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

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



21
22
23
24
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 21

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

#error(ex) ⇒ Object



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

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



34
35
36
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 34

def force_user_change
  @user_changed = true
end

#password(ask = nil) ⇒ Object



85
86
87
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 85

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

#response(r) ⇒ Object



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

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

#set_credentials(*args) ⇒ Object



89
90
91
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 89

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

#statusObject



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

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



81
82
83
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 81

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

#user_changed?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 38

def user_changed?
  !!@user_changed
end