One of the ways I saw this in the API (and how I am implementing it now) is to create a RESTful resource called Session created using POST that provides a username and password.
Here is basically how I implemented it:
POST /sessions { Username: "User", Password: "Password" }
Create a time-limited session and return a session resource that contains the session key value and expires. You can also return this as a cookie value for the convenience of implementing API clients.
DELETE /session/{id}
Terminates the session urgently, so it can no longer be used. This is used for explicit exits.
Then I add the session key through the request parameter, although you can also allow it to be sent via the cookie value, I would recommend allowing both.
I prefer it to be very simple.
Obviously, your script will dictate a little how your sessions should be managed, perhaps they are not time limited and do not work indefinitely, and perhaps they are hashed or encrypted for added security.
If you use HTTPS everywhere, you probably don't need to worry too much. However, if you want to use HTTP, you will need to use something like a hash along with the secret key and say a time stamp to create a secure key for each request. This way you can share the secret key over HTTPS and then switch to HTTP for further calls. Even if someone manages to get the key out of the request, it can expire almost immediately and be useless.
Disclaimer: I'm not a security expert ,-).
Chris Nicola Jun 19 '11 at 17:33 2011-06-19 17:33
source share