|
| 1 | +.. include:: /includes/authentication/kerberos-intro.rst |
| 2 | + |
| 3 | +Using GSSAPI Authentication in Your Application |
| 4 | +----------------------------------------------- |
| 5 | + |
| 6 | +To use GSSAPI authentication in your application, follow the steps below. |
| 7 | + |
| 8 | +1. Install the `winkerberos <https://pypi.python.org/pypi/winkerberos/>`__ module. |
| 9 | +#. Set the following connection options: |
| 10 | + |
| 11 | + - ``username``: The Kerberos principal to authenticate. Percent-encode this value before including |
| 12 | + it in a connection URI. |
| 13 | + - ``authMechanism``: Set to ``"GSSAPI"``. |
| 14 | + - ``password``: Optional. If the user to authenticate is different from the user |
| 15 | + that owns the application process, set this option to the authenticating user's |
| 16 | + password. |
| 17 | + - ``authMechanismProperties``: Optional. This option includes multiple |
| 18 | + authentication properties. To specify more than one of the following properties, |
| 19 | + use a comma-delimited list. |
| 20 | + |
| 21 | + - ``SERVICE_NAME``: By default, MongoDB uses ``mongodb`` as |
| 22 | + the authentication service name. Use this option to specify a different service name. |
| 23 | + - ``CANONICALIZE_HOST_NAME``: Whether to use the fully qualified domain name (FQDN) |
| 24 | + of the MongoDB host for the server principal. |
| 25 | + - ``SERVICE_REALM``: The service realm. Use this option when the user's |
| 26 | + realm is different from the service's realm. |
| 27 | + |
| 28 | +You can set these options in two ways: by passing arguments to the |
| 29 | +``MongoClient`` constructor or through parameters in your connection string. Select the tab that corresponds to your connection method to learn how |
| 30 | +to set connection options. |
| 31 | + |
| 32 | +.. tabs:: |
| 33 | + |
| 34 | + .. tab:: MongoClient |
| 35 | + :tabid: mongoclient |
| 36 | + |
| 37 | + .. code-block:: python |
| 38 | +
|
| 39 | + client = pymongo.MongoClient("mongodb://<hostname>:<port>", |
| 40 | + username="mongodbuser@EXAMPLE.COM", |
| 41 | + authMechanism="GSSAPI", |
| 42 | + password="<user password>", |
| 43 | + authMechanismProperties="SERVICE_NAME:<authentication service name>, |
| 44 | + CANONICALIZE_HOST_NAME:true, |
| 45 | + SERVICE_REALM:<service realm>") |
| 46 | +
|
| 47 | + .. tab:: Connection String |
| 48 | + :tabid: connectionstring |
| 49 | +
|
| 50 | + .. code-block:: python |
| 51 | +
|
| 52 | + uri = ("mongodb://mongodbuser%40EXAMPLE.COM:<percent-encoded user password>" |
| 53 | + "@<hostname>:<port>/?" |
| 54 | + "&authMechanism=GSSAPI" |
| 55 | + "&authMechanismProperties=" |
| 56 | + "SERVICE_NAME:<authentication service name>," |
| 57 | + "CANONICALIZE_HOST_NAME:true," |
| 58 | + "SERVICE_REALM:<service realm>") |
| 59 | + client = pymongo.MongoClient(uri) |
| 60 | + |
| 61 | + .. tab:: MongoClient (Asynchronous) |
| 62 | + :tabid: mongoclient-async |
| 63 | +
|
| 64 | + .. code-block:: python |
| 65 | +
|
| 66 | + client = pymongo.AsyncMongoClient("mongodb://<hostname>:<port>", |
| 67 | + username="mongodbuser@EXAMPLE.COM", |
| 68 | + authMechanism="GSSAPI", |
| 69 | + password="<user password>", |
| 70 | + authMechanismProperties="SERVICE_NAME:<authentication service name>, |
| 71 | + CANONICALIZE_HOST_NAME:true, |
| 72 | + SERVICE_REALM:<service realm>") |
| 73 | +
|
| 74 | + .. tab:: Connection String (Asynchronous) |
| 75 | + :tabid: connectionstring-async |
| 76 | +
|
| 77 | + .. code-block:: python |
| 78 | +
|
| 79 | + uri = ("mongodb://mongodbuser%40EXAMPLE.COM:<percent-encoded user password>" |
| 80 | + "@<hostname>:<port>/?" |
| 81 | + "&authMechanism=GSSAPI" |
| 82 | + "&authMechanismProperties=" |
| 83 | + "SERVICE_NAME:<authentication service name>," |
| 84 | + "CANONICALIZE_HOST_NAME:true," |
| 85 | + "SERVICE_REALM:<service realm>") |
| 86 | + client = pymongo.AsyncMongoClient(uri) |
| 87 | +
|
| 88 | +API Documentation |
| 89 | +----------------- |
| 90 | +
|
| 91 | +To learn more about using authentication mechanisms with {+driver-short+}, |
| 92 | +see the following API documentation: |
| 93 | +
|
| 94 | +- `MongoClient <{+api-root+}pymongo/mongo_client.html#pymongo.mongo_client.MongoClient>`__ |
0 commit comments