fix(server): include auth challenge on typed 401 (#26455)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { ServerAuth } from "@/server/auth"
|
import { ServerAuth } from "@/server/auth"
|
||||||
import { Effect, Encoding, Layer, Redacted } from "effect"
|
import { Effect, Encoding, Layer, Redacted } from "effect"
|
||||||
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
import { HttpEffect, HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
||||||
import { HttpApiError, HttpApiMiddleware } from "effect/unstable/httpapi"
|
import { HttpApiError, HttpApiMiddleware } from "effect/unstable/httpapi"
|
||||||
import { hasPtyConnectTicketURL } from "@/server/shared/pty-ticket"
|
import { hasPtyConnectTicketURL } from "@/server/shared/pty-ticket"
|
||||||
import { isPublicUIPath } from "@/server/shared/public-ui"
|
import { isPublicUIPath } from "@/server/shared/public-ui"
|
||||||
@@ -33,7 +33,12 @@ function validateCredential<A, E, R>(
|
|||||||
) {
|
) {
|
||||||
return Effect.gen(function* () {
|
return Effect.gen(function* () {
|
||||||
if (!ServerAuth.required(config)) return yield* effect
|
if (!ServerAuth.required(config)) return yield* effect
|
||||||
if (!ServerAuth.authorized(credential, config)) return yield* new HttpApiError.Unauthorized({})
|
if (!ServerAuth.authorized(credential, config)) {
|
||||||
|
yield* HttpEffect.appendPreResponseHandler((_request, response) =>
|
||||||
|
Effect.succeed(HttpServerResponse.setHeader(response, "www-authenticate", WWW_AUTHENTICATE)),
|
||||||
|
)
|
||||||
|
return yield* new HttpApiError.Unauthorized({})
|
||||||
|
}
|
||||||
return yield* effect
|
return yield* effect
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,7 +72,9 @@ describe("HttpApi authorization middleware", () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
expect(missing.status).toBe(401)
|
expect(missing.status).toBe(401)
|
||||||
|
expect(missing.headers["www-authenticate"] ?? "").toContain("Basic")
|
||||||
expect(badPassword.status).toBe(401)
|
expect(badPassword.status).toBe(401)
|
||||||
|
expect(badPassword.headers["www-authenticate"] ?? "").toContain("Basic")
|
||||||
expect(good.status).toBe(200)
|
expect(good.status).toBe(200)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user