Sessions Deep Dive
AuthManager.session(...) is the bridge between stored credentials and the runtime Integrations container. It resolves which auth providers should run, gathers credentials for the active subject, calls bindings, and returns a container primed with provider settings.
Anatomy of a Session
from integrations.auth.auth_provider_key import AuthProviderKey
from integrations.auth import AuthManager
auth = AuthManager()
async with auth.session(
subject="user-123",
providers=[AuthProviderKey.GITHUB],
with_credentials={AuthProviderKey.GITHUB: {"access_token": "..."}},
auto_load_credentials=True,
overrides={"github": {"timeout": 30}},
) as integrations:
...
subjectidentifies the actor. It can be a string or any JSON-serializable mapping.providers(optional) filters which bindings run. When omitted, every registered binding executes.with_credentialslets you bypass the credential store for specific providers. Supply typedUserCredentialsor plain mappings and the provider will coerce them.auto_load_credentials(defaultTrue) controls whether the manager looks up stored credentials whenwith_credentialsis missing. Set it toFalsewhen handling ephemeral flows.overridesfeeds directly into theIntegrationsconstructor. This is ideal for swapping transport configs temporarily without building a new container.
Behind the scenes, each auth provider exposes a mapping of bindings keyed by AuthProviderKey. For every binding that matches the filter, the manager:
- Normalizes the provider key (string, enum, or alias).
- Parses manual credentials (if provided) into the provider’s
UserCredentialsmodel. - Loads stored credentials when allowed.
- Calls
binding.to_settings(...)with the manager, subject, and both credential models. - Injects the resulting
ProviderSettingsinto theIntegrationscontainer.
Manual Persistence
Sessions intentionally avoid writing to the credential store. They consume whatever you load or inject and then hand you the integrations container. When a flow issues new tokens, call store_credentials yourself before the session ends.
Nested Overrides
The integrations container also has its own overrides(...) context manager. Combine it with auth sessions to prototype changes quickly:
async with auth.session(subject="user-123") as integrations:
async with integrations.overrides(github={"retry_attempts": 0}):
await integrations.github.health_check()
Once the overrides exit, the original provider instances are restored but credentials remain available for future sessions.