Search code examples
httpauthenticationbasic-authentication

Determine whether a Basic Authentication login was successful


How does an HTTP client definitively determine whether a login was successful when using Basic Authentication? I've always thought that it was simple - look at the response code:

  • 200 = Successful
  • 401 = Unsuccessful

This is not correct. If you read RFC 2617 it says:

If the origin server does not wish to accept the credentials sent with a request, it SHOULD return a 401 (Unauthorized) response. The response MUST include a WWW-Authenticate header field containing at least one (possibly new) challenge applicable to the requested resource.

The word SHOULD is tricky, and in fact I've seen devices that do not return a 401 on login failure. This would suggest:

  • WWW-Authenticate does not exist = Successful
  • WWW-Authenticate exists = Unsuccessful

This is also incorrect. I'm playing with a TP-LINK router that provides the following headers:

  - Server : Router Webserver
  - Connection : close
  - Content-Type : text/html
  - WWW-Authenticate : Basic realm="TP-LINK Wireless N Router WR841N"

The response code is 200.

Here we have a 200 with a WWW-Authenticate!!!!!

Given all of this, what is the DEFINITIVE way to determine whether a BA login was successful?


Solution

  • The 200 status is what counts. That the server keeps including WWW-Authenticate simply is a bug.

    And yes, I tested this, I happen to have the same router over here.