class HammerCLIForeman::Api::SessionAuthenticatorWrapper

Constants

SESSION_STORAGE

Public Class Methods

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

Public Instance Methods

authenticate(request, args) click to toggle source
# 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
clear() click to toggle source
# 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) click to toggle source
# 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_change() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 32
def force_user_change
  @user_changed = true
end
response(r) click to toggle source
# 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) click to toggle source
# 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
status() click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 36
def user_changed?
  !!@user_changed
end

Protected Instance Methods

check_storage_permissions() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 124
def check_storage_permissions
  Dir.mkdir(@storage_dir, 0700) unless File.exist?(@storage_dir)
  ensure_mode(@storage_dir, '40700') && ensure_mode(session_storage, '100600')
end
destroy_session() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 119
def destroy_session
  @user = @session_id = nil
  File.delete(session_storage) if File.exist?(session_storage)
end
ensure_mode(file, expected_mode) click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 129
def ensure_mode(file, expected_mode)
  return true unless File.exist?(file)
  mode = File.stat(file).mode.to_s(8)
  if mode != expected_mode
    warn _("Invalid permissions for %{file}: %{mode}, expected %{expected_mode}") % {
      :mode => mode,
      :expected_mode => expected_mode,
      :file => file
    }
    false
  else
    true
  end
end
load_session() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 97
def load_session
  if File.exist?(session_storage)
    session_data = JSON.parse(File.read(session_storage))
    @user = session_data['user_name']
    @session_id = session_data['session_id']
  end
rescue JSON::ParserError
  destroy_session
  warn _('Invalid session file format')
  nil
end
save_session(session_id, user_name) click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 109
def save_session(session_id, user_name)
  File.open(session_storage, 'w', 0600) do |f|
    session = JSON.generate({
      :session_id => session_id,
      :user_name => user_name
    })
    f.write(session)
  end
end
session_storage() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 93
def session_storage
  "#{@storage_dir}/#{@session_file}"
end
uri() click to toggle source
# File lib/hammer_cli_foreman/api/session_authenticator_wrapper.rb, line 89
def uri
  @uri ||= URI.parse(@url)
end