Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/app/src/utils/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ describe("authTokenFromCredentials", () => {
test("encodes credentials with the default username", () => {
expect(authTokenFromCredentials({ password: "secret" })).toBe(btoa("opencode:secret"))
})

test("encodes unicode credentials as UTF-8", () => {
expect(authTokenFromCredentials({ password: "päss" })).toBe(
Buffer.from("opencode:päss", "utf8").toString("base64"),
)
})

test("round trips multibyte credentials", () => {
const token = authTokenFromCredentials({ username: "用户", password: "秘密🔐" })
expect(authFromToken(token)).toEqual({ username: "用户", password: "秘密🔐" })
})
})
7 changes: 6 additions & 1 deletion packages/app/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import type { ServerConnection } from "@/context/server"
import { decode64 } from "@/utils/base64"

export function authTokenFromCredentials(input: { username?: string; password: string }) {
return btoa(`${input.username ?? "opencode"}:${input.password}`)
return btoa(
Array.from(
new TextEncoder().encode(`${input.username ?? "opencode"}:${input.password}`),
(byte) => String.fromCharCode(byte),
).join(""),
)
}

export function authFromToken(token: string | null) {
Expand Down
Loading