Class: HammerCLIForeman::Api::SessionAuthenticatorWrapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authenticator, url, auth_type) ⇒ SessionAuthenticatorWrapper

Returns a new instance of SessionAuthenticatorWrapper.



9
10
11
12
13
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 9

def initialize(authenticator, url, auth_type)
  @authenticator = authenticator
  @url = url
  @auth_type = auth_type
end

Instance Attribute Details

#auth_typeObject (readonly)

Returns the value of attribute auth_type.



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

def auth_type
  @auth_type
end

#session_idObject (readonly)

Returns the value of attribute session_id.



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

def session_id
  @session_id
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#authenticate(request, args) ⇒ Object



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 42

def authenticate(request, args)
  user = @authenticator.user

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

  if !@user_changed && Sessions.configured?(@url) && 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
  session.destroy
  @authenticator.clear if @authenticator.respond_to?(:clear)
end

#error(ex) ⇒ Object



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

def error(ex)
  if ex.is_a?(RestClient::Unauthorized) && session.valid?
    if @user_changed
      return UnauthorizedError.new(_("Invalid credentials, continuing with session for '%s'.") % session.user_name)
    else
      session.destroy
      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



91
92
93
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 91

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

#response(r) ⇒ Object



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

def response(r)
  session_id = @authenticator.session_id if @authenticator.respond_to?(:session_id)
  session_id ||= r.cookies['_session_id']
  if session_id && r.code != 401
    session.id = session_id
    session.user_name = @authenticator.user
    session.store
  end
  @authenticator.response(r)
end

#sessionObject



15
16
17
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 15

def session
  @session ||= Sessions.get(@url)
end

#set_auth_params(*args) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 95

def set_auth_params(*args)
  if [AUTH_TYPES[:basic_auth], AUTH_TYPES[:basic_auth_external]].include?(@auth_type)
    @authenticator.set_credentials(*args)
  elsif @auth_type == AUTH_TYPES[:oauth_authentication_code_grant] ||
        @auth_type == AUTH_TYPES[:oauth_password_grant]
    @authenticator.set_token(*args)
  end
end

#statusObject



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

def status
  if session.valid?
    _("Session exists, currently logged in as '%s'.") % session.user_name
  elsif @authenticator.respond_to?(:status)
    @authenticator.status
  else
    _('Using sessions, you are currently not logged in.')
  end
end

#uriObject



104
105
106
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 104

def uri
  @uri ||= URI.parse(@url)
end

#user(ask = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 81

def user(ask=nil)
  return unless @authenticator.respond_to?(:user)
  if [AUTH_TYPES[:basic_auth], AUTH_TYPES[:basic_auth_external]].include?(@auth_type)
    @authenticator.user(ask)
  elsif @auth_type == AUTH_TYPES[:oauth_authentication_code_grant] ||
        @auth_type = AUTH_TYPES[:oauth_password_grant]
    @authenticator.user
  end
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