Class: Moped::Protocol::Commands::Authenticate

Inherits:
Moped::Protocol::Command show all
Defined in:
lib/moped/protocol/commands/authenticate.rb

Overview

Implementation of the authentication command for Mongo. See: www.mongodb.org/display/DOCS/Implementing+Authentication+in+a+Driver for details.

Examples:

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

Instance Attribute Summary

Attributes inherited from Query

#collection, #database, #fields, #flags, #full_collection_name, #length, #limit, #op_code, #request_id, #selector, #skip

Instance Method Summary collapse

Methods inherited from Moped::Protocol::Command

#log_inspect

Methods inherited from Query

#log_inspect, #receive_replies

Methods included from Message

included, #inspect, #receive_replies, #serialize

Constructor Details

#initialize(database, username, password, nonce) ⇒ Authenticate

Create a new authentication command.

command.

Parameters:

  • database (String)

    the database to authenticate against

  • username (String)
  • password (String)
  • nonce (String)

    the nonce returned from running the getnonce



24
25
26
# File 'lib/moped/protocol/commands/authenticate.rb', line 24

def initialize(database, username, password, nonce)
  super database, build_auth_command(username, password, nonce)
end

Instance Method Details

#build_auth_command(username, password, nonce) ⇒ Object

Parameters:



42
43
44
45
46
47
48
49
# 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) ⇒ String

nonce.

Parameters:

Returns:

  • (String)

    the mongo digest of the username, password, and



33
34
35
36
37
# 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