Sha256: 5270eb43fd9f161079e81e72332ec3a4e142de60dc9c22900c2aefbb2edcf74c

Contents?: true

Size: 1.7 KB

Versions: 17

Compression:

Stored size: 1.7 KB

Contents

module Moped
  module Protocol
    module Commands

      # Implementation of the authentication command for Mongo. See:
      # http://www.mongodb.org/display/DOCS/Implementing+Authentication+in+a+Driver
      # for details.
      #
      # @example
      #   socket.write Command.new :admin, getnonce: 1
      #   reply = Reply.deserialize socket
      #   socket.write Authenticate.new :admin, "username", "password",
      #     reply.documents[0]["nonce"]
      #   Reply.deserialize(socket).documents[0]["ok"] # => 1.0
      class Authenticate < Command

        # Create a new authentication command.
        #
        # @param [String] database the database to authenticate against
        # @param [String] username
        # @param [String] password
        # @param [String] nonce the nonce returned from running the getnonce
        # command.
        def initialize(database, username, password, nonce)
          super(database, build_auth_command(username, password, nonce))
        end

        # @param [String] username
        # @param [String] password
        # @param [String] nonce
        # @return [String] the mongo digest of the username, password, and
        # nonce.
        def digest(username, password, nonce)
          Digest::MD5.hexdigest(
            nonce + username + Digest::MD5.hexdigest(username + ":mongo:" + password)
          )
        end

        # @param [String] username
        # @param [String] password
        # @param [String] nonce
        def build_auth_command(username, password, nonce)
          {
            authenticate: 1,
            user: username,
            nonce: nonce,
            key: digest(username, password, nonce)
          }
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/bundler/gems/moped-cf817ca58a85/lib/moped/protocol/commands/authenticate.rb
moped-2.0.7 lib/moped/protocol/commands/authenticate.rb
moped-2.0.6 lib/moped/protocol/commands/authenticate.rb
moped-2.0.5 lib/moped/protocol/commands/authenticate.rb
moped-2.0.4 lib/moped/protocol/commands/authenticate.rb
moped-2.0.3 lib/moped/protocol/commands/authenticate.rb
moped-2.0.2 lib/moped/protocol/commands/authenticate.rb
moped-2.0.1 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0.rc2 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0.rc1 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0.beta6 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0.beta5 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0.beta4 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0.beta3 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0.beta2 lib/moped/protocol/commands/authenticate.rb
moped-2.0.0.beta lib/moped/protocol/commands/authenticate.rb