Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > bf9ddee8352dabf2ba1d26636a349741 > files > 421

python2-pymongo-3.7.2-1.mga7.armv7hl.rpm


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Authentication Examples &#8212; PyMongo 3.7.2 documentation</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Collations" href="collations.html" />
    <link rel="prev" title="Aggregation Examples" href="aggregation.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="collations.html" title="Collations"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="aggregation.html" title="Aggregation Examples"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">PyMongo 3.7.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Examples</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="authentication-examples">
<h1>Authentication Examples<a class="headerlink" href="#authentication-examples" title="Permalink to this headline">¶</a></h1>
<p>MongoDB supports several different authentication mechanisms. These examples
cover all authentication methods currently supported by PyMongo, documenting
Python module and MongoDB version dependencies.</p>
<div class="section" id="percent-escaping-username-and-password">
<h2>Percent-Escaping Username and Password<a class="headerlink" href="#percent-escaping-username-and-password" title="Permalink to this headline">¶</a></h2>
<p>Username and password must be percent-escaped with
<code class="xref py py-meth docutils literal notranslate"><span class="pre">urllib.parse.quote_plus()</span></code> in Python 3, or <code class="xref py py-meth docutils literal notranslate"><span class="pre">urllib.quote_plus()</span></code> in
Python 2, to be used in a MongoDB URI. For example, in Python 3:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urllib.parse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">username</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">quote_plus</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">username</span>
<span class="go">&#39;user&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">password</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">quote_plus</span><span class="p">(</span><span class="s1">&#39;pass/word&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">password</span>
<span class="go">&#39;pass%2Fword&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">MongoClient</span><span class="p">(</span><span class="s1">&#39;mongodb://</span><span class="si">%s</span><span class="s1">:</span><span class="si">%s</span><span class="s1">@127.0.0.1&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="n">password</span><span class="p">))</span>
<span class="gp">...</span>
</pre></div>
</div>
</div>
<div class="section" id="scram-sha-256-rfc-7677">
<span id="scram-sha-256"></span><h2>SCRAM-SHA-256 (RFC 7677)<a class="headerlink" href="#scram-sha-256-rfc-7677" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.7.</span></p>
</div>
<p>SCRAM-SHA-256 is the default authentication mechanism supported by a cluster
configured for authentication with MongoDB 4.0 or later. Authentication
requires a username, a password, and a database name. The default database
name is “admin”, this can be overidden with the <code class="docutils literal notranslate"><span class="pre">authSource</span></code> option.
Credentials can be specified as arguments to
<a class="reference internal" href="../api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><code class="xref py py-class docutils literal notranslate"><span class="pre">MongoClient</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="s1">&#39;example.com&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">username</span><span class="o">=</span><span class="s1">&#39;user&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">password</span><span class="o">=</span><span class="s1">&#39;password&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">authSource</span><span class="o">=</span><span class="s1">&#39;the_database&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">authMechanism</span><span class="o">=</span><span class="s1">&#39;SCRAM-SHA-256&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Or through the MongoDB URI:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://user:password@example.com/?authSource=the_database&amp;authMechanism=SCRAM-SHA-256&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="scram-sha-1-rfc-5802">
<h2>SCRAM-SHA-1 (RFC 5802)<a class="headerlink" href="#scram-sha-1-rfc-5802" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.8.</span></p>
</div>
<p>SCRAM-SHA-1 is the default authentication mechanism supported by a cluster
configured for authentication with MongoDB 3.0 or later. Authentication
requires a username, a password, and a database name. The default database
name is “admin”, this can be overidden with the <code class="docutils literal notranslate"><span class="pre">authSource</span></code> option.
Credentials can be specified as arguments to
<a class="reference internal" href="../api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><code class="xref py py-class docutils literal notranslate"><span class="pre">MongoClient</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="s1">&#39;example.com&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">username</span><span class="o">=</span><span class="s1">&#39;user&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">password</span><span class="o">=</span><span class="s1">&#39;password&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">authSource</span><span class="o">=</span><span class="s1">&#39;the_database&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">authMechanism</span><span class="o">=</span><span class="s1">&#39;SCRAM-SHA-1&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Or through the MongoDB URI:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://user:password@example.com/?authSource=the_database&amp;authMechanism=SCRAM-SHA-1&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
</pre></div>
</div>
<p>For best performance on Python versions older than 2.7.8 install <a class="reference external" href="https://pypi.python.org/pypi/backports.pbkdf2/">backports.pbkdf2</a>.</p>
</div>
<div class="section" id="mongodb-cr">
<h2>MONGODB-CR<a class="headerlink" href="#mongodb-cr" title="Permalink to this headline">¶</a></h2>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">MONGODB-CR was deprecated with the release of MongoDB 3.6 and
is no longer supported by MongoDB 4.0.</p>
</div>
<p>Before MongoDB 3.0 the default authentication mechanism was MONGODB-CR,
the “MongoDB Challenge-Response” protocol:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="s1">&#39;example.com&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">username</span><span class="o">=</span><span class="s1">&#39;user&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">password</span><span class="o">=</span><span class="s1">&#39;password&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">authMechanism</span><span class="o">=</span><span class="s1">&#39;MONGODB-CR&#39;</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://user:password@example.com/?authSource=the_database&amp;authMechanism=MONGODB-CR&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="default-authentication-mechanism">
<h2>Default Authentication Mechanism<a class="headerlink" href="#default-authentication-mechanism" title="Permalink to this headline">¶</a></h2>
<p>If no mechanism is specified, PyMongo automatically uses MONGODB-CR when
connected to a pre-3.0 version of MongoDB, SCRAM-SHA-1 when connected to
MongoDB 3.0 through 3.6, and negotiates the mechanism to use (SCRAM-SHA-1
or SCRAM-SHA-256) when connected to MongoDB 4.0+.</p>
</div>
<div class="section" id="default-database-and-authsource">
<h2>Default Database and “authSource”<a class="headerlink" href="#default-database-and-authsource" title="Permalink to this headline">¶</a></h2>
<p>You can specify both a default database and the authentication database in the
URI:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://user:password@example.com/default_db?authSource=admin&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
</pre></div>
</div>
<p>PyMongo will authenticate on the “admin” database, but the default database
will be “default_db”:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># get_database with no &quot;name&quot; argument chooses the DB from the URI</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">db</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span><span class="o">.</span><span class="n">get_database</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">db</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="go">&#39;default_db&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="mongodb-x509">
<span id="id1"></span><h2>MONGODB-X509<a class="headerlink" href="#mongodb-x509" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
<p>The MONGODB-X509 mechanism authenticates a username derived from the
distinguished subject name of the X.509 certificate presented by the driver
during SSL negotiation. This authentication method requires the use of SSL
connections with certificate validation and is available in MongoDB 2.6
and newer:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ssl</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="s1">&#39;example.com&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">username</span><span class="o">=</span><span class="s2">&quot;&lt;X.509 derived username&gt;&quot;</span>
<span class="gp">... </span>                     <span class="n">authMechanism</span><span class="o">=</span><span class="s2">&quot;MONGODB-X509&quot;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_certfile</span><span class="o">=</span><span class="s1">&#39;/path/to/client.pem&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_cert_reqs</span><span class="o">=</span><span class="n">ssl</span><span class="o">.</span><span class="n">CERT_REQUIRED</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_ca_certs</span><span class="o">=</span><span class="s1">&#39;/path/to/ca.pem&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>MONGODB-X509 authenticates against the $external virtual database, so you
do not have to specify a database in the URI:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://&lt;X.509 derived username&gt;@example.com/?authMechanism=MONGODB-X509&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_certfile</span><span class="o">=</span><span class="s1">&#39;/path/to/client.pem&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_cert_reqs</span><span class="o">=</span><span class="n">ssl</span><span class="o">.</span><span class="n">CERT_REQUIRED</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_ca_certs</span><span class="o">=</span><span class="s1">&#39;/path/to/ca.pem&#39;</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>When connected to MongoDB &gt;= 3.4 the username is no longer required.</p>
</div>
</div>
<div class="section" id="gssapi-kerberos">
<span id="gssapi"></span><h2>GSSAPI (Kerberos)<a class="headerlink" href="#gssapi-kerberos" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.5.</span></p>
</div>
<p>GSSAPI (Kerberos) authentication is available in the Enterprise Edition of
MongoDB.</p>
<div class="section" id="unix">
<h3>Unix<a class="headerlink" href="#unix" title="Permalink to this headline">¶</a></h3>
<p>To authenticate using GSSAPI you must first install the python <a class="reference external" href="http://pypi.python.org/pypi/kerberos">kerberos</a> or
<a class="reference external" href="https://pypi.python.org/pypi/pykerberos">pykerberos</a> module using easy_install or pip. Make sure you run kinit before
using the following authentication methods:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ kinit mongodbuser@EXAMPLE.COM
mongodbuser@EXAMPLE.COM&#39;s Password:
$ klist
Credentials cache: FILE:/tmp/krb5cc_1000
        Principal: mongodbuser@EXAMPLE.COM

  Issued                Expires               Principal
Feb  9 13:48:51 2013  Feb  9 23:48:51 2013  krbtgt/EXAMPLE.COM@EXAMPLE.COM
</pre></div>
</div>
<p>Now authenticate using the MongoDB URI. GSSAPI authenticates against the
$external virtual database so you do not have to specify a database in the
URI:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Note: the kerberos principal must be url encoded.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://mongodbuser</span><span class="si">%40E</span><span class="s2">XAMPLE.COM@mongo-server.example.com/?authMechanism=GSSAPI&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>The default service name used by MongoDB and PyMongo is <cite>mongodb</cite>. You can
specify a custom service name with the <code class="docutils literal notranslate"><span class="pre">authMechanismProperties</span></code> option:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://mongodbuser</span><span class="si">%40E</span><span class="s2">XAMPLE.COM@mongo-server.example.com/?authMechanism=GSSAPI&amp;authMechanismProperties=SERVICE_NAME:myservicename&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="windows-sspi">
<h3>Windows (SSPI)<a class="headerlink" href="#windows-sspi" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.3.</span></p>
</div>
<p>First install the <a class="reference external" href="https://pypi.python.org/pypi/winkerberos/">winkerberos</a> module. Unlike authentication on Unix kinit is
not used. If the user to authenticate is different from the user that owns the
application process provide a password to authenticate:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://mongodbuser</span><span class="si">%40E</span><span class="s2">XAMPLE.COM:mongodbuserpassword@example.com/?authMechanism=GSSAPI&quot;</span>
</pre></div>
</div>
<p>Two extra <code class="docutils literal notranslate"><span class="pre">authMechanismProperties</span></code> are supported on Windows platforms:</p>
<ul>
<li><p class="first">CANONICALIZE_HOST_NAME - Uses the fully qualified domain name (FQDN) of the
MongoDB host for the server principal (GSSAPI libraries on Unix do this by
default):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://mongodbuser</span><span class="si">%40E</span><span class="s2">XAMPLE.COM@example.com/?authMechanism=GSSAPI&amp;authMechanismProperties=CANONICALIZE_HOST_NAME:true&quot;</span>
</pre></div>
</div>
</li>
<li><p class="first">SERVICE_REALM - This is used when the user’s realm is different from the service’s realm:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://mongodbuser</span><span class="si">%40E</span><span class="s2">XAMPLE.COM@example.com/?authMechanism=GSSAPI&amp;authMechanismProperties=SERVICE_REALM:otherrealm&quot;</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>
<div class="section" id="sasl-plain-rfc-4616">
<span id="sasl-plain"></span><h2>SASL PLAIN (RFC 4616)<a class="headerlink" href="#sasl-plain-rfc-4616" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
<p>MongoDB Enterprise Edition version 2.6 and newer support the SASL PLAIN
authentication mechanism, initially intended for delegating authentication
to an LDAP server. Using the PLAIN mechanism is very similar to MONGODB-CR.
These examples use the $external virtual database for LDAP support:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://user:password@example.com/?authMechanism=PLAIN&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>SASL PLAIN is a clear-text authentication mechanism. We <strong>strongly</strong> recommend
that you connect to MongoDB using SSL with certificate validation when using
the SASL PLAIN mechanism:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ssl</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">pymongo</span> <span class="k">import</span> <span class="n">MongoClient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">uri</span> <span class="o">=</span> <span class="s2">&quot;mongodb://user:password@example.com/?authMechanism=PLAIN&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">uri</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_certfile</span><span class="o">=</span><span class="s1">&#39;/path/to/client.pem&#39;</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_cert_reqs</span><span class="o">=</span><span class="n">ssl</span><span class="o">.</span><span class="n">CERT_REQUIRED</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="n">ssl_ca_certs</span><span class="o">=</span><span class="s1">&#39;/path/to/ca.pem&#39;</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Authentication Examples</a><ul>
<li><a class="reference internal" href="#percent-escaping-username-and-password">Percent-Escaping Username and Password</a></li>
<li><a class="reference internal" href="#scram-sha-256-rfc-7677">SCRAM-SHA-256 (RFC 7677)</a></li>
<li><a class="reference internal" href="#scram-sha-1-rfc-5802">SCRAM-SHA-1 (RFC 5802)</a></li>
<li><a class="reference internal" href="#mongodb-cr">MONGODB-CR</a></li>
<li><a class="reference internal" href="#default-authentication-mechanism">Default Authentication Mechanism</a></li>
<li><a class="reference internal" href="#default-database-and-authsource">Default Database and “authSource”</a></li>
<li><a class="reference internal" href="#mongodb-x509">MONGODB-X509</a></li>
<li><a class="reference internal" href="#gssapi-kerberos">GSSAPI (Kerberos)</a><ul>
<li><a class="reference internal" href="#unix">Unix</a></li>
<li><a class="reference internal" href="#windows-sspi">Windows (SSPI)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sasl-plain-rfc-4616">SASL PLAIN (RFC 4616)</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="aggregation.html"
                        title="previous chapter">Aggregation Examples</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="collations.html"
                        title="next chapter">Collations</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/examples/authentication.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="collations.html" title="Collations"
             >next</a> |</li>
        <li class="right" >
          <a href="aggregation.html" title="Aggregation Examples"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">PyMongo 3.7.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >Examples</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright MongoDB, Inc. 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
    </div>
  </body>
</html>