Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package zio.http.endpoint

import zio.ZIO
import zio.test._

import zio.http._
import zio.http.codec._

object UnauthorizedSpec extends ZIOSpecDefault {
override def spec =
suite("UnauthorizedSpec")(
test("should respond with 401 Unauthorized when required authorization header is missing") {
val endpoint = Endpoint(Method.GET / "test")
.header(HeaderCodec.authorization)
.out[Unit]
val route = endpoint.implement(_ => ZIO.unit)
val request =
Request(method = Method.GET, url = url"/test")
for {
response <- route.toRoutes.runZIO(request)
} yield assertTrue(Status.Unauthorized == response.status)
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ object HttpCodecError {
final case class MissingHeader(headerName: String) extends HttpCodecError {
def message = s"Missing header $headerName"
}
final case object MissingAuthorizationHeader extends HttpCodecError {
def message = "Missing header Authorization"
}
final case class MalformedMethod(expected: zio.http.Method, actual: zio.http.Method) extends HttpCodecError {
def message = s"Expected $expected but found $actual"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,10 @@ private[codec] object EncoderDecoder {
.getOrElse(throw HttpCodecError.MalformedHeader(codec.name, codec.textCodec))

case None =>
throw HttpCodecError.MissingHeader(codec.name)
if (codec.name == Header.Authorization.name)
throw HttpCodecError.MissingAuthorizationHeader
else
throw HttpCodecError.MissingHeader(codec.name)
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import zio.schema.Schema

import zio.http.Header.Accept.MediaTypeWithQFactor
import zio.http._
import zio.http.codec._
import zio.http.codec.{StatusCodec, _}
import zio.http.endpoint.Endpoint.{OutErrors, defaultMediaTypes}

/**
Expand Down Expand Up @@ -340,9 +340,10 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
case Some(HttpCodecError.CustomError("SchemaTransformationFailure", message))
if maybeUnauthedResponse.isDefined && message.endsWith(" auth required") =>
maybeUnauthedResponse.get
case Some(_) =>
case Some(HttpCodecError.MissingAuthorizationHeader) =>
Handler.succeed(Response.unauthorized)
Copy link
Author

@notxcain notxcain Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this should be reported the same way as any other missing header.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The specs say, that we have to return acceptable auth methods.
And we have that data from the Endpoint API

The server generating a 401 response MUST send
a WWW-Authenticate header field (Section 4.1) containing at least one
challenge applicable to the target resource.

Copy link
Author

@notxcain notxcain Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@987Nabil the AuthType has quite limited information, please see the implementation draft. Any piece of advice here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most things are optional and can be omitted. Digest nonce is generated anyway.
Basic Digest and OAuth need a realm. We can add this to AuthType in the same fashion like ScopedAuth.
In case there is no realm, we could return no header or a default value. I tend to a default value. Like "default" or "restricted"

case Some(error) =>
Handler.fromFunctionZIO { (request: zio.http.Request) =>
val error = cause.defects.head.asInstanceOf[HttpCodecError]
val response = {
val outputMediaTypes =
(
Expand All @@ -355,7 +356,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
}
ZIO.succeed(response)
}
case None =>
case None =>
Handler.failCause(cause)
}
}
Expand Down
Loading