class HammerCLIForeman::Api::SessionAuthenticatorWrapper

Attributes

auth_type[R]
session_id[R]
url[R]

Public Class Methods

new(authenticator, url, auth_type) click to toggle source
# 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

Public Instance Methods

authenticate(request, args) click to toggle source
# 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
clear() click to toggle source
# 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) click to toggle source
# 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_change() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 34
def force_user_change
  @user_changed = true
end
password(ask=nil) click to toggle source
# 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) click to toggle source
# 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
session() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 15
def session
  @session ||= Sessions.get(@url)
end
set_auth_params(*args) click to toggle source
# 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
status() click to toggle source
# 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
uri() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 104
def uri
  @uri ||= URI.parse(@url)
end
user(ask=nil) click to toggle source
# 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?() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 38
def user_changed?
  !!@user_changed
end