Skip to content

Commit e3d57d5

Browse files
authored
docs(guide): convert /tutorials/connect/authenticating.md to rst
Fixes NODE-2199
1 parent 9f9d2e6 commit e3d57d5

File tree

1 file changed

+289
-0
lines changed

1 file changed

+289
-0
lines changed
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,292 @@
11
==============
22
Authentication
33
==============
4+
5+
The Node.js driver supports all MongoDB :manual:`authentication mechanisms </core/authentication/>` , including those only available in the MongoDB :manual:`Enterprise Edition </administration/install-enterprise/>` .
6+
7+
DEFAULT
8+
-------
9+
10+
To use the default mechanism, either omit the authentication mechanism specification or specify ``DEFAULT`` as the mechanism in the :manual:`URI ConnectionString </reference/connection-string/>` . The driver will attempt to authenticate using the :manual:`SCRAM-SHA-256 authentication </core/security-scram/>` method if it is available on the MongoDB server. If the server does not support ``SCRAM-SHA-256``, but does support `SCRAM-SHA-1``, the driver will authenticate with ``SCRAM-SHA-1``. If neither ``SCRAM-SHA-256`` or ``SCRAM-SHA-1`` are supported on the server, the driver will authenticate using ``MONGODB-CR`` .
11+
12+
Include the name and password and the :manual:`authentication database </core/security-users/#user-authentication-database>` (\ ``authSource``\ ) in the connection string.
13+
14+
In the following example, the connection string specifies the user ``dave``\ , password ``abc123``\ , and authentication mechanism ``DEFAULT``.
15+
16+
.. important::
17+
The user and password should always be **URI** encoded using ``encodeURIComponent`` to ensure any non URI compliant user or password characters are correctly parsed.
18+
19+
.. code-block:: js
20+
21+
const { MongoClient } = require('mongodb');
22+
23+
const user = encodeURIComponent('dave');
24+
const password = encodeURIComponent('abc123');
25+
const authMechanism = 'DEFAULT';
26+
27+
// Connection URL
28+
const url = `mongodb://${user}:${password}@localhost:27017/?authMechanism=${authMechanism}`;
29+
30+
// Create a new MongoClient
31+
const client = new MongoClient(url);
32+
33+
// Function to connect to the server
34+
async function run() {
35+
try {
36+
// Connect the client to the server
37+
await client.connect();
38+
console.log('Connected successfully to server');
39+
} finally {
40+
// Ensures that the client will close when you finish/error
41+
await client.close();
42+
}
43+
}
44+
45+
// Runs your code
46+
run();
47+
48+
SCRAM-SHA-256
49+
-------------
50+
51+
.. note::
52+
``SCRAM-SHA-256`` is the default authentication method for MongoDB starting in version 4.0
53+
54+
To explicitly connect to MongoDB using :manual:`SCRAM-SHA-256 </core/security-scram/>` , specify ``SCRAM-SHA-256`` as the mechanism in the :manual:`URI ConnectionString </reference/connection-string/>` .
55+
56+
Include the name and password and the :manual:`authentication database </core/security-users/#user-authentication-database>` (\ ``authSource``\ ) in the connection string.
57+
58+
In the following example, the connection string specifies the user ``dave``\ , password ``abc123``\ , authentication mechanism ``SCRAM-SHA-256``\ , and authentication database ``myprojectdb``
59+
60+
.. code-block:: js
61+
62+
const { MongoClient } = require('mongodb');
63+
64+
// Connection URL
65+
const url = 'mongodb://dave:abc123@localhost:27017/?authMechanism=SCRAM-SHA-256&authSource=myprojectdb';
66+
67+
// Create a new MongoClient
68+
const client = new MongoClient(url);
69+
70+
// Function to connect to the server
71+
async function run() {
72+
try {
73+
// Connect the client to the server
74+
await client.connect();
75+
console.log('Connected successfully to server');
76+
} finally {
77+
// Ensures that the client will close when you finish/error
78+
await client.close();
79+
}
80+
}
81+
82+
// Runs your code
83+
run();
84+
85+
SCRAM-SHA-1
86+
-----------
87+
88+
.. note::
89+
``SCRAM-SHA-1`` is the default authentication method for MongoDB versions 3.0, 3.2, 3.4, and 3.6.
90+
91+
To explicitly connect to MongoDB using :manual:`SCRAM-SHA-1 </core/security-scram/>` , specify ``SCRAM-SHA-1`` as the mechanism in the :manual:`URI ConnectionString </reference/connection-string/>` .
92+
93+
Include the name and password and the :manual:`authentication database </core/security-users/#user-authentication-database>` (\ ``authSource``\ ) in the connection string.
94+
95+
In the following example, the connection string specifies the user ``dave``\ , password ``abc123``\ , authentication mechanism ``SCRAM-SHA-1``\ , and authentication database ``myprojectdb``
96+
97+
.. code-block:: js
98+
99+
const { MongoClient } = require('mongodb');
100+
101+
// Connection URL
102+
const url = 'mongodb://dave:abc123@localhost:27017/?authMechanism=SCRAM-SHA-1&authSource=myprojectdb';
103+
104+
// Create a new MongoClient
105+
const client = new MongoClient(url);
106+
107+
// Function to connect to the server
108+
async function run() {
109+
try {
110+
// Connect the client to the server
111+
await client.connect();
112+
console.log('Connected successfully to server');
113+
} finally {
114+
// Ensures that the client will close when you finish/error
115+
await client.close();
116+
}
117+
}
118+
119+
// Runs your code
120+
run();
121+
122+
MONGODB-CR
123+
----------
124+
125+
.. warning::
126+
MONGODB-CR was deprecated starting in MongoDB 3.6, and is no longer supported as of MongoDB 4.0
127+
128+
129+
To explicitly connect to MongoDB using ``MONGODB-CR`` , specify ``MONGODB-CR`` as the mechanism in the :manual:`URI ConnectionString </reference/connection-string/>` .
130+
131+
Include the name and password and the :manual:`authentication database </core/security-users/#user-authentication-database>` (\ ``authSource``\ ) in the connection string.
132+
133+
In the following example, the connection string specifies the user ``dave``\ , password ``abc123``\ , authentication mechanism ``MONGODB-CR``\ , and authentication database ``myprojectdb``.
134+
135+
.. code-block:: js
136+
137+
const { MongoClient } = require('mongodb');
138+
139+
// Connection URL
140+
const url = 'mongodb://dave:abc123@localhost:27017/?authMechanism=MONGODB-CR&authSource=myprojectdb';
141+
142+
// Create a new MongoClient
143+
const client = new MongoClient(url);
144+
145+
// Function to connect to the server
146+
async function run() {
147+
try {
148+
// Connect the client to the server
149+
await client.connect();
150+
console.log('Connected successfully to server');
151+
} finally {
152+
// Ensures that the client will close when you finish/error
153+
await client.close();
154+
}
155+
}
156+
157+
// Runs your code
158+
run();
159+
160+
.. important::
161+
If you have :manual:`upgraded the authentication schema </release-notes/3.0-scram/>` from ``MONGODB-CR`` to ``SCRAM-SHA-1``\ , ``MONGODB-CR`` credentials will fail to authenticate.
162+
163+
X509
164+
----
165+
166+
With :manual:`X.509 </core/security-x.509>` mechanism, MongoDB uses the X.509 certificate presented during SSL negotiation to authenticate a user whose name is derived from the distinguished name of the X.509 certificate.
167+
168+
X.509 authentication requires the use of SSL connections with certificate validation and is available in MongoDB 2.6 and newer.
169+
170+
To connect using the X.509 authentication mechanism, specify ``MONGODB-X509`` as the mechanism in the :manual:`URI ConnectionString </reference/connection-string/>` , ``ssl=true``\ , and the username. Use ``enodeURIComponent`` to encode the username string.
171+
172+
In addition to the connection string, pass to the new ``MongoClient`` a connections options for the ``server`` with the X.509 certificate and other :doc:`TLS/SSL connections </tutorials/connect/tls>` options.
173+
174+
.. code-block:: js
175+
176+
const { MongoClient } = require('mongodb');
177+
const fs = require('fs');
178+
179+
// Read the cert and key
180+
const cert = fs.readFileSync(`${__dirname}/ssl/x509/client.pem`);
181+
const key = fs.readFileSync(`${__dirname}/ssl/x509/client.pem`);
182+
183+
// User name
184+
const userName = encodeURIComponent('CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US');
185+
const url = `mongodb://${userName}:${password}@server:27017?authMechanism=MONGODB-X509&ssl=true`;
186+
187+
// Create a new MongoClient
188+
const client = new MongoClient(url, {
189+
sslKey: key,
190+
sslCert: cert,
191+
sslValidate: false
192+
});
193+
194+
// Function to connect to the server
195+
async function run() {
196+
try {
197+
// Connect the client to the server
198+
await client.connect();
199+
console.log('Connected successfully to server');
200+
} finally {
201+
// Ensures that the client will close when you finish/error
202+
await client.close();
203+
}
204+
}
205+
206+
// Runs your code
207+
run();
208+
209+
For more information on connecting to MongoDB instance, replica set, and sharded cluster with TLS/SSL options, see :doc:`TLS/SSL connections </tutorials/connect/tls>` .
210+
211+
For more information, refer to the MongoDB manual
212+
:manual:`X.509 tutorial </tutorial/configure-x509-client-authentication/#add-x-509-certificate-subject-as-a-user>` for more information about determining the subject name from the certificate.
213+
214+
Kerberos (GSSAPI/SSPI)
215+
----------------------
216+
217+
`MongoDB Enterprise <http://www.mongodb.com/products/mongodb-enterprise>`_ supports proxy authentication through a Kerberos service. The Node.js driver supports Kerberos on UNIX via the MIT Kerberos library and on Windows via the SSPI API.
218+
219+
To connect using the GSSAPI authentication mechanism, specify ``authMechanism=GSSAPI`` as the mechanism in the :manual:`URI ConnectionString </reference/connection-string/>` . Specify the user principal and the service name in the connection string. Use ``enodeURIComponent`` to encode the user principal string.
220+
221+
The following example connects to MongoDB using Kerberos for UNIX.
222+
223+
.. code-block:: js
224+
225+
const { MongoClient } = require('mongodb');
226+
227+
// KDC Server
228+
const server = 'mongo-server.example.com';
229+
const principal = 'drivers@KERBEROS.EXAMPLE.COM';
230+
const urlEncodedPrincipal = encodeURIComponent(principal);
231+
232+
const url = `mongodb://${urlEncodedPrincipal}@${server}/?authMechanism=GSSAPI&gssapiServiceName=mongodb`;
233+
234+
const client = new MongoClient(url);
235+
236+
// Function to connect to the server
237+
async function run() {
238+
try {
239+
// Connect the client to the server
240+
await client.connect();
241+
console.log('Connected successfully to server');
242+
} finally {
243+
// Ensures that the client will close when you finish/error
244+
await client.close();
245+
}
246+
}
247+
248+
// Runs your code
249+
run();
250+
251+
.. note::
252+
The method refers to the ``GSSAPI`` authentication mechanism instead of ``Kerberos`` because technically the driver authenticates via the :rfc:`GSSAPI <4752>` SASL mechanism.
253+
254+
LDAP (PLAIN)
255+
------------
256+
257+
`MongoDB Enterprise <http://www.mongodb.com/products/mongodb-enterprise>`_ supports proxy authentication through a Lightweight Directory Access Protocol (LDAP) service.
258+
259+
To connect using the LDAP authentication mechanism, specify ``authMechanism=PLAIN`` as the mechanism in the :manual:`URI ConnectionString </reference/connection-string/>` .
260+
261+
.. code-block:: js
262+
263+
const { MongoClient } = require('mongodb');
264+
265+
// LDAP Server
266+
const server = 'ldap.example.com';
267+
const user = 'ldap-user';
268+
const pass = 'ldap-password';
269+
270+
// Url
271+
const url = `mongodb://${user}:${pass}@${server}?authMechanism=PLAIN&maxPoolSize=1`;
272+
273+
// Client
274+
const client = new MongoClient(url);
275+
276+
// Function to connect to the server
277+
async function run() {
278+
try {
279+
// Connect the client to the server
280+
await client.connect();
281+
console.log('Connected successfully to server');
282+
} finally {
283+
// Ensures that the client will close when you finish/error
284+
await client.close();
285+
}
286+
}
287+
288+
// Runs your code
289+
run();
290+
291+
.. note::
292+
The method refers to the ``PLAIN`` authentication mechanism instead of ``LDAP`` because technically the driver authenticates via the :rfc:`PLAIN <4616>` SASL mechanism.

0 commit comments

Comments
 (0)