PyMongo-Smart-Auth
About
This package extends PyMongo's MongoClient to provide built-in smart authentication. It simplifies authentication by:
- automatically authenticating when creating the connection instead of having to manually call
authenticateon the authentication database - if an authenticated client was created without passing any credentials, it authenticates by:
- looking for a
MONGO_CREDENTIAL_FILEenvironment variable and associated kwargs in the constructor to format it, if applicable - looking for a
MONGO_AUTHENTICATED_URIenvironment variable - looking for credentials in the
MONGO_AUTHENTICATION_DATABASE,MONGO_USERNAMEandMONGO_PASSWORDenvironment variables - looking for a
.mongo_credentialsfile in the user's home - looking for a
mongo_credentialsfile in the/etcfolder
- looking for a
It also allows the user to specify the path to another credentials file or pass credentials directly.
Installation
pip install pymongo-smart-auth
Usage
The MongoClient class from the PyMongo-Smart-Auth package is a drop-in replacement for PyMongo's MongoClient that simplifies authentication management.
The constructor works in the same way as the MongoClient constructor with four additional parameters, all optional:
username: the username to authenticate withpassword: the password to authenticate withcredentials_file: a file where credentials can be foundauthenticate: a boolean indicating whether the client should authenticate (defaults toTrue)
Credentials file
When using a credentials file, it should either have:
-
a single line with a fully authenticated URI
-
the authentication database on the first line, the user on the second and the password on the third. Empty lines are ignored. Example file:
admin administrator P4ssw0rd
Credential lookup order
Upon initialisation with the default authenticate=True, the client looks for credentials in the following order:
- The
usernameandpasswordparameters - The passed
credentials_file - The
MONGO_CREDENTIAL_FILEenvironment variable formatted with the kwargs of the constructor, if applicable - The
MONGO_AUTHENTICATED_URIenvironment variable - The
MONGO_AUTHENTICATION_DATABASE,MONGO_USERNAMEandMONGO_PASSWORDenvironment variables - The
.mongo_credentialsfile in the user's home - The
mongo_credentialsfile in the/etcfolder
Usage examples
from pymongo_smart_auth import MongoClient
# Explicit user and password
mongo1 = MongoClient(username='user', password='p4ssw0rd')
database1 = mongo1['database1'] # Automatically authenticated
# Explicit user and password with separate authentication database
mongo2 = MongoClient(username='user', password='p4ssw0rd', authentication_database='mongo_users')
database2 = mongo2['database2'] # Automatically authenticated
# Will read /some/path/mongo_credentials
mongo3 = MongoClient(credentials_file='/some/path/mongo_credentials')
database3 = mongo3['database3'] # Automatically authenticated
# Will read the file in the MONGO_CREDENTIAL_FILE environment variable if set,
# then the file in MONGO_CREDENTIAL_FILE environment variable if set,
# then the MONGO_AUTHENTICATION_DATABASE, MONGO_USERNAME and MONGO_PASSWORD environment variables if set,
# then ~/.mongo_credentials if it exists,
# otherwise /etc/mongo_credentials
mongo4 = MongoClient()
database4 = mongo4['database4'] # Automatically authenticated
# Will authenticate with a file defined in the environment with a keyword argument
# Assuming MONGO_CREDENTIAL_FILE=/etc/credentials_{group},
# this will authenticate with the credentials in /etc/credentials_my_group
mongo5 = MongoClient(group='my_group')
database5 = mongo6['my_group_database'] # Automatically authenticated
# Will not authenticate
mongo6 = MongoClient(authenticate=False)
database6 = mongo5['database5'] # Not authenticated
License
This project is licensed under the terms of the MIT license.
Release files for pymongo-smart-auth 2.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pymongo_smart_auth-2.1.3.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pymongo_smart_auth-2.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.8 kB
Release files / pymongo_smart_auth-2.1.3.tar.gz
| Download URL | pymongo_smart_auth-2.1.3.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
719a1210bd854f4f408a970e0b035f15e45449f2e872ba545eeaf91832499d3f
|
|
BLAKE2b-256 checksum How to use checksums |
b19cbf4f59429578c8fb1309b6525a78040d81c1343999ae0f741d3da26b3146
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.6
|
Release files / pymongo_smart_auth-2.1.3-py3-none-any.whl
| Download URL | pymongo_smart_auth-2.1.3-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9081af5dac8eeafb67acc130ef80dfc052fab17596c7e2aae17e246bbd234d9b
|
|
BLAKE2b-256 checksum How to use checksums |
a7a17e73b2f6301f55135c737680dfd43e919b49572644e366e599738136309a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.6
|