def login(database, username, password)
getnonce = Protocol::Command.new(database, getnonce: 1)
self.write([getnonce])
reply = self.receive_replies([getnonce]).first
if getnonce.failure?(reply)
return
end
result = getnonce.results(reply)
authenticate = Protocol::Commands::Authenticate.new(database, username, password, result["nonce"])
self.write([ authenticate ])
document = self.read.documents.first
unless result["ok"] == 1
e = Errors::PotentialReconfiguration.new(authenticate, document)
if e.reconfiguring_replica_set?
raise Errors::ReplicaSetReconfigured.new(e.command, e.details)
elsif e.connection_failure?
raise Errors::ConnectionFailure.new(e.inspect)
end
raise Errors::AuthenticationFailure.new(authenticate, document)
end
credentials[database] = [username, password]
end