Skip to content
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

Missing Authorization header is reported as 400 instead of 401 #3235

Open
notxcain opened this issue Dec 5, 2024 · 9 comments
Open

Missing Authorization header is reported as 400 instead of 401 #3235

notxcain opened this issue Dec 5, 2024 · 9 comments
Labels
💎 Bounty bug Something isn't working

Comments

@notxcain
Copy link

notxcain commented Dec 5, 2024

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 notxcain added the bug Something isn't working label Dec 5, 2024
@weili96
Copy link

weili96 commented Dec 12, 2024

image
I also meet this bug !

@weili96
Copy link

weili96 commented Dec 18, 2024

image
content-type is also error!

@987Nabil
Copy link
Contributor

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

@weili96
Copy link

weili96 commented Dec 18, 2024

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
Copy link

weili96 commented Dec 18, 2024

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

@jdegoes
Copy link
Member

jdegoes commented Jan 22, 2025

/bounty $100

Copy link

algora-pbc bot commented Jan 22, 2025

💎 $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

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @notxcain Jan 22, 2025, 6:09:56 PM #3281

@notxcain
Copy link
Author

notxcain commented Jan 22, 2025

/attempt #3235

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

Copy link

algora-pbc bot commented Jan 22, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💎 Bounty bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants