Skip to content

Commit 8cbc43f

Browse files
authored
fix(server): include auth challenge on typed 401 (#26455)
1 parent 82359c4 commit 8cbc43f

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

packages/opencode/src/server/routes/instance/httpapi/middleware/authorization.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ServerAuth } from "@/server/auth"
22
import { Effect, Encoding, Layer, Redacted } from "effect"
3-
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
3+
import { HttpEffect, HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
44
import { HttpApiError, HttpApiMiddleware } from "effect/unstable/httpapi"
55
import { hasPtyConnectTicketURL } from "@/server/shared/pty-ticket"
66
import { isPublicUIPath } from "@/server/shared/public-ui"
@@ -33,7 +33,12 @@ function validateCredential<A, E, R>(
3333
) {
3434
return Effect.gen(function* () {
3535
if (!ServerAuth.required(config)) return yield* effect
36-
if (!ServerAuth.authorized(credential, config)) return yield* new HttpApiError.Unauthorized({})
36+
if (!ServerAuth.authorized(credential, config)) {
37+
yield* HttpEffect.appendPreResponseHandler((_request, response) =>
38+
Effect.succeed(HttpServerResponse.setHeader(response, "www-authenticate", WWW_AUTHENTICATE)),
39+
)
40+
return yield* new HttpApiError.Unauthorized({})
41+
}
3742
return yield* effect
3843
})
3944
}

packages/opencode/test/server/httpapi-authorization.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ describe("HttpApi authorization middleware", () => {
7272
)
7373

7474
expect(missing.status).toBe(401)
75+
expect(missing.headers["www-authenticate"] ?? "").toContain("Basic")
7576
expect(badPassword.status).toBe(401)
77+
expect(badPassword.headers["www-authenticate"] ?? "").toContain("Basic")
7678
expect(good.status).toBe(200)
7779
}),
7880
)

0 commit comments

Comments
 (0)