raise exception on authentication failure

This commit is contained in:
Kyle Banker 2010-02-24 13:05:45 -05:00
parent 60832b7895
commit 3479317098
4 changed files with 18 additions and 6 deletions

View File

@ -665,7 +665,7 @@ module Mongo
user = auth[0]
pwd = auth[1]
db_name = auth[2]
self.db(db_name).authenticate(user, pwd) || raise(MongoDBError, "Failed to authenticate db #{db_name}.")
self.db(db_name).authenticate(user, pwd)
end
end
end

View File

@ -79,6 +79,8 @@ module Mongo
#
# @return [Boolean]
#
# @raise [AuthenticationError]
#
# @core authenticate authenticate-instance_method
def authenticate(username, password)
doc = command(:getnonce => 1)
@ -90,7 +92,8 @@ module Mongo
auth['user'] = username
auth['nonce'] = nonce
auth['key'] = Digest::MD5.hexdigest("#{nonce}#{username}#{hash_password(username, password)}")
ok?(command(auth))
ok?(command(auth)) ||
raise(MongoDBError::AuthenticationError, "Failed to authenticate user '#{username}' on db '#{self.name}'")
end
# Adds a user to this database for use with authentication. If the user already

View File

@ -46,6 +46,9 @@ module Mongo
# when the document contains objects that can't be serialized as BSON.
class InvalidDocument < MongoDBError; end
# Raised when authentication fails.
class AuthenticationError < MongoDBError; end
# Raised when a database operation fails.
class OperationFailure < MongoDBError; end

View File

@ -128,8 +128,12 @@ class DBTest < Test::Unit::TestCase
def test_authenticate
@@db.add_user('spongebob', 'squarepants')
assert !@@db.authenticate('nobody', 'nopassword')
assert !@@db.authenticate('spongebob' , 'squareliederhosen')
assert_raise Mongo::AuthenticationError do
assert !@@db.authenticate('nobody', 'nopassword')
end
assert_raise Mongo::AuthenticationError do
assert !@@db.authenticate('spongebob' , 'squareliederhosen')
end
assert @@db.authenticate('spongebob', 'squarepants')
@@db.logout
@@db.remove_user('spongebob')
@ -139,7 +143,7 @@ class DBTest < Test::Unit::TestCase
@@db.add_user('spongebob', 'squarepants')
assert Mongo::Connection.from_uri("mongodb://spongebob:squarepants@localhost/#{@@db.name}")
assert_raise MongoDBError do
assert_raise Mongo::AuthenticationError do
Mongo::Connection.from_uri("mongodb://wrong:info@localhost/#{@@db.name}")
end
end
@ -207,7 +211,9 @@ class DBTest < Test::Unit::TestCase
assert @@db.authenticate("bob", "secret")
@@db.logout
assert @@db.remove_user("bob")
assert !@@db.authenticate("bob", "secret")
assert_raise Mongo::AuthenticationError do
@@db.authenticate("bob", "secret")
end
end
def test_remove_non_existant_user