Skip to content

Commit 9bd8b90

Browse files
committed
edits
1 parent ff96daf commit 9bd8b90

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

source/includes/authentication.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5+
// start-default-client
6+
$uriOptions = [
7+
'username' => '<username>',
8+
'password' => '<password>',
9+
'authSource' => '<authentication database>',
10+
];
11+
12+
$client = new MongoDB\Client(
13+
'mongodb://<hostname>:<port>',
14+
$uriOptions,
15+
);
16+
// end-default-client
17+
18+
// start-default-uri
19+
$uri = 'mongodb://<username>:<password>@<hostname>:<port>/?authSource=admin';
20+
$client = new MongoDB\Client($uri);
21+
// end-default-uri
22+
523
// start-scram-sha-256-client
624
$uriOptions = [
725
'username' => '<username>',

source/security/authentication/scram.txt

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,36 @@ To authenticate with this mechanism, set the following connection options:
5656
- ``authSource``: The MongoDB database to authenticate against. By default, the
5757
{+php-library+} authenticates against the database in the connection
5858
URI, if you include one. If you don't, it authenticates against the ``admin`` database.
59-
- ``authMechanism``: Set to ``'SCRAM-SHA-256'``. When connected to {+mdb-server+} v4.0,
60-
setting ``authMechanism`` is optional.
6159

6260
You can set these options in two ways: by passing an options array to the
6361
``MongoDB\Client`` constructor or through parameters in your connection URI.
6462
Select the :guilabel:`MongoDB\\Client` or :guilabel:`Connection URI` tab to
6563
see the corresponding code:
6664

65+
.. tabs::
66+
67+
.. tab:: MongoDB\\Client
68+
:tabid: Client
69+
70+
.. literalinclude:: /includes/authentication.php
71+
:language: php
72+
:dedent:
73+
:start-after: start-default-client
74+
:end-before: end-default-client
75+
76+
.. tab:: Connection URI
77+
:tabid: connectionstring
78+
79+
.. literalinclude:: /includes/authentication.php
80+
:language: php
81+
:dedent:
82+
:start-after: start-default-uri
83+
:end-before: end-default-uri
84+
85+
You can also explicitly specify the ``SCRAM-SHA-256`` authentication mechanism
86+
by setting the ``authMechanism`` connection option to ``'SCRAM-SHA-256'``, as
87+
shown in the following example:
88+
6789
.. tabs::
6890

6991
.. tab:: MongoDB\\Client
@@ -89,18 +111,46 @@ see the corresponding code:
89111
SCRAM-SHA-1
90112
-----------
91113

92-
SCRAM-SHA-1, as defined by `RFC 5802 <https://www.rfc-editor.org/rfc/rfc5802>`__,
93-
is the default authentication mechanism on MongoDB deployments
94-
running {+mdb-server+} v3.6.
114+
``SCRAM-SHA-1``, as defined by `RFC 5802 <https://www.rfc-editor.org/rfc/rfc5802>`__,
115+
is a Salted Challenge Response Authentication Mechanism (SCRAM) that uses your
116+
username and password, encrypted with the ``SHA-1`` algorithm, to authenticate
117+
your user.
95118

96-
.. note::
119+
To authenticate with this mechanism, set the following connection options:
97120

98-
{+php-library+} v1.20 drops support for {+mdb-server+} v3.6. When using v1.20+ of
99-
the library, all supported server versions default to the SCRAM-SHA-256 authentication
100-
mechanism.
121+
- ``username``: The username to authenticate. Percent-encode this value before including
122+
it in a connection URI.
123+
- ``password``: The password to authenticate. Percent-encode this value before including
124+
it in a connection URI.
125+
- ``authSource``: The MongoDB database to authenticate against. By default, the
126+
{+php-library+} authenticates against the database in the connection
127+
URI, if you include one. If you don't, it authenticates against the ``admin`` database.
128+
- ``authMechanism``: Set to ``'SCRAM-SHA-1'``.
129+
130+
You can set these options in two ways: by passing an options array to the
131+
``MongoDB\Client`` constructor or through parameters in your connection URI.
132+
Select the :guilabel:`MongoDB\\Client` or :guilabel:`Connection URI` tab to
133+
see the corresponding code:
134+
135+
.. tabs::
136+
137+
.. tab:: MongoDB\\Client
138+
:tabid: Client
139+
140+
.. literalinclude:: /includes/authentication.php
141+
:language: php
142+
:dedent:
143+
:start-after: start-scram-sha-1-client
144+
:end-before: end-scram-sha-1-client
101145

102-
To authenticate with this mechanism, use the same syntax as the :ref:`php-scram-sha-256`,
103-
but change the value of the ``authMechanism`` option to ``'SCRAM-SHA-1'``.
146+
.. tab:: Connection URI
147+
:tabid: connectionstring
148+
149+
.. literalinclude:: /includes/authentication.php
150+
:language: php
151+
:dedent:
152+
:start-after: start-scram-sha-1-uri
153+
:end-before: end-scram-sha-1-uri
104154

105155
Additional Information
106156
----------------------

0 commit comments

Comments
 (0)