1414
1515"""Database level operations."""
1616
17- import warnings
18-
1917from bson .binary import OLD_UUID_SUBTYPE
2018from bson .code import Code
2119from bson .dbref import DBRef
2220from bson .son import SON
23- from pymongo import common , helpers
21+ from pymongo import auth , common , helpers
2422from pymongo .collection import Collection
2523from pymongo .errors import (CollectionInvalid ,
2624 InvalidName ,
@@ -471,7 +469,7 @@ def validate_collection(self, name_or_collection,
471469 raise CollectionInvalid ("%s invalid: %s" % (name , info ))
472470 # Sharded results
473471 elif "raw" in result :
474- for repl , res in result ["raw" ].iteritems ():
472+ for _ , res in result ["raw" ].iteritems ():
475473 if "result" in res :
476474 info = res ["result" ]
477475 if (info .find ("exception" ) != - 1 or
@@ -627,7 +625,7 @@ def add_user(self, name, password, read_only=False):
627625 """
628626
629627 user = self .system .users .find_one ({"user" : name }) or {"user" : name }
630- user ["pwd" ] = helpers ._password_digest (name , password )
628+ user ["pwd" ] = auth ._password_digest (name , password )
631629 user ["readOnly" ] = common .validate_boolean ('read_only' , read_only )
632630
633631 try :
@@ -656,12 +654,10 @@ def remove_user(self, name):
656654 def authenticate (self , name , password ):
657655 """Authenticate to use this database.
658656
659- Once authenticated, the user has full read and write access to
660- this database. Raises :class:`TypeError` if either `name` or
661- `password` is not an instance of :class:`basestring`
662- (:class:`str` in python 3). Authentication lasts for the life
663- of the underlying :class:`~pymongo.connection.Connection`, or
664- until :meth:`logout` is called.
657+ Raises :class:`TypeError` if either `name` or `password` is not
658+ an instance of :class:`basestring` (:class:`str` in python 3).
659+ Authentication lasts for the life of the underlying client
660+ instance, or until :meth:`logout` is called.
665661
666662 The "admin" database is special. Authenticating on "admin"
667663 gives access to *all* databases. Effectively, "admin" access
@@ -670,28 +666,20 @@ def authenticate(self, name, password):
670666 .. note::
671667 This method authenticates the current connection, and
672668 will also cause all new :class:`~socket.socket` connections
673- in the underlying :class:`~pymongo.connection.Connection` to
674- be authenticated automatically.
675-
676- - When sharing a :class:`~pymongo.connection.Connection`
677- between multiple threads, all threads will share the
678- authentication. If you need different authentication profiles
679- for different purposes (e.g. admin users) you must use
680- distinct instances of :class:`~pymongo.connection.Connection`.
669+ in the underlying client instance to be authenticated automatically.
681670
682- - To get authentication to apply immediately to all
683- existing sockets you may need to reset this Connection's
684- sockets using :meth:`~pymongo.connection.Connection.disconnect` .
671+ - Authenticating more than once on the same database with different
672+ credentials is not supported. You must call :meth:`logout` before
673+ authenticating with new credentials .
685674
686- .. warning::
675+ - When sharing a client instance between multiple threads, all
676+ threads will share the authentication. If you need different
677+ authentication profiles for different purposes you must use
678+ distinct client instances.
687679
688- Currently, calls to
689- :meth:`~pymongo.connection.Connection.end_request` will
690- lead to unpredictable behavior in combination with
691- auth. The :class:`~socket.socket` owned by the calling
692- thread will be returned to the pool, so whichever thread
693- uses that :class:`~socket.socket` next will have whatever
694- permissions were granted to the calling thread.
680+ - To get authentication to apply immediately to all
681+ existing sockets you may need to reset this client instance's
682+ sockets using :meth:`~pymongo.mongo_client.MongoClient.disconnect`.
695683
696684 :Parameters:
697685 - `name`: the name of the user to authenticate
@@ -706,42 +694,22 @@ def authenticate(self, name, password):
706694 raise TypeError ("password must be an instance "
707695 "of %s" % (basestring .__name__ ,))
708696
709- # So we can authenticate during a failover. The start_request()
710- # call below will pin the host used for getnonce so we use the
711- # same host for authenticate.
712- read_pref = rp .ReadPreference .PRIMARY_PREFERRED
713-
714- in_request = self .connection .in_request ()
715697 try :
716- if not in_request :
717- self .connection .start_request ()
718-
719- nonce = self .command ("getnonce" ,
720- read_preference = read_pref )["nonce" ]
721- key = helpers ._auth_key (nonce , name , password )
722- try :
723- self .command ("authenticate" , user = unicode (name ),
724- nonce = nonce , key = key , read_preference = read_pref )
725- self .connection ._cache_credentials (self .name ,
726- unicode (name ),
727- unicode (password ))
728- return True
729- except OperationFailure :
730- return False
731- finally :
732- if not in_request :
733- self .connection .end_request ()
698+ credentials = (self .name , unicode (name ), unicode (password ))
699+ self .connection ._cache_credentials (self .name , credentials )
700+ return True
701+ except OperationFailure :
702+ return False
734703
735704 def logout (self ):
736- """Deauthorize use of this database for this connection
737- and future connections.
705+ """Deauthorize use of this database for this client instance.
738706
739707 .. note:: Other databases may still be authenticated, and other
740708 existing :class:`~socket.socket` connections may remain
741709 authenticated for this database unless you reset all sockets
742- with :meth:`~pymongo.connection.Connection .disconnect`.
710+ with :meth:`~pymongo.mongo_client.MongoClient .disconnect`.
743711 """
744- self . command ( "logout" )
712+ # Sockets will be deauthenticated as they are used.
745713 self .connection ._purge_credentials (self .name )
746714
747715 def dereference (self , dbref ):
0 commit comments