One major tenant of REST is sessionlessness or statelessness of a request. In other words no client state should be stored on the server. Sticking to this approach generally allows for a simpler API and simpler coding exercise in building the backend behavior. This tenant also facilitates load-balancing of requests or robustness (i.e. server failures)--meaning that with no client state on the server it is much easier to distribute requests among server clusters.
So, in my experience I really get the concept of sticking to statelessness in the design of an interface. Every request then that comes into a RESTful system requires authentication. An example of a request could be:
> POST /some/rest/api/my_api_method HTTP/1.1
> User-Agent: curl/7.21.0 (i486-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/126.96.36.199 libidn/1.15 libssh2/1.2.6
> Host: 127.0.0.1
> Accept: */*
> authorization: Basic dnlhdRRh5nZ5YXR0YQ==
The request above contains no body and the access is allowed with the correct authorization header. As with the example above the authorization header parameter is required to accompany all requests (that need authorization that is). This does simplify code on the backend as this means all requests can be treated identically with request to authorized access.
And this is all fine and dandy. And makes for a nice simple contract between the frontend and backend.
However, this doesn't work well for browser based REST clients (note that a REST client can be any application, including browsers). Let's step back a second and look at how the browser stores stateful data. Such as a username and password accompanies each request as part of the authorization header parameter. On the client this can be kept in the following locations:
- In memory via the browser session object
- URL or form based techniques
The cookie approach is elegant and does the job, especially with timing out access, but is considered insecure. If the password is obtained through some browser specific weakness in accessing the cookie, the hacked access is good forever (or until the user changes their password if the hacker hasn't already done that for them).
Or pass the username/password combo back and forth via POSTS/GETS between the server and client. This is an awkward approach though and mixes the authentication behavior into the client/server presentation logic.
The only real way to avoid weak or insecure password storage and avoid the problems mentioned in the techniques above is to keep the password out of the client. The client then needs to exchange a token instead. This token can be created by the server and returned to the client. And now we are now back to session management. In this way, the storage of the token is still considered insecure (if kept in a cookie) for the same reasons given above, but the single key difference is that the token or session key lifespan is now controlled by the server and if obtained maliciously will only provide for access for a short period of time (or server controlled period of time).
And this precisely works against the concept of RESTful principals. The best you can make of it then is to keep the session object as simple as possible and have it extend across all of the API (i.e. reducing state transitions).