# Credential connection unification

One `CredentialData` replaces six payload classes. One `resolve_connection()` gives the wire layer everything a request needs, so a key can carry its own base URL. Design source: [credential connection unification](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/auth/credential-connection-unification.html).

```
@dataclass
class CredentialData:
    kind: str
    auth_value: str = ""            # what to send
    base_url: str = ""
    headers: dict = field(default_factory=dict)
    data: dict = field(default_factory=dict)   # refresh tokens, file paths, flow ids

@dataclass
class ResolvedConnection:
    kind: str
    auth_value: str
    base_url: str | None
    headers: dict
```

Stored flat as `{kind, auth_value, base_url, headers, data}`, with no `__type__`. `_payload_from_dict` accepts only this. `Credential.metadata` and display information stay on `Credential`.

## Resolution

| Credential | `resolve_connection` returns |
| --- | --- |
| `cli_delegated` | Reads its external file now, so the token is the freshest on disk |
| `credential_process` | Runs its helper, cached for `cache_seconds`; a failing helper raises `AuthCredentialProcessError` rather than letting another credential stand in |
| `sso` | Raises `AuthConfigError`: no login flow implements it |
| No auth value | `None`, and the caller falls back |
| Unsupported provider; Anthropic `oauth`, `device_code` or `cli_delegated`; any `sk-ant-oat` value | `None`. Anthropic needs an API key |

An empty `base_url` becomes `None`, separating "no endpoint" from "this endpoint". `acquire_pooled(provider_id, account_id=None)` returns `(ResolvedConnection, str, str) | None`. `_extract_token` remains as a thin reader over it.

At the wire layer the credential wins and the catalogue fills gaps:

```
base_url = (conn.base_url if conn and conn.base_url else None) or model.base_url
headers  = {**(opts.headers or {}), **(conn.headers if conn else {})}
```

OAuth detection reads `conn.kind`, not the token string.

## Stored-credential carry-over

| Part | Rule |
| --- | --- |
| What | Every old payload (a `__type__` class included) becomes the flat shape, and every account file of a provider joins its one list, `auth/<provider>/credentials.json` |
| When | Once, before the store first reads; no command runs it |
| Failure | The whole `auth` folder is copied aside first; a failed step leaves the old files as they were and the next start runs it again |
| Version | `CREDENTIAL_SCHEMA_VERSION` is 4 |

**Old class to new shape**

| Old test or field | Now |
| --- | --- |
| `isinstance(payload, ApiKeyPayload)` | `payload.kind == "api_key"` |
| `isinstance(payload, OAuthPayload)` | `payload.kind == "oauth"` |
| `isinstance(payload, (OAuthPayload, DeviceCodePayload))` | `payload.kind in ("oauth", "device_code")` |
| `isinstance(payload, DeviceCodePayload)` | `payload.kind == "device_code"` |
| `isinstance(payload, CliDelegatedPayload)` | `payload.kind == "cli_delegated"` |
| `isinstance(payload, ExternalProcessPayload)` | `payload.kind == "credential_process"` |
| `isinstance(payload, SsoPayload)` | `payload.kind == "sso"` |
| `payload.access_token`, `payload.api_key` | `payload.auth_value` |
| `payload.refresh_token` | `payload.data.get("refresh_token", "")` |
| `payload.expires_at_ms` | `payload.data.get("expires_at_ms", 0)` |
| Other fields (`store_path`, `client_id`, …) | `payload.data.get(...)` |

| Constructor | `CredentialData` |
| --- | --- |
| `ApiKeyPayload(api_key=K)` | `kind="api_key", auth_value=K` |
| `OAuthPayload(access_token=A, …)` | `kind="oauth", auth_value=A`; `data`: `refresh_token`, `expires_at_ms`, `scope`, `client_id`, `token_endpoint`, `id_token`, `extra` |
| `DeviceCodePayload(access_token=A, …)` | `kind="device_code", auth_value=A`; `data`: `refresh_token`, `expires_at_ms`, `device_code_flow_id`, `extra` |
| `CliDelegatedPayload(…)` | `kind="cli_delegated"`; `data`: `store_path`, `access_key_path`, `refresh_key_path`, `expires_key_path` |
| `ExternalProcessPayload(…)` | `kind="credential_process"`; `data`: `command`, `parses`, `json_key_path`, `cache_seconds` |

## Implementation status

| Part | File |
| --- | --- |
| `CredentialData`, schema version | `openprogram/models/auth/types.py` |
| `ResolvedConnection`, `resolve_connection` | `openprogram/models/auth/resolver.py` |
| Payload and layout carry-over | `openprogram/models/auth/carry_over.py`, run by `store.py` |
| CLI | `openprogram/interfaces/cli/commands/providers/keys.py` |

The carry-over was rehearsed on a store with the real store's names and file shapes and placeholder values.
