<commit_before>Authentication Examples
=======================

MongoDB supports several different authentication mechanisms. These examples
cover all authentication methods currently supported by PyMongo, documenting
Python module and MongoDB version dependencies.

SCRAM-SHA-1 (RFC 5802)
----------------------
.. versionadded:: 2.8

SCRAM-SHA-1 is the default authentication mechanism supported by a cluster
configured for authentication with MongoDB 2.8 or later. Authentication is
per-database and credentials can be specified through the MongoDB URI or
passed to the :meth:`~pymongo.database.Database.authenticate` method::

  >>> from pymongo import MongoClient
  >>> client = MongoClient('example.com')
  >>> client.the_database.authenticate('user', 'password', mechanism='SCRAM-SHA-1')
  True
  >>>
  >>> uri = "mongodb://user:password@example.com/the_database?authMechanism=SCRAM-SHA-1"
  >>> client = MongoClient(uri)

MONGODB-CR
----------

Before MongoDB 2.8 the default authentication mechanism was MONGODB-CR,
the "MongoDB Challenge-Response" protocol::

  >>> from pymongo import MongoClient
  >>> client = MongoClient('example.com')
  >>> client.the_database.authenticate('user', 'password', mechanism='MONGODB-CR')
  True
  >>>
  >>> uri = "mongodb://user:password@example.com/the_database?authMechanism=MONGODB-CR"
  >>> client = MongoClient(uri)

Default Authentication Mechanism
--------------------------------

If no mechanism is specified, PyMongo automatically uses MONGODB-CR when<commit_msg>PYTHON-706 Mention that backports.pbkdf2 provides the best performance.<commit_after>Authentication Examples
=======================

MongoDB supports several different authentication mechanisms. These examples
cover all authentication methods currently supported by PyMongo, documenting
Python module and MongoDB version dependencies.

SCRAM-SHA-1 (RFC 5802)
----------------------
.. versionadded:: 2.8

SCRAM-SHA-1 is the default authentication mechanism supported by a cluster
configured for authentication with MongoDB 2.8 or later. Authentication is
per-database and credentials can be specified through the MongoDB URI or
passed to the :meth:`~pymongo.database.Database.authenticate` method::

  >>> from pymongo import MongoClient
  >>> client = MongoClient('example.com')
  >>> client.the_database.authenticate('user', 'password', mechanism='SCRAM-SHA-1')
  True
  >>>
  >>> uri = "mongodb://user:password@example.com/the_database?authMechanism=SCRAM-SHA-1"
  >>> client = MongoClient(uri)

For best performance install `backports.pbkdf2`_, especially on Python older
than 2.7.8, or on Python 3 before Python 3.4.

.. _backports.pbkdf2: https://pypi.python.org/pypi/backports.pbkdf2/

MONGODB-CR
----------

Before MongoDB 2.8 the default authentication mechanism was MONGODB-CR,
the "MongoDB Challenge-Response" protocol::

  >>> from pymongo import MongoClient
  >>> client = MongoClient('example.com')
  >>> client.the_database.authenticate('user', 'password', mechanism='MONGODB-CR')
  True
  >>>
  >>> uri = "mongodb://user:password@example.com/the_database?authMechanism=MONGODB-CR"
  >>> client = MongoClient(uri)

Default Authentication Mechanism
--------------------------------

If no mechanism is specified, PyMongo automatically uses MONGODB-CR when
{'fraction_non_alphanumeric': 0.15237226277372262, 'fraction_numerical': 0.012469586374695864, 'mean_word_length': 4.32200647249191, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 75, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4602552', 'n_tokens_mistral': 974, 'n_tokens_neox': 908, 'n_words': 268}