Question

I'm currently building a web application (utilizing Zend Framework) and have gotten some of the basic HTTP status codes down, such as:

  • 404 on missing controller/action
  • 500 on server exception
  • 3xx returned by Apache

At the moment I have just implemented some basic ACL checking that the user is authorized to access a certain resource. It's implemented as a controller plugin, doing its thing on the routeShutdown event. It works like follows:

  1. Get the role of the user. If the user is not logged in, assign the role of 'guest'
  2. Check that the user's rule has access to the resource

    2.1. If the does not have access to the resource and is a guest, save the resource he was trying to access, and forward him to a login prompt. Once he has supplied his credentials, he is redirected back to the original resource (via a HTTP redirect status code)

    2.2. If the user is authenticated and the ACL denies him access to the resource, he is forwarded to the error controller, to an action I've called noPrivilegies.

  3. If the user does have access, let the request continue as usual.

Now, my questions:

  1. Can/should I use HTTP 401 for the 2.1 scenario? It's not like I want a WWW-Authenticate header field sent by the client. I just need him to login through my login-form. But I still think that the request wasn't a 200 OK, since he didn't get access to the requested resource.
  2. Can/should I use HTTP 401 for the 2.2 scenario? Same reason here, WWW-Authenticate won't help. Perhaps it's better to use HTTP 403 Forbidden?
  3. Do you recommend any other status codes for these two scenarios?
  4. What other status codes do you usually return from your application, and when are they applicable?
Was it helpful?

Solution

  1. Provided that the guest role may be allowed to perform some actions, then no 4xx response or other form of error is warranted at stage 2.1 of your process because not being logged in is not an error.

  2. Neither 401 nor 403 is really ideal for an application-level "permission denied", but weighing 401's "The response MUST include a WWW-Authenticate header field" against 403's "Authorization will not help", I would say (and most cases I've encountered to date seem to agree) that 401 is the appropriate response if you are requesting HTTP level authentication before proceeding and 403 is appropriate in all other cases. So probably 403 in your case.

OTHER TIPS

I think that 403 is the correct response. Per the HTTP spec, 401 is for cases where HTTP-level authentication is possible and required, 403 is for cases where the user does not have permission to access the resource. In the case of your app, the user has logged in, and it's unreasonable to expect that s/he will be able to use a different account.

Normally I don't ever use the status codes to report 'application' level authentication failures. That's generally reported via the app.

And it makes sense to do that; because they can access the 'page' from a 'http' sense; they just can't access your applications processing of that page, if that makes sense.

What I'm saying is generally you don't worry about the codes, or set them yourself at all, when returning data to the client. Leave it to the webserver and provide an 'abstracted' method of responding; i.e. the typical 'invalid username or password' page, etc.

Just my view, though.

i've recently implemented something that works in a similar way, we return a 401 response when a user has to login and a 403 if after login they've tried accessing something that they don't have permission to access.

you can also set the reason phrase in the http response to something to denote the reason for the failure.

Even not specifically stated in HTTP spec, the common practice is

401 - For authentication error 403 - For authorization error

Try not to use 401 if you don't want authenticate user. I recently encountered a problem with a HTTP client (not one of the popular browsers) that it logs an error when it sees 401 but not WWW-Authenticate header. Granted this is a bug on the client side but it shows how people perceives 401.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top