From 34793170982543fa32fb42f54b92d73b199c343b Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Wed, 24 Feb 2010 13:05:45 -0500 Subject: [PATCH] raise exception on authentication failure --- lib/mongo/connection.rb | 2 +- lib/mongo/db.rb | 5 ++++- lib/mongo/exceptions.rb | 3 +++ test/db_test.rb | 14 ++++++++++---- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/mongo/connection.rb b/lib/mongo/connection.rb index ece5c41..346fdcc 100644 --- a/lib/mongo/connection.rb +++ b/lib/mongo/connection.rb @@ -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 diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index cf0417c..b79a096 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -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 diff --git a/lib/mongo/exceptions.rb b/lib/mongo/exceptions.rb index 8d39148..a7990ce 100644 --- a/lib/mongo/exceptions.rb +++ b/lib/mongo/exceptions.rb @@ -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 diff --git a/test/db_test.rb b/test/db_test.rb index 3cbd9e6..9b08086 100644 --- a/test/db_test.rb +++ b/test/db_test.rb @@ -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