Skip to content

Sign in to a Z.ai or BigModel account - #112

Merged
fylorn merged 1 commit into
mainfrom
feat/zai-login
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/zai-login

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Signing in to a Z.ai or BigModel account, the way the Z.ai client itself does it: the flow ends with an ordinary API key on the account, so what lands in config.yaml is a plain anthropic upstream with a key. No oauth block, no refresh, no rotation — tw_gateway::oauth is not involved at all.

The plan's quota follows the key: their own Coding Plan requests carry nothing but that key, and the documented endpoint for an Anthropic-compatible client is the same https://api.z.ai/api/anthropic this writes.

What it adds

  • POST /zai/login → {id, authorize_url, expires_in_secs}, GET /zai/login/{id}, DELETE /zai/login/{id}. One sign-in at a time; a new one voids the one still running.
  • The flow: ask their platform for an authorization address, poll it until the user has authorized, exchange that for an account token (Z.ai only — BigModel's business API takes the OAuth token itself, without a Bearer prefix), find the account's organization and project, reuse or create an API key named thinkwatch, read its secret half, write the upstream.
  • Signing in again replaces only the key. Outbound route, model scope, disabled state — everything the user set stays.

Three protocol facts worth stating

  • The authorization page belongs to their own client. Z.ai does not let third parties register an OAuth app, so the client_id in the address they hand us is theirs. The half we control is honest: our own User-Agent on every call, a polling token we generate ourselves, and no field anywhere in the request where a client could claim to be someone else. The UI has to say whose page the user is looking at.
  • The browser ends up on their page, not ours. The callback goes to their server, so there is no callback listener and no local port. Polling is the only way to learn the outcome.
  • Their business API answers HTTP 200 with a business error code in the body ({"code":401,...}, measured). Every call checks that code. Without it a failure reads as a success and the real reason resurfaces several steps later as something unrelated — there is a test for exactly that.

Side effect the UI must disclose

Getting the key creates one on the user's account when there is no key named thinkwatch yet; an existing one is reused. Not the name their client uses — a key's owner has to be recognizable in the user's own console.

Pricing, which was broken for these models anyway

GLM prices exist in the dataset only under a zai/ prefix, so a bare glm-5.2 resolved to nothing and every request through such an upstream had no cost at all. The prefixed key is now the last pricing candidate — after the exact name and the stripped ones, since a reseller's price is not the official one.

Also

user_agent() and ORIGINATOR move from the chatgpt module to the gateway root: they are what we say about ourselves to any account API, not something specific to Codex.

Tests

crates/tw-control/tests/zai.rs, against a fake Z.ai on loopback — the write, waiting through pending, the 200-with-a-business-error case, re-login keeping the user's settings, a name that belongs to another service being a conflict, BigModel's different auth and its key without a secret half, an unknown family or proxy refused before anything is started, and cancel/replace. cargo clippy --workspace --all-targets and cargo test --workspace are clean.

🤖 Generated with Claude Code

The sign-in ends with an ordinary API key on the account, so the upstream it
writes is a plain `anthropic` provider with a `key` — no `oauth` block, no
refresh, no rotation. Z.ai's own client works the same way: a Coding Plan
request carries nothing but that key, which is why the plan's quota follows
the key and a direct call to the documented endpoint is enough.

Three things about their protocol that the code has to know:

- The authorization page belongs to their own client, because Z.ai does not
  let third parties register an OAuth app. The half we control is honest: our
  own User-Agent, a polling token we generate ourselves, and no field in the
  request where a client could claim to be someone else.
- The browser lands on their page, not ours, so there is no callback server
  and no port to bind. Polling is the only way to learn the outcome.
- Their business API answers HTTP 200 with a business error code in the body.
  Every call checks that code; otherwise a failure reads as a success and the
  real reason surfaces several steps later as something unrelated.

Getting the key creates one on the account if there is none named
`thinkwatch` — an existing one is reused. The UI has to say so before the
sign-in starts.

Also: GLM prices exist in the dataset only under a `zai/` prefix, so a bare
`glm-5.2` resolved to nothing and every request through these upstreams had
no cost at all. The prefixed key is now the last pricing candidate.

`user_agent()` and `ORIGINATOR` move from the chatgpt module to the gateway
root, since they are what we say about ourselves to any account API.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn merged commit ab29e27 into main Sep 23, 2026
1 check passed
@fylorn
fylorn deleted the feat/zai-login branch September 23, 2026 04:22
fylorn added a commit to ThinkWatchProject/ThinkWatch-Lite that referenced this pull request Sep 23, 2026
A new entry in the service-type list, next to the ChatGPT account. The
sign-in ends with an ordinary API key on the account, so what it writes is a
plain anthropic upstream — everything after the sign-in is the same as any
other upstream.

Two differences from the ChatGPT dialog, both forced by their protocol:

- There is no code-on-another-device path. Their platform only offers the
  browser flow.
- The browser ends up on their page, not one of ours, so nothing hands the
  app back the focus and the dialog learns the outcome by polling.

The dialog says two things before the sign-in can start: the authorization
page belongs to their own client, because the service does not let third
parties register an authorization app; and signing in creates an API key
named thinkwatch on the account, or reuses the one already there.

Requires the core release that carries ThinkWatchProject/ThinkWatch-Core#112.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
fylorn added a commit to ThinkWatchProject/ThinkWatch-Lite that referenced this pull request Sep 23, 2026
A new entry in the service-type list, next to the ChatGPT account. The
sign-in ends with an ordinary API key on the account, so what it writes is a
plain anthropic upstream — everything after the sign-in is the same as any
other upstream.

Two differences from the ChatGPT dialog, both forced by their protocol:

- There is no code-on-another-device path. Their platform only offers the
  browser flow.
- The browser ends up on their page, not one of ours, so nothing hands the
  app back the focus and the dialog learns the outcome by polling.

The dialog says two things before the sign-in can start: the authorization
page belongs to their own client, because the service does not let third
parties register an authorization app; and signing in creates an API key
named thinkwatch on the account, or reuses the one already there.

Requires the core release that carries ThinkWatchProject/ThinkWatch-Core#112.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant