-
-
Notifications
You must be signed in to change notification settings - Fork 467
Make Endpoint respond with a 401 status code if the Authorization header is missing #3281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f065eaa
63cef82
2d509b1
7a2e227
6ac5b12
00f9cac
ddbb739
3f5b1d5
ad0fe12
1bf2221
93f33cc
3176433
eeeb709
a32d60c
c9da011
64c1ea2
113c497
142b508
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
|---|---|---|
|
|
@@ -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} | ||
|
|
||
| /** | ||
|
|
@@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The specs say, that we have to return acceptable auth methods.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| case Some(error) => | ||
notxcain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Handler.fromFunctionZIO { (request: zio.http.Request) => | ||
| val error = cause.defects.head.asInstanceOf[HttpCodecError] | ||
| val response = { | ||
| val outputMediaTypes = | ||
| ( | ||
|
|
@@ -355,7 +356,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Auth <: AuthType]( | |
| } | ||
| ZIO.succeed(response) | ||
| } | ||
| case None => | ||
| case None => | ||
| Handler.failCause(cause) | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.