Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I return a 401 or a 405 response code to a REST API user without sufficient access?

Tags:

I'm developing an API which will also have an authentication/authorization component.

Anybody, regardless of authentication status, will be able to write (POST), but depending on if you are unauthenticated, authenticated as a normal user or authenticated as an admin and what resource you are trying to access I'm going to return different responses for GET, DELETE and PUT.

I'm trying to figure out the most appropriate response code for a user who isn't authenticated and/or authorized.

Keep in mind http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html:

Unauthorized -> 401

Forbidden -> 403

Method Not Allowed -> 405

Let's use a specific examples:

  • John Doe is unauthenticated, on DELETE should he receive a 401 or a 405?
  • Amy is authenticated but not authorized, on DELETE should she receive a 403 or a 405?

(Keep in mind that even though John and Amy are forbidden or unauthorized that doesn't mean they arent able to access the same resource with a different HTTP VERB.)

Thanks.

like image 583
Chris W. Avatar asked Jun 26 '12 03:06

Chris W.


People also ask

When should a 405 error be returned?

The 405 Method Not Allowed error occurs when the web server is configured in a way that does not allow you to perform a specific action for a particular URL. It's an HTTP response status code that indicates that the request method is known by the server but is not supported by the target resource.

Should I return 401k or 403?

In summary, a 401 Unauthorized response should be used for missing or bad authentication, and a 403 Forbidden response should be used afterwards, when the user is authenticated but isn't authorized to perform the requested operation on the given resource.

When should I return my 401k?

401 Unauthorized is the status code to return when the client provides no credentials or invalid credentials. 403 Forbidden is the status code to return when a client has valid credentials but not enough privileges to perform an action on a resource.

Which of the following cases will lead to a 401 response status code?

A 401 error response indicates that the client tried to operate on a protected resource without providing the proper authorization. It may have provided the wrong credentials or none at all. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource.


1 Answers

In this case, I think providing some examples for clarification are useful:

  • Unauthenticated + Supported method = 401
  • Unauthenticated + Unsupported method = 405
  • Authenticated + Authorized + Supported method = 2xx
  • Authenticated + Authorized + Unsupported method = 405
  • Authenticated + Unauthorized + Supported method = 403
  • Authenticated + Unauthorized + Unsupported method = 405

In other words, from a procedural standpoint:

  1. Check whether methods are supported. If not: 405
  2. If supported, check if the user is authenticated. If not: 401
  3. If authenticated, check if the user is authorized. If not: 403
  4. If authorized: 2xx

EDIT: I stumbled upon this diagram and thought it might be useful to anyone else who might stumble across this post. Click to enlarge.

enter image description here

Original here.

like image 129
docksteaderluke Avatar answered Oct 03 '22 18:10

docksteaderluke