Metadata-Version: 2.5
Name: credchain
Version: 0.1.0
Summary: Credential provider chain: read usernames, passwords and tokens from environment variables, the OS keychain or secret files, with automatic fallback.
Author-email: Stephan Eberle <stephaneberle9@gmail.com>
License-Expression: MIT
License-File: LICENSE
Requires-Python: >=3.10
Provides-Extra: keyring
Requires-Dist: keyring>=25.0.0; extra == 'keyring'
Description-Content-Type: text/markdown

# credchain <!-- omit in toc -->

Read the credentials an application needs — a username and password, or an
API token — from environment variables, the OS keychain or secret files, with
automatic fallback through a priority chain.

`credchain` has no framework dependency. FastMCP servers that also take
credentials from HTTP headers use
[fastmcp-creds](https://github.com/stephaneberle9/fastmcp-creds), which builds
on it.

- [Installation](#installation)
- [Usage](#usage)
  - [Credential provider chain](#credential-provider-chain)
  - [Environment variables](#environment-variables)
  - [OS keychain](#os-keychain)
  - [Secret files](#secret-files)
- [Logging](#logging)
- [API Reference](#api-reference)
  - [`CredentialsProvider`](#credentialsprovider)
  - [`CredentialsProviderChain`](#credentialsproviderchain)
  - [`EnvironmentCredentialsProvider`](#environmentcredentialsprovider)
  - [`KeyringCredentialsProvider`](#keyringcredentialsprovider)
  - [`FileCredentialsProvider`](#filecredentialsprovider)
- [Contributing](#contributing)
- [License](#license)

## Installation

The package is not published to PyPI yet. Install it from its Git repository
or a local checkout:

```bash
uv add "credchain @ git+https://github.com/stephaneberle9/python-credchain.git"
# with the OS keychain provider:
uv add "credchain[keyring] @ git+https://github.com/stephaneberle9/python-credchain.git"
```

Requires Python 3.10+.

## Usage

### Credential provider chain

Compose providers in priority order. The chain tries each in sequence and
returns the first result where both username and password (or token) are
non-empty.

```python
from credchain import CredentialsProviderChain, EnvironmentCredentialsProvider
from credchain.keyring import KeyringCredentialsProvider  # optional dep

chain = CredentialsProviderChain([
    EnvironmentCredentialsProvider("MY_USERNAME", "MY_PASSWORD"),
    KeyringCredentialsProvider("my-service"),
])

username, password = chain.get_credentials()
```

A provider that raises is logged (exception type only) and skipped.

### Environment variables

```python
from credchain import EnvironmentCredentialsProvider

provider = EnvironmentCredentialsProvider("MY_USERNAME", "MY_PASSWORD")
token_provider = EnvironmentCredentialsProvider.for_token("MY_API_TOKEN")
```

### OS keychain

Reads credentials stored in the OS keychain via
[`keyring`](https://pypi.org/project/keyring/). Requires `credchain[keyring]`.

```python
from credchain.keyring import KeyringCredentialsProvider

provider = KeyringCredentialsProvider("my-service")            # "username" + "password" entries
provider = KeyringCredentialsProvider.for_token("my-service")  # one "token" entry
provider = KeyringCredentialsProvider("my-service", timeout=10)
```

The keychain lookup is bounded by a timeout (default 5 seconds), so a locked
or unresponsive keychain, e.g. one waiting for an unlock prompt in a headless
session, cannot hang the caller. On timeout, a warning is logged and
`(None, None)` is returned, so a chain falls through to its next provider.

### Secret files

Reads one value per file, such as a secret mounted into a container. One
trailing line break is removed; a missing file means no credentials.

```python
from credchain import FileCredentialsProvider

provider = FileCredentialsProvider("/run/secrets/username", "/run/secrets/password")
token_provider = FileCredentialsProvider.for_token("/run/secrets/api-token")
```

## Logging

Providers log at debug level where they looked and whether they found
something, never a credential or any part of one: in token mode the token is
returned in both positions, so even a "username" can be a secret. Errors are
logged with their exception type only, because messages may quote values.

## API Reference

### `CredentialsProvider`

A `Protocol`: any object with a
`get_credentials() -> tuple[str | None, str | None]` method satisfies it.

### `CredentialsProviderChain`

`CredentialsProviderChain(providers)` — tries each provider in order and
returns the first result where both values are non-empty, else
`(None, None)`.

### `EnvironmentCredentialsProvider`

`EnvironmentCredentialsProvider(username_env_var_name, password_env_var_name)`
reads two environment variables;
`EnvironmentCredentialsProvider.for_token(token_env_var_name)` reads one and
returns it as both elements.

### `KeyringCredentialsProvider`

`KeyringCredentialsProvider(service, username_key="username", password_key="password", timeout=5.0)`
reads two keychain entries of `service`;
`KeyringCredentialsProvider.for_token(service, token_key="token", timeout=5.0)`
reads one. `timeout=None` waits indefinitely. Requires `credchain[keyring]`.

### `FileCredentialsProvider`

`FileCredentialsProvider(username_file, password_file)` reads two files;
`FileCredentialsProvider.for_token(token_file)` reads one. Errors other than a
missing file propagate.

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.

## License

MIT — see [LICENSE](LICENSE).
