Registry / aws / pycognito

pycognito

JSON →
library2024.5.1pypypi✓ verified 27d ago

pycognito is a Python library that provides a simplified interface for interacting with AWS Cognito User Pools, wrapping Boto3's Cognito client. It includes built-in support for the Secure Remote Password (SRP) protocol, making user authentication straightforward. The current version is 2024.5.1, and it typically releases new versions monthly or bi-monthly, incorporating fixes and feature enhancements.

pip install pycognito
INSTALL
IMPORT
SIG · PYCOGNITO
P
pycognito
awspythonv2024.5.1
Install
5.4s avg
Import
866ms
Disk
70MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2024.5.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.898s · 71.5MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 5.4s · import 0.834s · 72MB
70MB installed
● package 70MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Cognito
✓ from pycognito import Cognito

This quickstart demonstrates how to initialize the `Cognito` client and authenticate a user using their username and password. It automatically handles the SRP (Secure Remote Password) protocol. Ensure you replace the placeholder values for `USER_POOL_ID`, `CLIENT_ID`, `USERNAME`, and `PASSWORD` with your actual Cognito credentials, ideally loaded from environment variables.

import os from pycognito import Cognito # It's highly recommended to load these from environment variables or a secure configuration system. # Replace 'your_pool_id', 'your_client_id', 'your_username', 'YourStrongPassword123!' USER_POOL_ID = os.environ.get('COGNITO_USER_POOL_ID', 'us-east-1_your_pool_id') CLIENT_ID = os.environ.get('COGNITO_CLIENT_ID', 'your_client_id') USERNAME = os.environ.get('COGNITO_USERNAME', 'your_username') PASSWORD = os.environ.get('COGNITO_PASSWORD', 'YourStrongPassword123!') # Check if placeholder values are still present if 'your_pool_id' in USER_POOL_ID or 'your_client_id' in CLIENT_ID or\ USERNAME == 'your_username' or PASSWORD == 'YourStrongPassword123!': print("Please set COGNITO_USER_POOL_ID, COGNITO_CLIENT_ID, COGNITO_USERNAME, and COGNITO_PASSWORD environment variables or replace placeholders in the code.") else: try: # Initialize Cognito with User Pool ID, Client ID, and username c = Cognito(USER_POOL_ID, CLIENT_ID, username=USERNAME) # Authenticate the user. SRP (Secure Remote Password) protocol is handled automatically. c.authenticate(password=PASSWORD) print(f"Successfully authenticated user: {USERNAME}") print(f"Access Token (first 30 chars): {c.access_token[:30]}...") print(f"ID Token (first 30 chars): {c.id_token[:30]}...") # Refresh token is also available as c.refresh_token # Example: Get user attributes user_attributes = c.get_user_attributes() print(f"User attributes: {user_attributes}") except Exception as e: print(f"Authentication failed: {e}")
Debug
Known issues
breakingThe internal JWT validation library switched from `python-jose` to `PyJWT`. While the public API for token handling is largely stable, users who relied on specific internals or configurations of `python-jose` might encounter issues.
fix
Ensure `PyJWT` is installed (it's a dependency) and review any custom JWT processing code. Most direct usage of `pycognito` should remain unaffected.
affects: <2024.2.0
breakingThe minimum required Python version has been raised to 3.8. Users on older Python versions will experience installation or runtime failures.
fix
Upgrade your Python environment to version 3.8 or newer before installing or running `pycognito`.
affects: <2024.5.0
gotchaParameter changes and fixes for `admin_create_user` and `admin_reset_user_password` methods, particularly regarding `client_metadata` and temporary password generation, were introduced. Older implementations might not align with the current expected parameters or behavior.
fix
Review calls to `admin_create_user` and `admin_reset_user_password` in your application. Consult the latest documentation or GitHub examples for required parameters, especially `client_metadata` and handling of temporary passwords.
affects: <2022.11.0
gotchaWhile pycognito handles SRP, Multi-Factor Authentication (MFA) setup and interaction (e.g., `associate_srp_mfa`, `verify_mfa`) can be complex and is a common source of implementation errors if not handled precisely according to the Cognito flow. Though fixes were made, careful implementation is still required.
fix
Thoroughly test your MFA implementation. Follow the Cognito documentation and `pycognito` examples carefully for `associate_srp_mfa`, `verify_mfa`, and other MFA-related methods to ensure correct sequence and parameter handling.
affects: <2022.11.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pycognito'
The 'pycognito' package is not installed in the Python environment where the code is being executed.
fix
Install the pycognito library using pip: `pip install pycognito`
botocore.exceptions.NotAuthorizedException: An error occurred (NotAuthorizedException)
This error typically occurs when the authentication credentials are incorrect, the user's account is not confirmed, the application client is misconfigured (e.g., missing a client secret), or the provided tokens are invalid or expired.
fix
Verify the username and password, ensure the user is confirmed in Cognito, confirm the client secret is correctly used if required by your app client, and check that tokens are valid and not expired. For `pycognito.Cognito` initialization, ensure `client_secret` is provided if the app client requires it.
botocore.exceptions.UserNotFoundException: An error occurred (UserNotFoundException)
The requested user does not exist in the specified Cognito User Pool, or the user is not confirmed and the 'Prevent user existence errors' setting is disabled, or an incorrect alias (like email/phone) is used where a username is expected by an administrative API.
fix
Verify the user's existence and spelling of the username in the Cognito User Pool. If using aliases (email/phone), ensure the correct identifier is passed to the `pycognito` method, and consider checking if the user is confirmed. If 'Prevent user existence errors' is enabled on the app client, this error might be masked as `NotAuthorizedException`.
botocore.exceptions.InvalidParameterException: An error occurred (InvalidParameterException)
One or more parameters passed to the underlying AWS Cognito API call via `pycognito` do not meet the expected format, constraints, or are missing. This could be due to incorrect data types or missing required fields.
fix
Review the specific `pycognito` method call and its arguments against the AWS Cognito API documentation for the expected parameter types and formats. Ensure all required parameters are present and correctly formatted.
Upgrade
Version history
2024.5.1latest on PyPI · released May 16, 2024
Audit
Dependencies
boto3requiredCore dependency for interacting with AWS services, including Cognito.
PyJWTrequiredUsed for JWT validation; replaced `python-jose` in version 2024.2.0.
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources