class Net::SSH::Authentication::Methods::Hostbased

Implements the host-based SSH authentication method.

Public Instance Methods

authenticate(next_service, username, password = nil) click to toggle source

Attempts to perform host-based authorization of the user by trying all known keys.

# File lib/net/ssh/authentication/methods/hostbased.rb, line 13
def authenticate(next_service, username, password = nil)
  return false unless key_manager

  key_manager.each_identity do |identity|
    return true if authenticate_with(identity, next_service,
                                     username, key_manager)
  end

  return false
end

Private Instance Methods

authenticate_with(identity, next_service, username, key_manager) click to toggle source

Attempts to perform host-based authentication of the user, using the given host identity (key).

# File lib/net/ssh/authentication/methods/hostbased.rb, line 33
def authenticate_with(identity, next_service, username, key_manager)
  debug { "trying hostbased (#{identity.fingerprint})" }
  client_username = ENV['USER'] || username

  req = build_request(identity, next_service, username, "#{hostname}.", client_username)
  sig_data = Buffer.from(:string, session_id, :raw, req)

  sig = key_manager.sign(identity, sig_data.to_s)

  message = Buffer.from(:raw, req, :string, sig)

  send_message(message)
  message = session.next_message

  case message.type
  when USERAUTH_SUCCESS
    info { "hostbased succeeded (#{identity.fingerprint})" }
    return true
  when USERAUTH_FAILURE
    info { "hostbased failed (#{identity.fingerprint})" }

    raise Net::SSH::Authentication::DisallowedMethod unless
      message[:authentications].split(/,/).include? 'hostbased'

    return false
  else
    raise Net::SSH::Exception, "unexpected server response to USERAUTH_REQUEST: #{message.type} (#{message.inspect})"
  end
end
build_request(identity, next_service, username, hostname, client_username) click to toggle source

Build the “core” hostbased request string.

# File lib/net/ssh/authentication/methods/hostbased.rb, line 64
def build_request(identity, next_service, username, hostname, client_username)
  userauth_request(username, next_service, "hostbased", identity.ssh_type,
                   Buffer.from(:key, identity).to_s, hostname, client_username).to_s
end
hostname() click to toggle source

Returns the hostname as reported by the underlying socket.

# File lib/net/ssh/authentication/methods/hostbased.rb, line 27
def hostname
  session.transport.socket.client_name
end