# File lib/net/ssh/userauth/driver.rb, line 140
        def authenticate( next_service, username, password=nil )
          msg = @buffers.writer
          msg.write_byte SERVICE_REQUEST
          msg.write_string "ssh-userauth"
          send_message msg

          message = wait_for_message
          unless message.message_type == SERVICE_ACCEPT
            raise Net::SSH::Exception,
              "expected SERVICE_ACCEPT, got #{message.inspect}"
          end

          data = { :password => password,
                   :key_manager => @key_manager }

          @order.each do |auth_method|
            # if the server has reported a list of auth methods that are
            # allowed to continue, only consider those auth methods.
            next if @allowed_auth_methods &&
              !@allowed_auth_methods.include?( auth_method )

            @log.debug "trying #{auth_method.inspect}" if @log.debug?

            impl = @methods[ auth_method.downcase.gsub(/-/,"_").intern ]
            if impl.nil?
              raise NotImplementedError,
                "`#{auth_method}' authentication is not implemented"
            end

            return true if impl.authenticate( next_service, username, data )
          end

          @log.debug "all authorization methods failed" if @log.debug?
          return false

        ensure
          @key_manager.finish
        end