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



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

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



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

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



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

def force_user_change
  @user_changed = true
end

#password(ask = nil) ⇒ Object



87
88
89
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 87

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

#response(r) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 68

def response(r)
  if (r.cookies['_session_id'] && r.code != 401)
    session.id = r.cookies['_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



91
92
93
94
95
96
97
98
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 91

def set_auth_params(*args)
  if @auth_type == AUTH_TYPES[:basic_auth]
    @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
# 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
  else
    _('Using sessions, you are currently not logged in.')
  end
end

#uriObject



100
101
102
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 100

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

#user(ask = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb', line 77

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


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

def user_changed?
  !!@user_changed
end