Skip to content

stack-auth: make the profile one credential source for both the token and the client key #2025

Backlog

Description

@coderdan

Background

A Stack Encrypt cipher in Rust needs two things, named by two traits:

  • AuthStrategy (in stack-auth) produces the bearer token for each ZeroKMS request.
  • KeyProvider (in stack-kms) produces the client key.

The token and the key must be for the same workspace, or ZeroKMS refuses the request.

stash auth login saves a profile: the ~/.cipherstash directory, or CS_CONFIG_PATH. There are two files in it for each workspace the developer logged in to. auth.json is the login's access token and refresh token. secretkey.json is the client key. A marker records which workspace the CLI is on.

Problem

A developer who wants to "use my profile" for both halves has to build two unrelated objects, and nothing makes them read the same workspace:

StackCipher::builder()
    .auth_strategy(DeviceSessionStrategy::with_profile(store).build()?) // or AutoStrategy::detect()?
    .key_provider(ProfileKeyProvider::for_detected_workspace())
  1. Two names for one source. The token side is DeviceSessionStrategy, in packages/stack-auth/src/device_session_strategy.rs. The key side is ProfileKeyProvider, in packages/stack-kms/src/key_provider.rs. A developer has to learn that a "device session" is the profile's token.
  2. The workspace is chosen twice, in different ways. ProfileKeyProvider names a workspace with for_workspace(&crn) or for_detected_workspace(). DeviceSessionStrategy takes no CRN; it takes a ProfileStore the caller opened, for the profile root (with_profile) or for one workspace (with_workspace_store).
  3. Nothing checks that the two halves agree. A strategy for one workspace and a key for another compile, and fail only when ZeroKMS refuses the request.

Proposal

The profile itself is the credential source. One handle, scoped to one workspace, implements both traits.

  1. A Profile handle with two constructors:
    • Profile::detected() uses the workspace CS_WORKSPACE_CRN names, else the CLI's current workspace. This is how AutoStrategy and ProfileKeyProvider::for_detected_workspace() choose it now.
    • Profile::for_workspace(&crn) uses the workspace you name.
  2. It is the profile's AuthStrategy, with the profile's auth.json token, refreshed under the cross-process lock the CLI uses, with the background refresh of feat: refresh tokens in the background before they expire, with backoff after a failure #1989. It also implements KeyProvider, with the profile's secretkey.json. The handle carries the token cache and the refresh schedule. So it is a workspace handle, not the plain stack_profile::ProfileStore file store. That store has other uses.
  3. One builder call for both halves. .profile(&profile) sets the auth strategy and the key provider from the same handle, so the two are for one workspace by construction. .auth_strategy and .key_provider stay for mixed setups, such as OIDC federation with a key from the environment.
  4. Type state for the separate calls. Profile::detected() and Profile::for_workspace(&crn) return different types. The builder records where the strategy's workspace comes from: detected, named, or not a profile workspace (an access key or OIDC federation). key_provider accepts only a provider whose workspace source can match. A detected strategy with a named-workspace key does not compile. What the types cannot prove is that two CRN values are equal. Two separately named workspaces are still checked when the cipher is built, with an error that names both.
  5. AutoStrategy becomes the access key from CS_CLIENT_ACCESS_KEY, else the detected profile. StackCipher::with_detected_credentials() builds the same thing.
  6. DeviceSessionStrategy and its builder leave the public API. Two of its forms stay internal:
    • with_token, the in-memory form. The device-code login flow and tests use it.
    • with_workspace_store, the WASI guests' path. A guest uses it after it takes the auth.json lock.
  7. Docs and examples: stack-auth's and stack-encrypt's rustdoc, packages/stack-auth/examples/auto_strategy.rs, and packages/stack-encrypt/examples/zerokms_auth.rs. CHANGELOG entries under Breaking for stack-auth and stack-kms. stack-auth 0.43.0 on crates.io has DeviceSessionStrategy.

Open decisions:

  • Where the type lives: stack-profile, where the profile's files are read and written. stack-profile depends on no other stack crate. stack-auth depends on it, stack-kms depends on stack-auth, and stack-encrypt depends on both. So stack-profile cannot implement the traits itself without a dependency cycle. Each trait's own crate implements it for Profile instead. The orphan rule allows that: stack-auth implements AuthStrategy, and stack-kms implements KeyProvider.
  • Where the refresh state lives. The auth strategy needs a token cache and a refresh schedule (feat: refresh tokens in the background before they expire, with backoff after a failure #1989), and those types are defined in stack-auth. So a Profile defined in stack-profile cannot contain them. There are two ways to keep the state:
    • the builder wraps the profile in a stack-auth refresh engine, and the cipher owns that engine. Profile stays plain data, and the crates stay apart.
    • stack-auth keeps the state in an opaque field of Profile.
  • What ProfileKeyProvider becomes. Either it is removed, since Profile is the key provider, or it stays as a key-only provider. The second suits a developer who wants the profile's key with another strategy.
  • The names, including .profile(&profile) on the builder.

Relationship to other work

Activity

  1. added
    enhancement
    Rust
    on Nov 29, 2026
  2. coderdan commented on Nov 29, 2026

    @coderdan
    ContributorAuthor

    Decided: the TypeScript interface changes to match.

    These places expose the device session today:

    • @cipherstash/auth publishes DeviceSessionStrategy.fromProfile() (languages/typescript/packages/auth/index.d.ts, line 103), and its deprecated alias OAuthStrategy.
    • @cipherstash/stack re-exports DeviceSessionStrategy (languages/typescript/packages/stack/src/index.ts, lines 30 to 32).
    • The CLI's env command uses DeviceSessionStrategy.fromProfile() (languages/typescript/packages/cli/src/commands/env/index.ts, line 343).
    • skills/stash-auth/SKILL.md (line 165) lists DeviceSessionStrategy among the auth strategies. That skill ships to customers.

    What changes:

    1. A Profile in @cipherstash/auth, with Profile.detected() and Profile.forWorkspace(crn). It is accepted where an auth strategy is (config.authStrategy), and it supplies the client key from the same workspace.
    2. DeviceSessionStrategy and OAuthStrategy are deprecated first, then removed. Both packages are published, so a release has to pass between the two steps, with changesets for @cipherstash/auth and @cipherstash/stack.
    3. The CLI's env command moves to Profile.
    4. skills/stash-auth describes Profile in place of DeviceSessionStrategy, with a stash patch changeset, as AGENTS.md requires for a change to skills/.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RustSDKenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions