Skip to content

Update auth.go #180

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

Closed
wants to merge 6 commits into from
Closed

Update auth.go #180

wants to merge 6 commits into from

Conversation

raphaeljlps
Copy link

Add Header WWW-Authenticate and removes http.BadRequest error

Hello guys, I was having a issue where when using the BasicAuth middleware, in the first request when the browser doesn't know that the URI is protected by Basic HTTP Auth scheme I was getting a Bad Request error, and the browser wasn't prompting me to put my User and Password.

So I look at the RFC 2617 and made these changes, now the browser knows that he needs to prompt the user asking his User and Password to resubmit the request.

raphaeljlps and others added 2 commits August 12, 2015 22:09
Add Header WWW-Authenticate and removes http.BadRequest error
@vishr
Copy link
Member

vishr commented Aug 13, 2015

@raphaeljlps Don't you think bad request is still valid? Can we keep it and still make it working?

@raphaeljlps
Copy link
Author

@vishr I will take a closer look and see if I can make it work with the bad request, unfortunately I won't be able to do this today.

@raphaeljlps
Copy link
Author

and I will make the tests pass 👍

}
}
}
}

c.Response().Header().Add("WWW-Authenticate", "Basic")
Copy link
Member

Choose a reason for hiding this comment

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

We would like header to be coming from a constant, look at echo.go. Same for "Basic". I see people also using realm, check on that too.

Copy link
Author

Choose a reason for hiding this comment

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

I will do, thanks for the notes.

@raphaeljlps
Copy link
Author

Okay @vishr I looked around and study the RFC a bit more and don't think we can maintain the BadRequest and make it work with it.

I made the changes that you suggest about using the basic constant.

@raphaeljlps
Copy link
Author

About adding the realm, thats where I have a problem, don't know from where we will get the realm value, do you have any suggestions @vishr ?

@@ -12,13 +12,13 @@ type (
)

const (
Basic = "Basic"
Basic = "Basic"
WWWAuthenticate = "WWW-Authenticate"
Copy link
Member

Choose a reason for hiding this comment

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

This should be moved to echo.go while other header constants.

@raphaeljlps
Copy link
Author

done @vishr

@vishr vishr closed this in 8aaf620 Sep 1, 2015
vishr added a commit that referenced this pull request Sep 1, 2015
@raphaeljlps raphaeljlps deleted the patch-1 branch September 1, 2015 18:34
@vishr
Copy link
Member

vishr commented Sep 1, 2015

Can you check if this works?

@raphaeljlps
Copy link
Author

I will take a look @vishr

@raphaeljlps
Copy link
Author

@vishr I'm getting unauthorized, but the browser is not asking for user/password
the browser is not getting the WWW-Authenticate header

@vishr vishr self-assigned this Sep 1, 2015
@vishr vishr added the bug label Sep 1, 2015
@raphaeljlps
Copy link
Author

@vishr I solved by the changing the location of the WWWAuthenticate header

        if len(auth) > l+1 && auth[:l] == Basic {
            b, err := base64.StdEncoding.DecodeString(auth[l+1:])
            if err == nil {
                cred := string(b)
                for i := 0; i < len(cred); i++ {
                    if cred[i] == ':' {
                        // Verify credentials
                        if fn(cred[:i], cred[i+1:]) {
                            return nil
                        }
                    }
                }
            }
        }
        c.Response().Header().Set(echo.WWWAuthenticate, Basic+" realm=Restricted")
        return echo.NewHTTPError(http.StatusUnauthorized)

@vishr
Copy link
Member

vishr commented Sep 1, 2015

That was it. I have changed in master. You can get the latest.

@raphaeljlps
Copy link
Author

it's working @vishr, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants