Credentials, tokens, and signing
Credentials, tokens, and signing give an asynchronous AWS client the identity and proof it needs for a service call. The session resolves an identity from explicit values or provider chains, refreshes temporary data when needed, and gives a request signer the frozen values required to authenticate each request.
Identity providers
The identity system separates long-lived credentials from temporary credentials and service access tokens. A credential resolver checks its providers in order and returns the first usable credential set. A token resolver supplies a separate authentication token when the selected service auth scheme needs one.
Credential chain
home/…/credentials.py builds an asynchronous credential chain that follows botocore provider order while replacing blocking work with async providers. Explicit client credentials take priority before this chain is used. The chain can use environment values, profiles, role exchange, web identity, SSO, login, credential processes, container metadata, and instance metadata.
The first provider that returns a value wins. A configured role can obtain source credentials from another provider or profile, then exchange them with STS. Role and web-identity results are deferred so the STS call can wait until credentials are needed. Each role-provider load resets its profile traversal state, so concurrent loads do not reuse another task's profile path.
References: aiobotocore/session.py• L250-279 aiobotocore/credentials.py• L83-193 aiobotocore/credentials.py• L933-1074 aiobotocore/credentials.py• L1254-1273 aiobotocore/credentials.py• L1328-1365
Token chain
The token chain checks scoped environment token data and then the SSO token provider. A token is an authentication input for a service auth scheme. It is not a replacement for the access key and secret key used by ordinary credential-based signing.
SSO role credentials can use this token chain to call the SSO service. The token provider loads cached SSO data and can exchange a refresh token for a new access token before it expires.
References: aiobotocore/tokens.py• L25-35 aiobotocore/credentials.py• L1276-1325 aiobotocore/credentials.py• L1328-1365
Refresh behavior
Refreshable identity objects delay network or process work until a value is needed. They return a stable, read-only snapshot to the signer. This keeps one request consistent while allowing later requests to use new temporary credentials or tokens.
Credential refresh
Refreshable credentials use an advisory window and a mandatory window. During the advisory window one task refreshes the value while other tasks can continue to use the current value. During the mandatory window callers wait for the refresh, and a refresh error is returned to the caller because the old value is no longer safe to use.
An async lock protects each refresh. A second check after lock acquisition prevents duplicate refresh work. If a non-mandatory refresh fails, the current credentials remain available. A successful refresh updates the frozen snapshot and must produce credentials that are not already expired. The lock uses the selected async runtime, so asyncio and AnyIO clients apply the same refresh rules without sharing blocking synchronization.
References: aiobotocore/credentials.py• L340-523
Token refresh
Deferred tokens use the same two-stage idea with a refresh lock and a next-refresh time. The cached SSO token is refreshed when it enters the refresh window. The provider saves a successful replacement token with its registration data so later loads can use the renewed state.
If the token registration has expired or lacks the data needed for refresh, the provider cannot renew the token. A failure during the advisory period is logged, but an expired token still raises a token retrieval error. A failure during the mandatory period is propagated to the caller.
References: aiobotocore/tokens.py• L38-95 aiobotocore/tokens.py• L104-191
Credential source refresh
Different providers refresh through their own source while exposing the same credential contract. Metadata providers call their metadata service again, credential-process providers run the configured process again, and role providers repeat the STS exchange. Cached role results avoid a new exchange while the cached result remains current.
SSO and login providers add a second dependency: their access token must remain usable before the AWS role credentials can be renewed. Login refresh also writes the new token data to its cache and maps service errors to clear login refresh failures.
References: aiobotocore/credentials.py• L526-588 aiobotocore/credentials.py• L641-782 aiobotocore/credentials.py• L1184-1243 aiobotocore/credentials.py• L1276-1365 aiobotocore/credentials.py• L1372-1500
Signer selection
The request signer turns service metadata and request context into a signature choice. It uses the client default unless operation context or an event hook supplies a more specific choice. Presigning adds a query or POST-specific suffix to the selected signature version.
Authentication scheme
The client creator determines the service authentication scheme from the service model. It passes the selected signature version, signing name, signing region, credentials, and optional auth token to the asynchronous request signer.
The signer gives operation context priority over client defaults for the auth type, signing name, and signing region. The choose-signer event can replace the choice. An unknown standard signature version raises its normal error; an unsupported non-standard presign choice is reported as an unsupported signature version.
References: aiobotocore/client.py• L42-110 aiobotocore/args.py• L59-68 aiobotocore/signers.py• L94-131
Credential and token selection
The signer selects credentials for schemes that need an AWS identity. It can use request-specific credentials, client credentials, or credentials from an identity cache. Before signing, it awaits a frozen credential snapshot.
Schemes that require an auth token follow a separate path. The signer awaits a refreshable token and passes the frozen token to the auth class. Unsigned schemes skip credential and token signing. A regional scheme still needs a resolved signing region.
References: aiobotocore/signers.py• L133-177 aiobotocore/credentials.py• L340-357 aiobotocore/tokens.py• L52-54
Request signing
Request signing is part of the request-created event flow. The client builds a request signer with the resolved identity and endpoint signing data, then the endpoint emits a request-created event before the request is sent. The signer chooses the auth scheme, allows a before-sign hook to adjust the request, and adds the resulting auth data.
This diagram shows how resolved identity and service context become an authenticated request.
flowchart LR
A[Session] --> B[Credential resolver]
A --> C[Token resolver]
B --> D[Credentials]
C --> E[Auth token]
F[Service model and endpoint] --> G[Signer context]
D --> H[Async request signer]
E --> H
G --> H
I[Operation request] --> J[request-created event]
H --> J
J --> K[Signed or unsigned HTTP request]
Request lifecycle
When a client operation reaches the transport boundary, the client has already built its request context and request dictionary. The endpoint creates the request and emits the service operation’s request-created event. The signer handler then signs the request before transport sends it.
The signer emits a service operation before-sign event before it creates the auth object. This gives service-specific or caller hooks a controlled point to change signing inputs. The final auth object adds headers or query data to the request.
References: aiobotocore/session.py• L276-331 aiobotocore/args.py• L59-68 aiobotocore/endpoint.py• L95-120 aiobotocore/signers.py• L20-92
Presigned requests
Presigning uses the same signer and identity rules but returns a request that carries its proof for later use. Standard presigned URLs use query signing. S3 presigned POST builds a policy and returns the upload URL with the fields needed for the form.
Database auth helpers use presigned requests as short-lived database tokens. RDS uses the rds-db signing name for its connect action. DSQL uses the selected connect action and the requested expiry. Invalid DSQL actions fail before a token is generated.
References: aiobotocore/signers.py• L182-201 aiobotocore/signers.py• L217-270 aiobotocore/signers.py• L273-350 aiobotocore/signers.py• L353-390 aiobotocore/signers.py• L397-473
Async runtime boundary
Credential and token providers keep network and process work off the blocking path used by synchronous botocore providers. The session selects asyncio or AnyIO primitives from the configured HTTP session, so locks and provider operations match the client’s async runtime.
The provider layer also adapts credential-process execution. It uses async subprocess support when available and a worker thread when the event loop cannot provide subprocess transports. This preserves async request progress without changing the credential-process contract.
References: aiobotocore/session.py• L61-83 aiobotocore/credentials.py• L116-129 aiobotocore/credentials.py• L360-366 aiobotocore/credentials.py• L634-761
Related pages
- Identity and configuration explains how identity, region data, endpoint data, and configuration meet during client preparation.
- Configuration, regions, and endpoint discovery explains how configuration and endpoint decisions provide the signing region and signing name used here.
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to provider order must preserve the first-match rule and the precedence of explicit credentials and explicitly selected profiles.
- Changes to refresh windows or lock behavior must preserve the difference between advisory refresh and mandatory refresh for both credentials and tokens.
- Changes to token or credential object types must preserve the frozen snapshot methods expected by the signer.
- Changes to service auth metadata or endpoint signing data must preserve the link between auth scheme, signing name, signing region, and presign suffix.
- Changes to request events must preserve signing after request creation and before transport sends the request.
- Changes to SSO or login token refresh must preserve cache updates and the error types that tell callers when a new login is required.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Explicit access key and secret key are not supplied together | Client creation raises a partial-credentials error | Supply both values or use the provider chain |
| No provider returns credentials for a credential-based auth scheme | The signer has no identity to freeze and cannot authenticate the request | Check the active environment, profile, role source, or metadata source |
| Temporary credentials fail during the mandatory refresh window | The request fails instead of using expired credentials | Keep the source service or process available and refresh before expiry |
| An SSO token lacks refresh data or its registration has expired | SSO role credentials cannot be renewed | Re-authenticate the SSO session and write a valid cached token |
| A token or credential refresh returns an already expired value | The refresh operation raises instead of installing unusable identity data | Verify source expiry fields and clock values |
| A regional signature has no resolved region | Signing raises a region error before transport | Set a session or client region and preserve endpoint signing data |
| A service selects an unknown signature version | Request signing fails before the request is sent | Keep service auth metadata and signer support aligned |
| A DSQL auth helper receives an unsupported action | Token generation raises parameter validation error | Use DbConnect or DbConnectAdmin |
Key files
| File | Why to read it |
|---|---|
| aiobotocore/credentials.py | Defines the async credential chain, role exchanges, source providers, caches, and refresh rules. |
| aiobotocore/tokens.py | Defines token provider selection and refreshable SSO token behavior. |
| aiobotocore/signers.py | Defines signer choice, frozen identity use, request authentication, and presigned auth helpers. |
| aiobotocore/session.py | Shows how the session resolves credentials and tokens and passes them into client creation. |
| aiobotocore/client.py | Shows how service auth metadata and client-specific tokens enter client construction. |
| aiobotocore/args.py | Shows how endpoint signing data and identity become the request signer. |
| aiobotocore/endpoint.py | Shows where request creation emits the event that drives signing before transport. |
| aiobotocore/hooks.py | Shows how async event handling preserves signer and presign integration with botocore hooks. |