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
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 @@ -16,6 +16,7 @@ sealed trait AuthType { self =>
AuthType { type ClientRequirement = ClientReq },
]

def asWWWAuthenticateHeader: Option[Header.WWWAuthenticate]
}

object AuthType {
Expand All @@ -27,28 +28,43 @@ object AuthType {
case object None extends AuthType {
type ClientRequirement = Unit
override val codec: HeaderCodec[Unit] = HttpCodec.empty.asInstanceOf[HeaderCodec[Unit]]

override def asWWWAuthenticateHeader: Option[Header.WWWAuthenticate] =
Option.empty
}

case object Basic extends AuthType {
type ClientRequirement = Header.Authorization.Basic
override val codec: HeaderCodec[Header.Authorization.Basic] =
HeaderCodec.basicAuth

override def asWWWAuthenticateHeader: Option[Header.WWWAuthenticate] =
Some(Header.WWWAuthenticate.Basic())
}
case object Bearer extends AuthType {
type ClientRequirement = Header.Authorization.Bearer
override val codec: HeaderCodec[Header.Authorization.Bearer] =
HeaderCodec.bearerAuth

override def asWWWAuthenticateHeader: Option[Header.WWWAuthenticate] =
Some(Header.WWWAuthenticate.Bearer(???))
}

case object Digest extends AuthType {
type ClientRequirement = Header.Authorization.Digest
override val codec: HeaderCodec[Header.Authorization.Digest] =
HeaderCodec.digestAuth

override def asWWWAuthenticateHeader: Option[Header.WWWAuthenticate] =
Some(Header.WWWAuthenticate.Digest(None))
}

final case class Custom[ClientReq](override val codec: HttpCodec[HttpCodecType.RequestType, ClientReq])
extends AuthType {
type ClientRequirement = ClientReq

override def asWWWAuthenticateHeader: Option[Header.WWWAuthenticate] =
Some(Header.WWWAuthenticate.Unknown(???, ???, ???))
}

final case class Or[ClientReq1, ClientReq2, ClientReq](
Expand All @@ -57,8 +73,10 @@ object AuthType {
alternator: Alternator.WithOut[ClientReq1, ClientReq2, ClientReq],
) extends AuthType {
type ClientRequirement = ClientReq
override val codec: HttpCodec[HttpCodecType.RequestType, ClientReq] =
override val codec: HttpCodec[HttpCodecType.RequestType, ClientReq] =
auth1.codec.orElseEither(auth2.codec)(alternator)
override def asWWWAuthenticateHeader: Option[Header.WWWAuthenticate] =
auth1.asWWWAuthenticateHeader
}

final case class ScopedAuth[ClientReq](
Expand All @@ -68,6 +86,9 @@ object AuthType {
type ClientRequirement = ClientReq
override val codec: HttpCodec[HttpCodecType.RequestType, ClientReq] = authType.codec

override def asWWWAuthenticateHeader: Option[Header.WWWAuthenticate] =
authType.asWWWAuthenticateHeader

def scopes: List[String] = _scopes

def scopes(newScopes: List[String]) = copy(_scopes = newScopes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,14 @@ 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.MissingHeaders(headerNames))
if headerNames.contains(Header.Authorization.name) =>
Handler.succeed(Response.unauthorized)
case Some(HttpCodecError.MissingHeader(headerName)) if headerName == Header.Authorization.name =>
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(HttpCodecError.DecodingErrorHeader(headerName, _)) if headerName == Header.Authorization.name =>
Handler.succeed(Response.unauthorized)
case _: Some[_] =>
Handler.fromFunctionZIO { (request: zio.http.Request) =>
val error = cause.defects.head.asInstanceOf[HttpCodecError]
val response = {
Expand All @@ -399,7 +406,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType](
}
ZIO.succeed(response)
}
case None =>
case None =>
Handler.failCause(cause)
}
}
Expand Down
Loading