Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What status code should a REST API return for login requests performed with wrong credentials?

I have found a lot of answers and explanations for the meanings of HTTP status codes. My question is specifically about the POST request to a login endpoint, which asks for username and password for example and the scenario where incorrect ones are provided.

Some thoughts:

400 Bad Response I think this code is not appropriate, because it says the request was syntactically incorrect and not understood by the server, which is not the case here. The login data is just semantically not correct.

401 Unauthorized Here is the tricky part for me. If 401 can only occur on requests requiring an authentication header then this is not correct. But if 401 can occur on all requests, which require authentication (either as header or in the body) then 401 is a candidate.

403 Forbidden Usually, 403 is returned if the user already is authenticated and known to the system but requested a resource he/she is not allowed to access. The user definitely is not authenticated before the login. I don't know if there is a semantic for 403 for unauthenticated users.

I'm happy to be told the answer or hear your thoughts.

like image 372
Peter F Avatar asked Jul 27 '17 17:07

Peter F


People also ask

Which HTTP status code is usually returned when a user provides incorrect credentials?

The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource.

What is the status code for invalid credentials?

The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource.

When the user provides invalid credentials which of the following error status codes should be returned to the client?

401: “Unauthorized” or “Authorization Required.” This is returned by the server when the target resource lacks valid authentication credentials. You might see this if you've set up basic HTTP authentication using htpasswd.

Which response status code would be appropriate when the user making the request is not logged in?

A 403 error response indicates that the client's request is formed correctly, but the REST API refuses to honor it, i.e., the user does not have the necessary permissions for the resource. A 403 response is not a case of insufficient client credentials; that would be 401 (“Unauthorized”).


2 Answers

If a user is attempting to authenticate, but provides invalid credentials, the response should have a status of 401, regardless of if you are using Basic Authorization or not. 401 indicates that authentication failed, but the user can alter their request and attempt again.

If a user is authenticated, but not authorized to access the requested resource, then the response should have a status of 403. 403 indicates that the user is forbidden from accessing the resource, and no matter how they alter the request, they will not be permitted access.

In the scenario that your endpoint requires the credentials to be in the body of the request, you should return a 400 if the request body does not meet your specifications.

like image 97
Joshua Jones Avatar answered Oct 10 '22 08:10

Joshua Jones


My question is specifically about the POST request to a login endpoint, which asks for username and password for example and the scenario where incorrect ones are provided.

It depends on how the credentials are sent:

  • If you are using HTTP Authentication (sending credentials in the Authorization header), you can return 401 to indicate that the credentials are invalid.

  • If you send credentials in the request body (for example a JSON with username and password), 401 doesn't seem to be the most suitable status code (once it's not a real HTTP Authentication). In this situation, consider 403 instead with a descriptive response payload.


The 403 status code also can be used to indicate authorization problems, that is, to indicate that a user is not allowed to perform an action.

like image 36
cassiomolin Avatar answered Oct 10 '22 07:10

cassiomolin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!