zio-http icon indicating copy to clipboard operation
zio-http copied to clipboard

Missing Authorization header is reported as 400 instead of 401

Open notxcain opened this issue 1 year ago • 11 comments

A missing Authorization header is reported with 400 instead of 401 status code when served by a route created using the declarative endpoint syntax.

To Reproduce

  1. Create a simple endpoint using Endpoint(PathCodec.Get).header(HeaderCodec.authorization).
  2. Send a request without Authorization header.
  3. Get a response with 400 status code.

Expected Behaviour

According to the RFC, in such case a response should have 401 Unauthorized code:

The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource.

notxcain avatar Dec 05 '24 14:12 notxcain

image I also meet this bug !

weili96 avatar Dec 12 '24 06:12 weili96

image content-type is also error!

weili96 avatar Dec 18 '24 09:12 weili96

@weili96 without the endpoint definition, Idk what the issue is. Also, if you find a new bug, please open a new issue

987Nabil avatar Dec 18 '24 12:12 987Nabil

private val editorViewArticleEndpoint =
  Endpoint((RoutePattern.POST / "article" / "editorView") ?? Doc.p("文章编辑页面-查看文章详情"))
    .auth(AuthType.Bearer)
    .header(HeaderCodec.authorization)
    .in[ArticleId]
    .out[ArticleInfo]
    .outErrors[ServerError](
      HttpCodec.error[BadRequestError](Status.BadRequest),
      HttpCodec.error[DbError](Status.InternalServerError)
    )
    .tag("Article")
private val editorViewArticleRoute = editorViewArticleEndpoint.implement(
  (authHeader: Authorization, input: ArticleId) =>
    ZIO.serviceWithZIO[ArticleApiHandler](_.editorViewArticle(input, Authorization.render(authHeader)))
)

@987Nabil when i send , without header of authorization

weili96 avatar Dec 18 '24 12:12 weili96

private val uploadImageEndpoint =
    Endpoint((RoutePattern.POST / "upload" / "image") ?? Doc.p("上传图片"))
      .auth(AuthType.Bearer)
      .header(HeaderCodec.authorization)
      .header(HeaderCodec.contentType)
      .inCodec(HttpCodec.binaryStream(MediaType.multipart.`form-data`))
      .out[ImageUrl]
      .outErrors[ServerError](
        HttpCodec.error[BadRequestError](Status.BadRequest),
        HttpCodec.error[DbError](Status.InternalServerError)
      )
      .tag("Tool")
  private val uploadImageRoute = uploadImageEndpoint.implement(
    (_, bytes) => {
      for
        _ <- ZIO.logInfo(bytes.toString)
        form <- Body.fromStreamChunked(bytes).asMultipartForm.mapError(e => BadRequestError(s"请求转换异常; ${e.toString}"))
        _ <- ZIO.logInfo(form.formData(1).name)
        res <- ZIO.serviceWithZIO[ToolApiHandler](_.uploadImage(form))
      yield res
    }
  )

this is also without contentType

weili96 avatar Dec 18 '24 12:12 weili96

/bounty $100

jdegoes avatar Jan 22 '25 16:01 jdegoes

💎 $100 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #3235 with your implementation plan
  2. Submit work: Create a pull request including /claim #3235 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

❗ Important guidelines:

  • To claim a bounty, you need to provide a short demo video of your changes in your pull request
  • If anything is unclear, ask for clarification before starting as this will help avoid potential rework
  • Low quality AI PRs will not receive review and will be closed
  • Do not ask to be assigned unless you've contributed before

Thank you for contributing to zio/zio-http!

Attempt Started (UTC) Solution Actions
🟢 @Skyiesac Aug 10, 2025, 12:19:22 PM #3614 Reward
🟢 @notxcain Jan 22, 2025, 06:09:56 PM #3281 Reward
🟢 @melmathari May 25, 2025, 11:48:54 PM #3512 Reward

algora-pbc[bot] avatar Jan 22 '25 16:01 algora-pbc[bot]

/attempt #3235

Algora profile Completed bounties Tech Active attempts Options
@notxcain 1 ZIO bounty
Scala, JavaScript,
Java & more
Cancel attempt

notxcain avatar Jan 22 '25 18:01 notxcain

💡 @notxcain submitted a pull request that claims the bounty. You can visit your bounty board to reward.

algora-pbc[bot] avatar Jan 22 '25 21:01 algora-pbc[bot]

/attempt #3235

melmathari avatar May 25 '25 23:05 melmathari

/attempt #3235

Skyiesac avatar Aug 10 '25 12:08 Skyiesac