@@ -10,9 +10,16 @@ Enterprise Authentication Mechanisms
1010 :depth: 1
1111 :class: singlecol
1212
13+ .. facet::
14+ :name: genre
15+ :values: reference
16+
17+ .. meta::
18+ :keywords: ldap, encryption, principal, tls
19+
1320In this guide, you can find sample code for connection to MongoDB with each
1421authentication mechanism available in the MongoDB Enterprise Edition:
15- ``Kerberos (GSSAPI/SSPI)`` and ``LDAP (PLAIN)``.
22+ ``Kerberos (GSSAPI/SSPI)``, ``LDAP (PLAIN)``, and ``MONGODB-OIDC ``.
1623
1724``Kerberos (GSSAPI/SSPI)``
1825--------------------------
@@ -138,3 +145,209 @@ in the following sample code.
138145 The authentication mechanism is named ``PLAIN`` instead of ``LDAP`` since it
139146 authenticates using the `PLAIN Simple Authentication and Security Layer
140147 (SASL) defined in RFC-4616 <https://tools.ietf.org/html/rfc4616>`_.
148+
149+ MONGODB-OIDC
150+ ------------
151+
152+ .. important::
153+
154+ The MONGODB-OIDC authentication mechanism requires {+mdb-server+} v7.0 or later running
155+ on a Linux platform.
156+
157+ The following sections describe how to use the MONGODB-OIDC authentication mechanism to
158+ authenticate from various platforms.
159+
160+ For more information about the MONGODB-OIDC authentication mechanism, see
161+ :manual:`OpenID Connect Authentication </core/security-oidc/>` and
162+ :manual:`MongoDB Server Parameters </reference/parameters/#mongodb-parameter-param.oidcIdentityProviders>`
163+ in the {+mdb-server+} manual.
164+
165+ .. _node-mongodb-oidc-azure-imds:
166+
167+ Azure IMDS
168+ ~~~~~~~~~~
169+
170+ If your application runs on an Azure VM, or otherwise uses the
171+ `Azure Instance Metadata Service <https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service>`__
172+ (IMDS), you can authenticate to MongoDB by using the {+driver-short+}'s built-in Azure
173+ support.
174+
175+ To specify Azure IMDS OIDC as the authentication mechanism, set the following options
176+ in your connection string:
177+
178+ - ``username``: If you're using an Azure managed identity, set this to the client ID
179+ of the managed identity. If you're using a service principal to represent an
180+ enterprise application, set this to the application ID of the service principal.
181+ Otherwise, omit this option.
182+ - ``authMechanism``: Set to ``MONGODB-OIDC``.
183+ - ``authMechanismProperties``: Set to
184+ ``ENVIRONMENT:azure,TOKEN_RESOURCE:<audience>``.
185+ Replace the ``<audience>`` placeholder with the
186+ value of the ``audience`` parameter configured on your MongoDB deployment.
187+
188+ The following code example shows how to set the preceding connection options:
189+
190+ .. code-block:: js
191+ :emphasize-lines: 3-4
192+
193+ const { MongoClient } = require("mongodb");
194+
195+ const uri = "mongodb+srv://<username>@<hostname>:<port>/?authMechanism=MONGODB-OIDC"
196+ + "&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<audience>";
197+ const client = new MongoClient(uri);
198+
199+ .. _node-mongodb-oidc-gcp-imds:
200+
201+ GCP IMDS
202+ ~~~~~~~~
203+
204+ If your application runs on a Google Compute Engine VM, or otherwise uses the
205+ `GCP Instance Metadata Service <https://cloud.google.com/compute/docs/metadata/querying-metadata>`__,
206+ you can authenticate to MongoDB by using the {+driver-short+}'s built-in GCP
207+ support.
208+
209+ To specify GCP IMDS OIDC as the authentication mechanism, set the following options
210+ in your connection string:
211+
212+ - ``authMechanism``: Set to ``MONGODB-OIDC``.
213+ - ``authMechanismProperties``: Set to
214+ ``ENVIRONMENT:gcp,TOKEN_RESOURCE:<audience>``.
215+ Replace the ``<audience>`` placeholder with the
216+ value of the ``audience`` parameter configured on your MongoDB deployment.
217+
218+ The following code example shows how to set the preceding connection options:
219+
220+ .. code-block:: js
221+ :emphasize-lines: 3-4
222+
223+ const { MongoClient } = require("mongodb");
224+
225+ const uri = "mongodb+srv://<host>:<port>/?authMechanism=MONGODB-OIDC"
226+ + "&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<audience>";
227+ const client = new MongoClient(uri);
228+
229+ Custom Callback
230+ ~~~~~~~~~~~~~~~
231+
232+ The {+driver-short+} doesn't offer built-in support for all platforms, including
233+ Azure Functions and Azure Kubernetes Service (AKS). Instead, you
234+ must define a custom callback to use OIDC to authenticate from these platforms.
235+
236+ First, define a function that retrieves the access token to use for OIDC authentication.
237+ This function must have the following signature:
238+
239+ .. code-block:: js
240+
241+ const myCallback = (params: OIDCCallbackParams): Promise<OIDCResponse> => { }
242+
243+ The ``OIDCCallbackParams`` parameter contains the following properties, which you can
244+ access inside the function:
245+
246+ .. list-table::
247+ :header-rows: 1
248+
249+ * - Property
250+ - Value
251+ * - ``timeoutContext``
252+ - An ``AbortSignal`` that aborts the authentication workflow after 30 seconds
253+ * - ``version``
254+ - The current OIDC API version
255+ * - ``idpInfo``
256+ - The identity-provider information returned from the server
257+ * - ``username``
258+ - The username included in the connection string, if any
259+ * - ``refreshToken``
260+ - The refresh token to request a new access token from the issuer, if any
261+
262+ The callback function must return an ``OIDCResponse`` object. This object contains the
263+ following properties:
264+
265+ .. list-table::
266+ :header-rows: 1
267+
268+ * - Property
269+ - Value
270+ * - ``accessToken``
271+ - The access token to use for authentication.
272+ * - ``expiresInSeconds``
273+ - *Optional.* The number of seconds until the access token expires.
274+ * - ``refreshToken``
275+ - *Optional.* The refresh token to request a new access token from the issuer.
276+
277+ The following example shows a callback function that retrieves an OIDC access token
278+ from a file named ``access-token.dat`` in the local file system:
279+
280+ .. code-block:: js
281+
282+ const fs = require("node:fs");
283+
284+ const myCallback = (params: OIDCCallbackParams): Promise<OIDCResponse> => {
285+ const token = fs.readFileSync("access-token.dat", "utf8");
286+
287+ return {
288+ accessToken: token,
289+ expiresInSeconds: 300,
290+ refreshToken: token
291+ };
292+ }
293+
294+ After you define your callback function, pass it to the ``MongoClient`` constructor
295+ as part of the ``authMechanismProperties`` parameter. The {+driver-short+} supports
296+ the following authentication patterns:
297+
298+ - **Machine authentication:** Used by web services and other applications that require
299+ no human interaction. Select the :guilabel:`Machine Callback` tab to see an example of
300+ this syntax.
301+ - **Human authentication:** Used by database tools, command-line utilities, and other
302+ applications that involve direct human interaction. Select the :guilabel:`Human Callback`
303+ tab to see an example of this syntax.
304+
305+ .. tabs::
306+
307+ .. tab:: Machine Callback
308+ :tabid: machine-callback
309+
310+ For machine authentication, assign the callback function to the
311+ ``authMechanismProperties.OIDC_CALLBACK`` property, as shown in the following
312+ example:
313+
314+ .. code-block:: js
315+ :emphasize-lines: 4-7
316+
317+ const { MongoClient } = require("mongodb");
318+
319+ const uri = "mongodb+srv://<host>:<port>/?authMechanism=MONGODB-OIDC";
320+ const client = new MongoClient(uri, {
321+ authMechanismProperties: {
322+ OIDC_CALLBACK: myCallback
323+ }
324+ });
325+
326+ .. tab:: Human Callback
327+ :tabid: human-callback
328+
329+ For human authentication, assign the callback function to the
330+ ``authMechanismProperties.OIDC_HUMAN_CALLBACK`` property, as shown in the following
331+ example:
332+
333+ .. code-block:: js
334+ :emphasize-lines: 4-7
335+
336+ const { MongoClient } = require("mongodb");
337+
338+ const uri = "mongodb+srv://<host>:<port>/?authMechanism=MONGODB-OIDC";
339+ const client = new MongoClient(uri, {
340+ authMechanismProperties: {
341+ OIDC_HUMAN_CALLBACK: myCallback
342+ }
343+ });
344+
345+ API Documentation
346+ -----------------
347+
348+ To learn more about the methods and types discussed in this
349+ guide, see the following API documentation:
350+
351+ - `MongoClient <{+api+}/classes/MongoClient.html>`__
352+ - `OIDCCallbackParams <{+api+}/interfaces/OIDCCallbackParams.html>`__
353+ - `OIDCResponse <{+api+}/interfaces/OIDCResponse.html>`__
0 commit comments