Repository navigation
fix(server): honor OPENCODE_SERVER_USERNAME for serve and --server - #59379
Conversation
There was a problem hiding this comment.
Looks good. I ran serve from source with OPENCODE_SERVER_USERNAME=testuser OPENCODE_SERVER_PASSWORD=testpass. On the base, curl -u testuser:testpass gets a 401 and opencode:testpass is let in. With this PR it's the other way round, and session list --server with the same env works. The auth and server-connection tests pass. I couldn't run typecheck here because this Bun has no bun check.
Public API: the OPENCODE_SERVER_USERNAME env var is now honoured by opencode serve (default mode only) and by --server clients, with a default of opencode. ServerOptions gains an optional username, which the SDK's embedded CreateOptions also picks up; it has no effect there because embedded hosts have no password. The --server error text changes from "rejected the password" to "rejected the username or password". All of this is backwards compatible: without the env var nothing changes. Setups that set the variable but kept logging in as opencode will now need to use the configured username, which is what the docs describe.
Accepting pairing session tokens with any username is safe, because they're HMAC-signed with a key derived from the password. Keeping service, stdio and standalone modes on opencode matches their hardcoded clients. No blocking concerns.
Issue for this PR
Closes #45856
Type of change
What does this PR do?
V2 hardcoded the Basic auth username to
opencodeon both sides, soOPENCODE_SERVER_USERNAMEwas ignored even though the docs describe it. As a result,opencode serveturned away the credentials the user had configured.ServerOptionsgains an optionalusername, and the auth config uses it (defaultopencode). BothcreateRoutesand the process dispatcher, which handles requests before boot finishes, now read it.opencode:<token>, so a custom username would otherwise break existing pairing links. A plain password still has to come with the configured username.Env.usernamereadsOPENCODE_SERVER_USERNAME(defaultopencode).--serversends that username.opencode servepasses it to the server. Service and stdio/standalone modes keepopencode, because their clients (service registration, PTY handoff, standalone lease) always authenticate with the default.Not in this PR: the app and desktop "connect with password" forms still send
opencode. Fixing them needs a username field, persistence and i18n strings, so I'm leaving that for a follow-up.How did you verify your code works?
OPENCODE_SERVER_USERNAME=testuser OPENCODE_SERVER_PASSWORD=testpass opencode serve.curl -u testuser:testpasspasses auth,opencode:testpassgets 401, andsession list --serverwith the same env works.packages/server/test/auth.test.ts: a password needs the configured username, and a session token works with any username.packages/cli/test/server-connection.test.ts:--serversends the configured username.packages/serversuite and the relatedpackages/clitests pass, andbun typecheckpasses in both packages.Visual
Checklist
Added by the opencode bot (AI-generated).
Related: #49427, #46224
Public API: honours OPENCODE_SERVER_USERNAME in
serve(default mode) and--server; adds optionalusernameto ServerOptions (backwards compatible)