Class: EM::Mongo::MONGODB_CR

Inherits:
Authentication show all
Defined in:
lib/em-mongo/auth/mongodb_cr.rb

Constant Summary collapse

MECHANISM =
'MONGODB-CR'.freeze

Constants inherited from Authentication

Authentication::SYSTEM_COMMAND_COLLECTION

Instance Method Summary collapse

Methods inherited from Authentication

#initialize

Constructor Details

This class inherits a constructor from EM::Mongo::Authentication

Instance Method Details

#auth_key(username, password, nonce) ⇒ String

Generate an MD5 for authentication.

Parameters:

Returns:

  • (String)

    a key for db authentication.



47
48
49
# File 'lib/em-mongo/auth/mongodb_cr.rb', line 47

def auth_key(username, password, nonce)
  OpenSSL::Digest::MD5.hexdigest("#{nonce}#{username}#{Support.hash_password(username, password)}")
end

#authenticate(username, password) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/em-mongo/auth/mongodb_cr.rb', line 9

def authenticate(username, password)
  response = RequestResponse.new

  auth_resp = @db.collection(SYSTEM_COMMAND_COLLECTION).first({'getnonce' => 1})
  auth_resp.callback do |res|
    if not res or not res['nonce']
      if res.nil? then response.fail "connection failure"
      else response.fail "invalid first server response: " + res.to_s
      end
    else
      auth                 = BSON::OrderedHash.new
      auth['authenticate'] = 1
      auth['user']         = username
      auth['nonce']        = res['nonce']
      auth['key']          = auth_key(username, password, res['nonce'])

      auth_resp2 = @db.collection(SYSTEM_COMMAND_COLLECTION).first(auth)
      auth_resp2.callback do |res|
        if Support.ok?(res)
          response.succeed true
        else
          response.fail res
        end
      end
      auth_resp2.errback { |err| response.fail err }
    end
  end
  auth_resp.errback { |err| response.fail err }
  response
end