I am currently working on a REST library for .net, and I would like to hear some opinions about the open point I have: REST and authentication.
Here is an example of the RESTful interface used with the library:
[RestRoot("/user")] public interface IUserInterface { [RestPut("/")] void Add(User user); [RestGet("/")] int[] List(); [RestGet("/get/{id}")] User Get(int id); [RestDelete("/delete/{id}")] void Delete(int id); }
Then, the server code simply implements the interface, and clients can get the same interface through the factory. Or, if the client does not use the library, the standard HTTP request also works.
I know that there are basic ways to use HTTP Basic Auth or send a token to requests that require authenticated users.
The first method (HTTP Basic Auth) has the following problems (partially a web browser):
- A password is sent with every request - even with SSL it has some kind of "bad feeling".
- Since the password is transmitted with the request header, it would be easy for a local attacker to look at the transmitted headers in order to obtain the password.
- Password is available in browser memory.
- There is no standard way to expire user sessions.
- Login to the browser interrupts the appearance of the page.
The problems for the second method are more focused on the implementation and use of the library:
- Each URI request that requires authentication must have a parameter for the token, which is very repetitive.
- There is much more code to write if each implementation of the method needs to check if the token is valid.
- The interface will become less specific, for example.
[RestGet("/get/{id}")] versus [RestGet("/get/{id}/{token}")] . - Where to put the token: at the end of the URI? after the root? somewhere else?
My idea was to pass the token as a parameter to the url, like http:/server/user/get/1234?token=token_id .
Another possibility is to send the parameter in the form of an HTTP header, but this will make it difficult to use it using regular HTTP clients, which I suppose.
The token will be sent back to the client as a custom HTTP header ("X-Session-Id") for each request.
This can be completely abstracted from the interface, and any implementation that needs authentication can simply ask who the token belongs to (if given).
Do you think this violates REST too much or do you have any ideas?
authentication rest
Fionn Jan 19 '09 at 17:47 2009-01-19 17:47
source share