Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm specifically interested in how users perform authorized / authenticated operations on a web API.

Are authentication cookies compatible with the REST philosophy, and why?

share|improve this question
    
exact duplicate of restful-authentication –  Jarrod Roberson Mar 22 '12 at 18:21
    
    
@JarrodRoberson My understanding was that answers on another site wouldn't qualify the question as duplicate on here –  Tom Squires Mar 25 '12 at 16:42
    
that is why there is a "belongs on stackoverflow" option under close –  Jarrod Roberson Mar 26 '12 at 13:34
2  
@JarrodRoberson based on each site's FAQs, I would argue the question belongs on this site and not Stack Overflow. I'm interested in design methodology / philosophy and tradeoffs regarding this aspect of RESTful architecture. Stack Overflow is meant for implementation questions, where this site is more about design methodologies and tradeoffs. –  Brandon Linton Mar 26 '12 at 14:03

3 Answers 3

up vote 28 down vote accepted

An ideal ReSTful service allows clients (which may not be in-browser) to perform any needed task in one request; because the full state needed to do that is held by the client, not the server. Since the client has full control of the state, it can create the state on its own (if that is legitimate), and only talk to the API to "get 'er done".

Requiring cookies can make that difficult. For clients besides browsers, managing cookies is a pretty big inconvenience compared to query params, plain request headers or the request body. On the other hand, In browser, using cookies can make lots of things much simpler.

So an API might first look in the Authorization header for the authentication data it needs, since that's probably the place where non-browser clients will prefer to put it, but to simplify and streamline browser-based clients, it might also check for a session cookie for server side log in, but only if the regular Authorization header was missing.

Another example might be a complex request that normally requires lots of parameters set. A non interactive client would have no trouble jamming all of that data into one request, but a HTML form based interface might prefer to break the request into several pages (something like a set of 'wizard' pages) so that users aren't presented with options that are not applicable based on previous selections. All of the intermediate pages could store the values in client side cookies, so that only the very last page, where the user actually submits the request, has any server side effect at all. The API could look for the needed attributes in the request body, and fall back to looking at cookies if the needed parameters weren't there.

share|improve this answer
1  
+1, I definitely like the practicality of using the Authorization header but "falling back" to cookies depending on what works best for the client. –  Brandon Linton Mar 26 '12 at 14:17

Yes and No - Depends how you use it.

Cookies if used to maintain client state at the client, for the client, of the client and by the client then they are restful.

If you are storing server state into the cookie then you are basically just shifting the load to the client - which isn't restful.

So what are some examples?

Restful:

  • Authentication details or 'is logged in' kinda stuff
  • last viewed page or place in application etc.

Not Restful:

  • Storing session information

Restfulness comes from statelessness - of the server. Clients can maintain application state and send it to the server to say where they are so that the server can decide where to go from there. Basically sessions/states need historical data and are dependent on past requests so to speak, restful applications ideally aren't (It's not viable to have a 100% pure restful application if you are going to have a login screen :)

share|improve this answer
4  
If you store an "isLoggedIn" flag on the client, you might as well use no authentication at all. –  tdammers Mar 23 '12 at 6:54
    
This definitely makes sense - storing application state client-side wouldn't be consistent with REST, but client information that it uses to represent itself seems fine. –  Brandon Linton Mar 26 '12 at 14:15
    
I'd like to add that putting authentication information in cookies leaves open the possibility of cross-site request forgery attacks. There are better ways, I suggest copying Amazon: docs.aws.amazon.com/AmazonS3/latest/API/… –  Dobes Vandermeer Jun 16 at 18:11

One can use cookies. REST allows them.

REST requires that any session information be stored on the client side, but when it comes to authentication, some information has to stay on the server side for security reasons.

From one of my blog posts, there is a general agreement that authentication data is considered out of scope regarding REST. Hence, it is ok for servers to keep some of this session data on their side.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.