raise exception on authentication failure
This commit is contained in:
parent
60832b7895
commit
3479317098
|
@ -665,7 +665,7 @@ module Mongo
|
||||||
user = auth[0]
|
user = auth[0]
|
||||||
pwd = auth[1]
|
pwd = auth[1]
|
||||||
db_name = auth[2]
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -79,6 +79,8 @@ module Mongo
|
||||||
#
|
#
|
||||||
# @return [Boolean]
|
# @return [Boolean]
|
||||||
#
|
#
|
||||||
|
# @raise [AuthenticationError]
|
||||||
|
#
|
||||||
# @core authenticate authenticate-instance_method
|
# @core authenticate authenticate-instance_method
|
||||||
def authenticate(username, password)
|
def authenticate(username, password)
|
||||||
doc = command(:getnonce => 1)
|
doc = command(:getnonce => 1)
|
||||||
|
@ -90,7 +92,8 @@ module Mongo
|
||||||
auth['user'] = username
|
auth['user'] = username
|
||||||
auth['nonce'] = nonce
|
auth['nonce'] = nonce
|
||||||
auth['key'] = Digest::MD5.hexdigest("#{nonce}#{username}#{hash_password(username, password)}")
|
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
|
end
|
||||||
|
|
||||||
# Adds a user to this database for use with authentication. If the user already
|
# Adds a user to this database for use with authentication. If the user already
|
||||||
|
|
|
@ -46,6 +46,9 @@ module Mongo
|
||||||
# when the document contains objects that can't be serialized as BSON.
|
# when the document contains objects that can't be serialized as BSON.
|
||||||
class InvalidDocument < MongoDBError; end
|
class InvalidDocument < MongoDBError; end
|
||||||
|
|
||||||
|
# Raised when authentication fails.
|
||||||
|
class AuthenticationError < MongoDBError; end
|
||||||
|
|
||||||
# Raised when a database operation fails.
|
# Raised when a database operation fails.
|
||||||
class OperationFailure < MongoDBError; end
|
class OperationFailure < MongoDBError; end
|
||||||
|
|
||||||
|
|
|
@ -128,8 +128,12 @@ class DBTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_authenticate
|
def test_authenticate
|
||||||
@@db.add_user('spongebob', 'squarepants')
|
@@db.add_user('spongebob', 'squarepants')
|
||||||
assert !@@db.authenticate('nobody', 'nopassword')
|
assert_raise Mongo::AuthenticationError do
|
||||||
assert !@@db.authenticate('spongebob' , 'squareliederhosen')
|
assert !@@db.authenticate('nobody', 'nopassword')
|
||||||
|
end
|
||||||
|
assert_raise Mongo::AuthenticationError do
|
||||||
|
assert !@@db.authenticate('spongebob' , 'squareliederhosen')
|
||||||
|
end
|
||||||
assert @@db.authenticate('spongebob', 'squarepants')
|
assert @@db.authenticate('spongebob', 'squarepants')
|
||||||
@@db.logout
|
@@db.logout
|
||||||
@@db.remove_user('spongebob')
|
@@db.remove_user('spongebob')
|
||||||
|
@ -139,7 +143,7 @@ class DBTest < Test::Unit::TestCase
|
||||||
@@db.add_user('spongebob', 'squarepants')
|
@@db.add_user('spongebob', 'squarepants')
|
||||||
assert Mongo::Connection.from_uri("mongodb://spongebob:squarepants@localhost/#{@@db.name}")
|
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}")
|
Mongo::Connection.from_uri("mongodb://wrong:info@localhost/#{@@db.name}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -207,7 +211,9 @@ class DBTest < Test::Unit::TestCase
|
||||||
assert @@db.authenticate("bob", "secret")
|
assert @@db.authenticate("bob", "secret")
|
||||||
@@db.logout
|
@@db.logout
|
||||||
assert @@db.remove_user("bob")
|
assert @@db.remove_user("bob")
|
||||||
assert !@@db.authenticate("bob", "secret")
|
assert_raise Mongo::AuthenticationError do
|
||||||
|
@@db.authenticate("bob", "secret")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_remove_non_existant_user
|
def test_remove_non_existant_user
|
||||||
|
|
Loading…
Reference in New Issue