class Moped::Protocol::Commands::Authenticate

Implementation of the authentication command for Mongo. See: 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

Public Class Methods

new(database, username, password, nonce) click to toggle source

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.

Calls superclass method Moped::Protocol::Command.new
# File lib/moped/protocol/commands/authenticate.rb, line 24
def initialize(database, username, password, nonce)
  super database, build_auth_command(username, password, nonce)
end

Public Instance Methods

build_auth_command(username, password, nonce) click to toggle source

@param [String] username @param [String] password @param [String] nonce

# File lib/moped/protocol/commands/authenticate.rb, line 42
def build_auth_command(username, password, nonce)
  {
    authenticate: 1,
    user: username,
    nonce: nonce,
    key: digest(username, password, nonce)
  }
end
digest(username, password, nonce) click to toggle source

@param [String] username @param [String] password @param [String] nonce @return [String] the mongo digest of the username, password, and nonce.

# File lib/moped/protocol/commands/authenticate.rb, line 33
def digest(username, password, nonce)
  Digest::MD5.hexdigest(
    nonce + username + Digest::MD5.hexdigest(username + ":mongo:" + password)
  )
end