def mod_callback(result): nonce = result["nonce"] key = helpers._auth_key(nonce, name, password) self.command("authenticate",callback = mod_callback2, user=unicode(name), nonce=nonce, key=key)
def copy_database(self, from_name, to_name, from_host=None, username=None, password=None): """Copy a database, potentially from another host. Raises :class:`TypeError` if `from_name` or `to_name` is not an instance of :class:`basestring` (:class:`str` in python 3). Raises :class:`~pymongo.errors.InvalidName` if `to_name` is not a valid database name. If `from_host` is ``None`` the current host is used as the source. Otherwise the database is copied from `from_host`. If the source database requires authentication, `username` and `password` must be specified. :Parameters: - `from_name`: the name of the source database - `to_name`: the name of the target database - `from_host` (optional): host name to copy from - `username` (optional): username for source database - `password` (optional): password for source database .. note:: Specifying `username` and `password` requires server version **>= 1.3.3+**. .. versionadded:: 1.5 """ if not isinstance(from_name, basestring): raise TypeError("from_name must be an instance " "of %s" % (basestring.__name__,)) if not isinstance(to_name, basestring): raise TypeError("to_name must be an instance " "of %s" % (basestring.__name__,)) database._check_name(to_name) command = {"fromdb": from_name, "todb": to_name} if from_host is not None: command["fromhost"] = from_host in_request = self.in_request() try: if not in_request: self.start_request() if username is not None: nonce = self.admin.command("copydbgetnonce", fromhost=from_host)["nonce"] command["username"] = username command["nonce"] = nonce command["key"] = helpers._auth_key(nonce, username, password) return self.admin.command("copydb", **command) finally: if not in_request: self.end_request()
def mod_callback(result): nonce = result["nonce"] key = helpers._auth_key(nonce, name, password) self.command("authenticate", callback=mod_callback2, user=unicode(name), nonce=nonce, key=key)
def __auth(self, sock_info, dbname, user, passwd): """Authenticate socket against database `dbname`. """ # Get a nonce response = self.__simple_command(sock_info, dbname, {'getnonce': 1}) nonce = response['nonce'] key = helpers._auth_key(nonce, user, passwd) # Actually authenticate query = SON([('authenticate', 1), ('user', user), ('nonce', nonce), ('key', key)]) self.__simple_command(sock_info, dbname, query)