11# Authentication
22
33- Status: Accepted
4- - Minimum Server Version: 2.6
54
65______________________________________________________________________
76
@@ -103,8 +102,8 @@ Userinfo or authentication parameters in connection options MUST NOT be interpre
103102#### Errors
104103
105104Drivers SHOULD raise an error as early as possible when detecting invalid values in a credential. For instance, if a
106- ` mechanism_property ` is specified for [ MONGODB-CR ] ( #mongodb-cr ) , the driver should raise an error indicating that the
107- property does not apply.
105+ ` mechanism_property ` is specified for a mechanism that does not support it , the driver should raise an error indicating
106+ that the property does not apply.
108107
109108Drivers MUST raise an error if any required information for a mechanism is missing. For instance, if a ` username ` is not
110109specified for SCRAM-SHA-256, the driver must raise an error indicating the the property is missing.
@@ -147,8 +146,7 @@ Drivers MUST follow the following steps for an authentication handshake:
147146 type. If the ` hello ` or legacy hello of the MongoDB Handshake fails with an error, drivers MUST treat this as an
148147 authentication error.
1491482 . If the server is of type RSArbiter, no authentication is possible and the handshake is complete.
150- 3 . Inspect the value of ` maxWireVersion ` . If the value is greater than or equal to ` 6 ` , then the driver MUST use
151- ` OP_MSG ` for authentication. Otherwise, it MUST use ` OP_QUERY ` .
149+ 3 . Drivers MUST use ` OP_MSG ` for authentication.
1521504 . If credentials exist: 3.1. A driver MUST authenticate with all credentials provided to the MongoClient. 3.2. A single
153151 invalid credential is the same as all credentials being invalid.
154152
@@ -162,8 +160,6 @@ All blocking operations executed as part of the authentication handshake MUST ap
162160
163161#### Mechanism Negotiation via Handshake
164162
165- - Since: 4.0
166-
167163If an application provides a username but does not provide an authentication mechanism, drivers MUST negotiate a
168164mechanism via a ` hello ` or legacy hello command requesting a user's supported SASL mechanisms:
169165
@@ -177,8 +173,7 @@ in the auth credential. The username MUST NOT be modified from the form provided
177173SASLprep), as the server uses the raw form to look for conflicts with legacy credentials.
178174
179175If the handshake response includes a ` saslSupportedMechs ` field, then drivers MUST use the contents of that field to
180- select a default mechanism as described later. If the command succeeds and the response does not include a
181- ` saslSupportedMechs ` field, then drivers MUST use the legacy default mechanism rules for servers older than 4.0.
176+ select a default mechanism as described later.
182177
183178Drivers MUST NOT validate the contents of the ` saslSupportedMechs ` attribute of the initial handshake reply. Drivers
184179MUST NOT raise an error if the ` saslSupportedMechs ` attribute of the reply includes an unknown mechanism.
@@ -198,13 +193,8 @@ Drivers with a list of credentials at the time a connection is opened MAY do mec
198193handshake, but only for the first credential in the list of credentials.
199194
200195When authenticating each credential, if the authentication mechanism is not specified and has not been negotiated for
201- that credential:
202-
203- - If the connection handshake results indicate the server version is 4.0 or later, drivers MUST send a new ` hello ` or
204- legacy hello negotiation command for the credential to determine the default authentication mechanism.
205- - Otherwise, when the server version is earlier than 4.0, the driver MUST select a default authentication mechanism for
206- the credential following the instructions for when the ` saslSupportedMechs ` field is not present in a legacy hello
207- response.
196+ that credential, drivers MUST send a new ` hello ` or legacy hello negotiation command for the credential to determine the
197+ default authentication mechanism.
208198
209199### Caching credentials in SCRAM
210200
@@ -232,9 +222,6 @@ used when running the authentication spec tests.
232222
233223### Default Authentication Methods
234224
235- - Since: 3.0
236- - Revised: 4.0
237-
238225If the user did not provide a mechanism via the connection string or via code, the following logic describes how to
239226select a default.
240227
@@ -255,101 +242,22 @@ be used as the default, regardless of whether SCRAM-SHA-1 is in the list. Driver
255242mechanism (e.g. PLAIN) as the default.
256243
257244If ` saslSupportedMechs ` is not present in the handshake response for mechanism negotiation, then SCRAM-SHA-1 MUST be
258- used when talking to servers >= 3.0. Prior to server 3.0, MONGODB-CR MUST be used .
245+ used as the default .
259246
260247When a user has specified a mechanism, regardless of the server version, the driver MUST honor this.
261248
262- #### Determining Server Version
263-
264- Drivers SHOULD use the server's wire version ranges to determine the server's version.
265-
266- ### MONGODB-CR
267-
268- - Since: 1.4
269- - Deprecated: 3.0
270- - Removed: 4.0
271-
272- MongoDB Challenge Response is a nonce and MD5 based system. The driver sends a ` getnonce ` command, encodes and hashes
273- the password using the returned nonce, and then sends an ` authenticate ` command.
274-
275- #### Conversation
276-
277- 1 . Send ` getnonce ` command
278-
279- ``` javascript
280- CMD = { getnonce: 1 }
281- RESP = { nonce: < nonce> }
282- ```
283-
284- 2. Compute key
285-
286- ` ` ` javascript
287- passwordDigest = HEX( MD5( UTF8( username + ':mongo:' + password )))
288- key = HEX( MD5( UTF8( nonce + username + passwordDigest )))
289- ` ` `
290-
291- 3. Send ` authenticate` command
292-
293- ` ` ` javascript
294- CMD = { authenticate: 1, nonce: nonce, user: username, key: key }
295- ` ` `
296-
297- As an example, given a username of " user" and a password of " pencil" , the conversation would appear as follows:
298-
299- ` ` ` javascript
300- CMD = {getnonce : 1}
301- RESP = {nonce: "2375531c32080ae8", ok: 1}
302- CMD = {authenticate: 1, user: "user", nonce: "2375531c32080ae8", key: "21742f26431831d5cfca035a08c5bdf6"}
303- RESP = {ok: 1}
304- ` ` `
305-
306- #### [MongoCredential](#mongocredential) Properties
307-
308- - username
309-
310- MUST be specified and non- zero length.
311-
312- - source
313-
314- MUST be specified . Defaults to the database name if supplied on the connection string or ` admin` .
315-
316- - password
317-
318- MUST be specified.
319-
320- - mechanism
321-
322- MUST be " MONGODB-CR"
323-
324- - mechanism_properties
325-
326- MUST NOT be specified.
327-
328249### MONGODB-X509
329250
330- - Since: 2.6
331- - Changed: 3.4
332-
333251MONGODB-X509 is the usage of X.509 certificates to validate a client where the distinguished subject name of the client
334252certificate acts as the username.
335253
336- When connected to MongoDB 3.4 :
337-
338254- You MUST NOT raise an error when the application only provides an X.509 certificate and no username.
339255- If the application does not provide a username you MUST NOT send a username to the server.
340256- If the application provides a username you MUST send that username to the server.
341257
342- When connected to MongoDB 3.2 or earlier:
343-
344- - You MUST send a username to the server.
345- - If no username is provided by the application, you MAY extract the username from the X .509 certificate instead of
346- requiring the application to provide it.
347- - If you choose not to automatically extract the username from the certificate you MUST error when no username is
348- provided by the application.
349-
350258#### Conversation
351259
352- 1. Send ` authenticate` command (MongoDB 3.4 + )
260+ 1 . Send ` authenticate ` command
353261
354262 ``` javascript
355263 CMD = {" authenticate" : 1 , " mechanism" : " MONGODB-X509" }
@@ -371,7 +279,7 @@ When connected to MongoDB 3.2 or earlier:
371279
372280- username
373281
374- SHOULD NOT be provided for MongoDB 3.4 + MUST be specified and non - zero length for MongoDB prior to 3.4
282+ SHOULD NOT be provided
375283
376284- source
377285
@@ -393,8 +301,6 @@ TODO: Errors
393301
394302### SASL Mechanisms
395303
396- - Since: 2.4 Enterprise
397-
398304SASL mechanisms are all implemented using the same sasl commands and interpreted as defined by the
399305[SASL specification RFC 4422](http://tools.ietf.org/html/rfc4422).
400306
@@ -428,12 +334,6 @@ SASL mechanisms are all implemented using the same sasl commands and interpreted
428334
429335### GSSAPI
430336
431- - Since:
432-
433- 2.4 Enterprise
434-
435- 2.6 Enterprise on Windows
436-
437337GSSAPI is kerberos authentication as defined in [RFC 4752](http://tools.ietf.org/html/rfc4752). Microsoft has a
438338proprietary implementation called SSPI which is compatible with both Windows and Linux clients.
439339
@@ -557,8 +457,6 @@ configuration option is set to `false`.
557457
558458### PLAIN
559459
560- - Since: 2.6 Enterprise
561-
562460The PLAIN mechanism, as defined in [RFC 4616 ](http: // tools.ietf.org/html/rfc4616), is used in MongoDB to perform LDAP
563461authentication . It cannot be used to perform any other type of authentication . Since the credentials are stored outside
564462of MongoDB, the ` $external` database must be used for authentication.
@@ -605,8 +503,6 @@ MongoDB supports either of these forms.
605503
606504### SCRAM - SHA - 1
607505
608- - Since: 3.0
609-
610506SCRAM - SHA - 1 is defined in [RFC 5802 ](http: // tools.ietf.org/html/rfc5802).
611507
612508[Page 11 of the RFC ](http: // tools.ietf.org/html/rfc5802#page-11) specifies that user names be prepared with SASLprep,
@@ -691,8 +587,6 @@ RESP = {conversationId: 1, payload: BinData(0,"dj1VTVdlSTI1SkQxeU5ZWlJNcFo0Vkh2a
691587
692588### SCRAM-SHA-256
693589
694- - Since: 4.0
695-
696590SCRAM-SHA-256 extends [RFC 5802](http://tools.ietf.org/html/rfc5802) and is formally defined in
697591[RFC 7677](https://tools.ietf.org/html/rfc7677).
698592
@@ -1903,19 +1797,18 @@ def reauth(connection):
19031797
19041798- authMechanism
19051799
1906- MONGODB - CR , MONGODB - X509 , GSSAPI , PLAIN , SCRAM - SHA - 1 , SCRAM - SHA - 256 , MONGODB - AWS
1800+ MONGODB - X509 , GSSAPI , PLAIN , SCRAM - SHA - 1 , SCRAM - SHA - 256 , MONGODB - AWS
19071801
1908- Sets the Mechanism property on the MongoCredential . When not set, the default will be one of SCRAM - SHA - 256 ,
1909- SCRAM - SHA - 1 or MONGODB - CR , following the auth spec default mechanism rules.
1802+ Sets the Mechanism property on the MongoCredential . When not set, the default will be SCRAM - SHA - 256 or SCRAM - SHA - 1 ,
1803+ following the auth spec default mechanism rules.
19101804
19111805- authSource
19121806
19131807 Sets the Source property on the MongoCredential.
19141808
19151809For GSSAPI , MONGODB - X509 and MONGODB - AWS authMechanisms the authSource defaults to ` $external` . For PLAIN the authSource
1916- defaults to the database name if supplied on the connection string or ` $external` . For MONGODB - CR , SCRAM - SHA - 1 and
1917- SCRAM - SHA - 256 authMechanisms, the authSource defaults to the database name if supplied on the connection string or
1918- ` admin` .
1810+ defaults to the database name if supplied on the connection string or ` $external` . For SCRAM - SHA - 1 and SCRAM - SHA - 256
1811+ authMechanisms, the authSource defaults to the database name if supplied on the connection string or ` admin` .
19191812
19201813- authMechanismProperties= PROPERTY_NAME : PROPERTY_VALUE ,PROPERTY_NAME2 : PROPERTY_VALUE2
19211814
@@ -1960,8 +1853,6 @@ Connection string tests have been defined in the associated files:
19601853
19611854### SCRAM - SHA - 256 and mechanism negotiation
19621855
1963- Testing SCRAM - SHA - 256 requires server version 3.7 .3 or later with ` featureCompatibilityVersion` of " 4.0" or later.
1964-
19651856Drivers that allow specifying auth parameters in code as well as via connection string should test both for the test
19661857cases described below.
19671858
@@ -2143,6 +2034,8 @@ practice to avoid this. (See
21432034
21442035## Changelog
21452036
2037+ - 2026-06-17: Remove additional pre-4.2 version references.
2038+
21462039- 2025-11-25: Remove redundant `*.mongodbgov.net` on `ALLOWED_HOSTS`
21472040
21482041- 2025-11-19: Extend `ALLOWED_HOSTS` with `*.mongo.com` and `*.mongodbgov.net`
0 commit comments