To use Cognito's device tracking (Remembered Devices) feature, you must use the USER_SRP_AUTH authentication flow.
1. Retrieve Device Keys
When calling authenticate_user(), the returned tokens dictionary contains DeviceKey and DeviceGroupKey inside ['AuthenticationResult']['NewDeviceMetadata'].
2. Confirm a Device
Use confirm_device(tokens=tokens, DeviceName=...) to confirm the new device. This returns a response and a device_password. The device_password is required for subsequent device-based authentications.
3. Update Device Status
Use update_device_status(is_remembered, access_token, device_key) to set the device status.
is_remembered=True sets status to "remembered".is_remembered=False sets status to "not_remembered".
4. Authenticate using a known device
To authenticate a device that has already been confirmed, pass the device_key, device_group_key, and device_password directly into the AWSSRP constructor.
5. Forget a device
Use forget_device(access_token=..., device_key=...) to remove a device.
import boto3
from pycognito.aws_srp import AWSSRP
client = boto3.client('cognito-idp')
aws = AWSSRP(username='username', password='password', pool_id='user_pool_id',
client_id='client_id', client=client)
# 1. Get tokens and keys
tokens = aws.authenticate_user()
device_key = tokens["AuthenticationResult"]["NewDeviceMetadata"]["DeviceKey"]
device_group_key = tokens["AuthenticationResult"]["NewDeviceMetadata"]["DeviceGroupKey"]
# 2. Confirm device
response, device_password = aws.confirm_device(tokens=tokens)
# 3. Update status
aws.update_device_status(False, tokens["AuthenticationResult"]["AccessToken"], device_key)
# 4. Authenticate with device later
aws_device = AWSSRP(username='username', password='password', pool_id='user_pool_id',
client_id='client_id', client=client, device_key=device_key,
device_group_key=device_group_key, device_password=device_password)
tokens = aws_device.authenticate_user()
# 5. Forget device
aws.forget_device(access_token=tokens["AuthenticationResult"]["AccessToken"], device_key=device_key)