Skip to content

Auth

auth

Authenticates AI coding agents via the vendor's own login or an API key.

Three public entry points:

  • authenticate(project_id, provider, *, mounts_dir, image) — dispatches based on the provider's modes field: prompts for an API key (no container) or launches an auth container with the vendor CLI.
  • store_api_key(provider, api_key) — stores an API key directly in the credential DB (non-interactive fast path for CI).
  • load_auth_providers() — loads the current authentication providers for long-running frontends.

AUTH_PROVIDERS is a compatibility snapshot populated from the YAML roster at package load time. Use load_auth_providers() to read the current provider configuration in a long-running process.

AUTH_PROVIDERS = {} module-attribute

Initial auth-provider snapshot, keyed by name.

Use load_auth_providers when provider configuration can change during the process lifetime.

AuthProvider(name, label, host_dir_name, container_mount, command, banner_hint, extra_run_args=tuple(), modes=('api_key',), device_auth=False, api_key_hint='', post_capture_state=dict(), credential_provider='') dataclass

Describes how to authenticate one tool/agent.

name instance-attribute

Short key used in CLI and TUI dispatch (e.g. "codex").

label instance-attribute

Human-readable display name (e.g. "Codex").

host_dir_name instance-attribute

Single-segment directory name under mounts_dir() (e.g. "_codex-config").

container_mount instance-attribute

Mount point inside the container (e.g. "/home/dev/.codex").

command instance-attribute

Command to execute inside the container (login mode only).

banner_hint instance-attribute

Provider-specific help text shown before the container runs.

extra_run_args = field(default_factory=tuple) class-attribute instance-attribute

Additional podman run arguments (e.g. port forwarding).

modes = ('api_key',) class-attribute instance-attribute

Supported auth modes: "login" (the vendor's own login, run in a container), "api_key" (fast path).

device_auth = False class-attribute instance-attribute

Whether the vendor login has a headless device-code variant.

When set, the auth prompt offers a device-code method alongside the normal login: it runs <command> --device-auth with the normal flow's port-forwarding (extra_run_args) dropped — the device-code flow polls a remote endpoint and needs no localhost callback, so it works where the operator can't complete the forwarded-port login.

api_key_hint = '' class-attribute instance-attribute

Hint shown when prompting for an API key (URL to get one).

post_capture_state = field(default_factory=dict) class-attribute instance-attribute

JSON state files to write after credential capture.

Maps filename → key-value dict to merge into a JSON file in the auth mount directory. Example: {".claude.json": {"hasCompletedOnboarding": true}} marks Claude Code onboarding as complete so the first-run wizard is skipped.

credential_provider = '' class-attribute instance-attribute

Provider name the captured credential is stored under (e.g. "anthropic").

terok-executor auth claude authenticates Claude's default provider, so the credential is keyed under anthropic — matching the vault route and the v3-migrated DB. Populated by the roster loader from the agent's provider.default binding; empty falls back to the entry's own name.

supports_login property

Whether this provider supports the vendor login (run in a container).

supports_api_key property

Whether this provider supports direct API key entry.

supports_device_auth property

Whether this provider offers a headless device-code login variant.

__post_init__()

Validate fields that become filesystem paths.

Source code in src/terok_executor/credentials/auth.py
def __post_init__(self) -> None:
    """Validate fields that become filesystem paths."""
    p = Path(self.host_dir_name)
    if p.is_absolute() or ".." in p.parts or len(p.parts) != 1:
        raise ValueError(
            f"host_dir_name must be a single directory segment, got {self.host_dir_name!r}"
        )

AuthKeyConfig(label, key_url, env_var, config_path, printf_template, tool_name) dataclass

Describes how to prompt for and store an API key.

label instance-attribute

Human name shown in the prompt (e.g. "Claude").

key_url instance-attribute

URL where the user can obtain the key.

env_var instance-attribute

Name shown in the read -p prompt (e.g. "ANTHROPIC_API_KEY").

config_path instance-attribute

Destination inside the container (e.g. "~/.claude/config.json").

printf_template instance-attribute

printf format string (e.g. '{"api_key": "%s"}').

tool_name instance-attribute

Name shown in the success message (e.g. "claude").

Authenticator(provider) dataclass

Vendor-credential acquisition for a single agent.

Wraps the authenticate flow behind a stable class so callers that orchestrate a multi-step setup (terok project init, the standalone terok-executor auth command, the TUI auth flow) talk to one named surface bound to self.provider.

The discovery counterparts (list_authenticated_agents, scan_leaked_credentials) stay as module-level fns in their owning submodules — folding them in here would create a tach cycle through terok_executor.acp and terok_executor.credentials.vault_commands, which already depend on this module transitively.

provider instance-attribute

Auth provider name (e.g. "claude").

run(project_id, *, mounts_dir, image=None, expose_token=False, login_enabled=True, credential_set='default', device_auth=False)

Run the auth flow for self.provider; see module-level docs.

Mirrors the parameters of the underlying authenticate free function — instance-bound self.provider replaces the old positional provider arg. The device-code login is normally offered as a method in that flow's prompt; pass device_auth to skip the prompt and force it (the headless --device-auth path).

Source code in src/terok_executor/credentials/auth.py
def run(
    self,
    project_id: str | None,
    *,
    mounts_dir: Path,
    image: str | Callable[[], str] | None = None,
    expose_token: bool = False,
    login_enabled: bool = True,
    credential_set: str = "default",
    device_auth: bool = False,
) -> None:
    """Run the auth flow for ``self.provider``; see module-level docs.

    Mirrors the parameters of the underlying ``authenticate`` free
    function — instance-bound ``self.provider`` replaces the old
    positional ``provider`` arg.  The device-code login is normally
    offered as a method in that flow's prompt; pass *device_auth* to
    skip the prompt and force it (the headless ``--device-auth`` path).
    """
    authenticate(
        project_id,
        self.provider,
        mounts_dir=mounts_dir,
        image=image,
        expose_token=expose_token,
        login_enabled=login_enabled,
        credential_set=credential_set,
        device_auth=device_auth,
    )

prepare_login(project_id, *, mounts_dir, image, expose_token=False, credential_set='default', device_auth=False)

Build an AuthSession without running it.

Frontends that own their own UI loop (e.g. the terok Textual TUI, which wants to dispatch the login container into a new terminal tab or via tmux instead of inline) build the session here, run session.argv however they like, then call session.capture() on success. The CLI's blocking authenticate path is just another such caller — see _run_auth_container.

With device_auth, the session runs the provider's headless device-code login (see prepare_login_session).

Source code in src/terok_executor/credentials/auth.py
def prepare_login(
    self,
    project_id: str | None,
    *,
    mounts_dir: Path,
    image: str,
    expose_token: bool = False,
    credential_set: str = "default",
    device_auth: bool = False,
) -> AuthSession:
    """Build an [`AuthSession`][terok_executor.AuthSession] without running it.

    Frontends that own their own UI loop (e.g. the terok Textual TUI,
    which wants to dispatch the login container into a new terminal
    tab or via tmux instead of inline) build the session here, run
    ``session.argv`` however they like, then call ``session.capture()``
    on success.  The CLI's blocking ``authenticate`` path is just
    another such caller — see ``_run_auth_container``.

    With *device_auth*, the session runs the provider's headless
    device-code login (see [`prepare_login_session`][terok_executor.prepare_login_session]).
    """
    providers, route_refresher = _load_auth_context()
    info = providers.get(self.provider)
    if not info:
        available = ", ".join(providers)
        raise SystemExit(f"Unknown auth provider: {self.provider}. Available: {available}")
    if not info.supports_login:
        raise SystemExit(
            f"Provider {self.provider!r} has no vendor login — use store_api_key() instead."
        )
    if device_auth and not info.supports_device_auth:
        raise SystemExit(f"Provider {self.provider!r} has no device-auth login.")
    return _prepare_login_session(
        info,
        project_id,
        mounts_dir=mounts_dir,
        image=image,
        expose_token=expose_token,
        credential_set=credential_set,
        device_auth=device_auth,
        route_refresher=route_refresher,
    )

AuthSession(provider, project_id, container_name, argv, banner, auth_dir, mounts_dir, credential_set='default', expose_token=False, _tmpdir=None) dataclass

A prepared-but-not-run login container session.

Built by Authenticator.prepare_login (or the module-level prepare_login_session helper). Hold-don't-call: the caller is responsible for running argv (synchronously, in a new terminal tab, suspended TUI, etc.) and calling capture() afterwards. Use as a context manager so the temp dir and any dangling container are cleaned up on exit.

provider instance-attribute

Provider descriptor (label, banner hint, mount points).

project_id instance-attribute

Project scope for the banner; None for host-wide auth.

container_name instance-attribute

Podman container name (used for cleanup and -it log clarity).

argv instance-attribute

The podman run … command line — run this however you like.

banner instance-attribute

Banner text to display before launching argv.

auth_dir instance-attribute

Temp dir bind-mounted as the container's auth config target.

Lives until cleanup() (or __exit__). Credential extraction in capture() reads from here, so don't remove it manually.

mounts_dir instance-attribute

Base directory for the shared post-capture mount (OAuth providers only).

credential_set = 'default' class-attribute instance-attribute

Which credential set in the vault DB receives the captured token.

expose_token = False class-attribute instance-attribute

When True, real credential files are copied into the shared mount (tier 3).

title property

Short human-readable title ("Authenticating Claude (host-wide)").

capture()

Extract credentials from auth_dir, store them in the vault DB.

Call after argv exits successfully. Safe to call multiple times (the underlying extractor is idempotent on a stable credential file).

Source code in src/terok_executor/credentials/auth.py
def capture(self) -> None:
    """Extract credentials from ``auth_dir``, store them in the vault DB.

    Call after ``argv`` exits successfully.  Safe to call multiple
    times (the underlying extractor is idempotent on a stable
    credential file).
    """
    _capture_credentials(
        self.provider.name,
        self.auth_dir,
        self.credential_set,
        mounts_base=self.mounts_dir,
        auth_provider=self.provider,
        expose_token=self.expose_token,
        route_refresher=self._route_refresher,
    )

cleanup()

Release the temp dir and force-remove any lingering container.

Idempotent. Container removal is best-effort; local cleanup runs even when Podman cannot start. __exit__ calls this automatically.

Source code in src/terok_executor/credentials/auth.py
def cleanup(self) -> None:
    """Release the temp dir and force-remove any lingering container.

    Idempotent. Container removal is best-effort; local cleanup runs even
    when Podman cannot start. ``__exit__`` calls this automatically.
    """
    try:
        subprocess.run(
            [require_host_tool("podman"), "rm", "-f", self.container_name],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            check=False,
        )
    except OSError:
        pass
    finally:
        if self._tmpdir is not None:
            self._tmpdir.cleanup()
            self._tmpdir = None

__enter__()

Return self; the heavy lifting already happened in the factory.

Source code in src/terok_executor/credentials/auth.py
def __enter__(self) -> AuthSession:
    """Return self; the heavy lifting already happened in the factory."""
    return self

__exit__(*_exc)

Run cleanup on context-manager exit.

Source code in src/terok_executor/credentials/auth.py
def __exit__(self, *_exc: object) -> None:
    """Run ``cleanup`` on context-manager exit."""
    self.cleanup()

load_auth_providers()

Load auth providers from the current roster configuration.

This function reads one fresh roster snapshot. Authentication operations use the route publisher from the same snapshot.

Source code in src/terok_executor/credentials/auth.py
def load_auth_providers() -> dict[str, AuthProvider]:
    """Load auth providers from the current roster configuration.

    This function reads one fresh roster snapshot. Authentication operations
    use the route publisher from the same snapshot.
    """
    providers, _ = _load_auth_context()
    return dict(providers)

authenticate(project_id, provider, *, mounts_dir, image=None, expose_token=False, login_enabled=True, credential_set='default', device_auth=False)

Run the auth flow for provider, optionally scoped to a project.

Offers every auth method the provider supports — the vendor login (the vendor CLI in a container), its device-code variant (for providers that declare device_auth), and a direct API key — as the effective mode set: what the roster declares (modes:, device_auth) intersected with what the caller permits via login_enabled. One method runs straight through; two or more prompt the user to choose.

Pass device_auth to force the device-code variant non-interactively (the headless --device-auth path): the menu is skipped and the device-code login runs straight through. Raises SystemExit if the provider has no device-code login or the login is gated off.

Parameters:

Name Type Description Default
project_id str | None

Project identifier used for container naming and the banner line. Pass None for host-wide auth — the banner drops the project reference and the container gets a neutral host-auth-<provider> name.

required
provider str

Auth provider name (e.g. "claude").

required
mounts_dir Path

Base directory for shared config bind-mounts.

required
image str | Callable[[], str] | None

Container image for the login container. Either a tag string (eager) or a zero-arg callable returning the tag (lazy — invoked only when the user actually chooses the login). None is fine for API-key-only providers, where no container is launched. Use the lazy form to avoid paying the L1 build cost when the user might pick API key from the login-or-API-key prompt.

None
expose_token bool

When True, copy the real credential files into the shared mount instead of writing a phantom marker. Used by tier 3 (expose_oauth_token) where containers need the actual token.

False
login_enabled bool

External gate for the login. True (default) means the roster's modes list is honored verbatim. False instructs the function to skip the login and go straight to the API-key flow regardless of what the roster declares — terok passes False for providers whose login requires unset config flags (e.g. agent.codex.allow_oauth=true plus experimental: true). When the provider declares only the login and the gate is closed, raises SystemExit with a clear hint.

True
credential_set str

Storage namespace in the vault DB. Defaults to "default" — the shared host-wide bucket every standalone and pre-existing terok caller uses. Per-project callers pass a project-specific value (e.g. project.id) to keep each project's tokens isolated. The DB schema keys on (credential_set, provider), so two projects can hold independent logins for the same provider side-by-side.

'default'

Raises SystemExit if the provider name is unknown or no usable auth mode remains after gating.

Source code in src/terok_executor/credentials/auth.py
def authenticate(
    project_id: str | None,
    provider: str,
    *,
    mounts_dir: Path,
    image: str | Callable[[], str] | None = None,
    expose_token: bool = False,
    login_enabled: bool = True,
    credential_set: str = "default",
    device_auth: bool = False,
) -> None:
    """Run the auth flow for *provider*, optionally scoped to a project.

    Offers every auth method the provider supports — the vendor login (the
    vendor CLI in a container), its device-code variant (for providers that
    declare ``device_auth``), and a direct API key — as the *effective* mode set: what
    the roster declares (``modes:``, ``device_auth``) intersected with what the
    caller permits via *login_enabled*.  One method runs straight through; two or
    more prompt the user to choose.

    Pass *device_auth* to force the device-code variant non-interactively
    (the headless ``--device-auth`` path): the menu is skipped and the
    device-code login runs straight through.  Raises ``SystemExit`` if the
    provider has no device-code login or the login is gated off.

    Args:
        project_id: Project identifier used for container naming and the
            banner line.  Pass ``None`` for host-wide auth — the banner
            drops the project reference and the container gets a neutral
            ``host-auth-<provider>`` name.
        provider: Auth provider name (e.g. ``"claude"``).
        mounts_dir: Base directory for shared config bind-mounts.
        image: Container image for the login container.  Either a tag
            string (eager) or a zero-arg callable returning the tag
            (lazy — invoked only when the user actually chooses the
            login).  ``None`` is fine for API-key-only providers,
            where no container is launched.  Use the lazy form to avoid
            paying the L1 build cost when the user might pick API key
            from the login-or-API-key prompt.
        expose_token: When True, copy the real credential files into
            the shared mount instead of writing a phantom marker.  Used
            by tier 3 (``expose_oauth_token``) where containers need
            the actual token.
        login_enabled: External gate for the login.  ``True``
            (default) means the roster's ``modes`` list is honored
            verbatim.  ``False`` instructs the function to skip the
            login and go straight to the API-key flow regardless
            of what the roster declares — terok passes ``False`` for
            providers whose login requires unset config flags
            (e.g. ``agent.codex.allow_oauth=true`` plus ``experimental:
            true``).  When the provider declares only the login and the
            gate is closed, raises ``SystemExit`` with a clear hint.
        credential_set: Storage namespace in the vault DB.  Defaults to
            ``"default"`` — the shared host-wide bucket every standalone
            and pre-existing terok caller uses.  Per-project callers
            pass a project-specific value (e.g. ``project.id``) to
            keep each project's tokens isolated.  The DB schema keys on
            ``(credential_set, provider)``, so two projects can hold
            independent logins for the same provider side-by-side.

    Raises ``SystemExit`` if the provider name is unknown or no usable
    auth mode remains after gating.
    """
    providers, route_refresher = _load_auth_context()
    info = providers.get(provider)
    if not info:
        available = ", ".join(providers)
        raise SystemExit(f"Unknown auth provider: {provider}. Available: {available}")

    # Gating: a provider's roster may declare the login, but the deployment
    # may not allow it (terok's ``allow_oauth`` + ``experimental`` gate).
    has_login = info.supports_login and login_enabled
    has_api_key = info.supports_api_key

    # The login (and its device-code variant) runs the vendor CLI in a container;
    # the image is resolved lazily so the login-or-API-key prompt never pays the L1
    # build cost when the user picks the container-free API-key path.
    def _login(*, device: bool) -> None:
        _run_auth_container(
            project_id,
            info,
            mounts_dir=mounts_dir,
            image=_resolve_image(image, provider),
            expose_token=expose_token,
            credential_set=credential_set,
            device_auth=device,
            route_refresher=route_refresher,
        )

    def _api_key() -> None:
        _store_api_key(
            provider,
            _prompt_api_key(info),
            credential_set=credential_set,
            providers=providers,
            route_refresher=route_refresher,
        )

    methods: list[tuple[str, Callable[[], None]]] = []
    if has_login:
        methods.append(("Vendor login", lambda: _login(device=False)))
        if info.supports_device_auth:
            # Same credential, obtained via a device code the operator
            # enters on a second device instead of a forwarded callback port.
            methods.append(("Vendor login — device code", lambda: _login(device=True)))
    if has_api_key:
        methods.append(("API key", _api_key))

    if device_auth:
        # Headless force: bypass the chooser and run the device-code login
        # directly.  Reuses the login gate's hint when the path is closed.
        if not has_login:
            raise SystemExit(
                f"Device-code auth for {provider!r} needs the vendor login, which is "
                f"disabled by the caller's gating policy.  For terok this "
                f"typically means the experimental flag and/or the "
                f"provider-specific allow_oauth/expose_oauth_token config "
                f"keys are unset."
            )
        if not info.supports_device_auth:
            raise SystemExit(f"Provider {provider!r} has no device-code login.")
        _login(device=True)
        return

    if not methods:
        # Provider declares only the login and the caller's gate is closed.
        raise SystemExit(
            f"Auth for {provider!r} needs the vendor login, which is disabled by "
            f"the caller's gating policy.  For terok this typically means "
            f"the experimental flag and/or the provider-specific "
            f"allow_oauth/expose_oauth_token config keys are unset."
        )

    if len(methods) == 1:
        methods[0][1]()
        return

    print(f"Authenticate {info.label}:\n")
    for i, (label, _) in enumerate(methods, 1):
        print(f"  {i}. {label}")
    print()
    choice = input(f"Choose [1-{len(methods)}] (default 1): ").strip() or "1"
    try:
        index = int(choice) - 1
        if not 0 <= index < len(methods):
            raise ValueError
    except ValueError:
        raise SystemExit(f"Invalid choice: {choice!r}") from None
    methods[index][1]()

credential_provider(name)

Resolve an auth target (agent name) to the provider its credential is keyed under.

terok-executor auth claude authenticates Claude's default provider, so the credential is stored under anthropic — the same key the vault route and the v3-migrated DB use, so routed = stored & routes intersects at runtime. Tools resolve the same way (gh → github). Names not in the auth registry (harnesses with no credentials, or already a provider) pass through unchanged.

Reads the auth registry rather than the roster so the credentials layer stays below it — the resolved provider rides on each AuthProvider, populated by the loader.

Source code in src/terok_executor/credentials/auth.py
def credential_provider(name: str) -> str:
    """Resolve an auth target (agent name) to the provider its credential is keyed under.

    ``terok-executor auth claude`` authenticates Claude's *default provider*, so
    the credential is stored under ``anthropic`` — the same key the vault route
    and the v3-migrated DB use, so ``routed = stored & routes`` intersects at
    runtime.  Tools resolve the same way (``gh`` → ``github``).  Names not in the
    auth registry (harnesses with no credentials, or already a provider) pass
    through unchanged.

    Reads the auth registry rather than the roster so the credentials layer
    stays below it — the resolved provider rides on each
    [`AuthProvider`][terok_executor.credentials.auth.AuthProvider], populated by
    the loader.
    """
    info = AUTH_PROVIDERS.get(name)
    return info.credential_provider if (info and info.credential_provider) else name

store_api_key(provider, api_key, credential_set='default')

Store an API key in the credential database without a container.

This function supports non-interactive workflows and CI. It stores the key under the resolved provider name. See credential_provider. Then Terok writes the current provider routes to routes.json. Terok reports success only after it writes the routes. The next task can use a new provider without a separate command.

Source code in src/terok_executor/credentials/auth.py
def store_api_key(
    provider: str,
    api_key: str,
    credential_set: str = "default",
) -> None:
    """Store an API key in the credential database without a container.

    This function supports non-interactive workflows and CI. It stores the key
    under the resolved provider name. See
    [`credential_provider`][terok_executor.credentials.auth.credential_provider].
    Then Terok writes the current provider routes to ``routes.json``. Terok
    reports success only after it writes the routes. The next task can use a new
    provider without a separate command.
    """
    providers, route_refresher = _load_auth_context()
    _store_api_key(
        provider,
        api_key,
        credential_set=credential_set,
        providers=providers,
        route_refresher=route_refresher,
    )

prepare_login_session(provider, project_id, *, mounts_dir, image, expose_token=False, credential_set='default', device_auth=False)

Build an AuthSession without running it.

Creates a fresh temp dir, computes the podman run argv, and cleans up any leftover container of the same name (so re-auth after a previous abort isn't blocked). The caller drives execution and credential capture; see AuthSession.

The temp dir uses a clean slate so the vendor auth flow re-runs end to end — no stale config, no cached sessions.

With device_auth, the command gains --device-auth and the port-forwarding extra_run_args are dropped: the device-code flow polls a remote endpoint, so there is no localhost callback to forward.

Source code in src/terok_executor/credentials/auth.py
def prepare_login_session(
    provider: AuthProvider,
    project_id: str | None,
    *,
    mounts_dir: Path,
    image: str,
    expose_token: bool = False,
    credential_set: str = "default",
    device_auth: bool = False,
) -> AuthSession:
    """Build an [`AuthSession`][terok_executor.AuthSession] without running it.

    Creates a fresh temp dir, computes the ``podman run`` argv, and
    cleans up any leftover container of the same name (so re-auth
    after a previous abort isn't blocked).  The caller drives execution
    and credential capture; see [`AuthSession`][terok_executor.AuthSession].

    The temp dir uses a clean slate so the vendor auth flow re-runs end
    to end — no stale config, no cached sessions.

    With *device_auth*, the command gains ``--device-auth`` and the
    port-forwarding ``extra_run_args`` are dropped: the device-code flow
    polls a remote endpoint, so there is no localhost callback to forward.
    """
    _, route_refresher = _load_auth_context()
    return _prepare_login_session(
        provider,
        project_id,
        mounts_dir=mounts_dir,
        image=image,
        expose_token=expose_token,
        credential_set=credential_set,
        device_auth=device_auth,
        route_refresher=route_refresher,
    )

api_key_command(cfg)

Build a bash command that prompts for an API key and writes it to a config file.

Source code in src/terok_executor/credentials/auth.py
def api_key_command(cfg: AuthKeyConfig) -> list[str]:
    """Build a bash command that prompts for an API key and writes it to a config file."""
    config_dir = cfg.config_path.rsplit("/", 1)[0]
    parts = [
        f"echo 'Enter your {cfg.label} API key (get one at {cfg.key_url}):'",
        f"read -r -p '{cfg.env_var}=' api_key",
        f"mkdir -p {config_dir}",
        f"printf '{cfg.printf_template}\\n' \"$api_key\" > {cfg.config_path}",
        "echo",
        f"echo 'API key saved to {cfg.config_path}'",
        f"echo 'You can now use {cfg.tool_name} in task containers.'",
    ]
    return ["bash", "-c", " && ".join(parts)]