Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method should I use for a login (authentication) request?

I would like to know which http method I should use when doing a login request, and why? Since this request creates an object (a user session) on the server, I think it should be POST, what do you think? But since the login request should be idempotent, it could be PUT, couldn't it?

Same question for a logout request, should I use the DELETE method?

like image 640
greg0ire Avatar asked May 03 '11 11:05

greg0ire


People also ask

Which form method should be used for a login form?

In MOST cases, forms should use the post method. In the case of logins, that would be ALWAYS use post.

How do I authenticate a login?

Using HTTP Basic Authentication A client requests access to a protected resource. The Web server returns a dialog box that requests the user name and password. The client submits the user name and password to the server. The server validates the credentials and, if successful, returns the requested resource.

Is login API POST or get?

Always POST , and preferably with SSL (as in: https://... ). Because the parameters in GET get stored all over the place for caching reasons.

How do I authenticate a login in HTML?

Authentication SchemesThe Form authentication scheme uses a HTML web form for the user to enter their username and password credentials and HTTP Post requests to submit to the server for verification. It may also be used programmatically va HTTP POST requests.


2 Answers

If your login request is via a user supplying a username and password then a POST is preferable, as details will be sent in the HTTP messages body rather than the URL. Although it will still be sent plain text, unless you're encrypting via https.

The HTTP DELETE method is a request to delete something on the server. I don't think that DELETING an in memory user session is really what it's intended; more it's for deleting the user record itself. So potentially logout can be just a GET e.g. www.yoursite.com/logout.

like image 142
planetjones Avatar answered Sep 17 '22 18:09

planetjones


I believe that you can translate LOGIN & LOGOUT methods into basic CRUD operations CREATE & DELETE. Since you are creating a new resource called SESSION and destroying it when logging out:

  1. POST /login - creates session
  2. DELETE /logout - destroys session

I would never do LOGOUT as GET just because anyone could make an attack just simply by sending an email with IMG tag or link to website where such an IMG tag exists. (<img src="youtsite.com/logout" />)

P.S. Long time I was wondering how would you create a RESTful login/logout and it turned out it's really simple, you do it just like I described: use /session/ endpoint with CREATE and DELETE methods and you are fine. You could also use UPDATE if you want to update session in one way or another...

like image 28
Vytautas Butkus Avatar answered Sep 19 '22 18:09

Vytautas Butkus