24

Although I "think" I understand it I need some clarity. With PURE Restful authentication, things do get a bit unwieldy and using forms helps a lot with the UI of the application (i.e., get to have separate login page, forgot password links, easier logout? etc.,)

Now Forms come along and some folks say "not restful" - what is "not restful" about them? Is it that there is no corresponding login resource so to speak? Or does it force something else that I'm missing?

Note: If ones create sessions with them, that's a different matter altogether. I'm more keen on know "why" are they branded as restful? Just googling for "Form based authentication vs restful authentication" throws up quite a few hits.

One could use these "forms" to authenticate and pass on tokens for the application to store in cookies etc., which I feel is entirely restful (assuming cryptographic security etc.,)...

PhD
  • 11,202
  • 14
  • 64
  • 112

5 Answers5

23

There is nothing wrong with sending your credentials, perhaps through a form, for authentication. The problem is most Form based systems rely on sessions, thus requiring you to only log in "once".

Sessions are server state, thus violating the stateless constraint of a REST architecture.

If you have to send the credentials each time, you can either include them in the payload (i.e. using a form), or you can use the HTTP Authorization header.

If you include them in the payload, you can include them in the body, but only for a POST or PUT, and not a GET or DELETE (since they don't have bodies).

If you include them in the URL as part of the query parameters, then the URL is no longer necessarily representing the actual resource. One of the other tenets is that the URL matches the resource. Adding out of band information (such as credentials) within the query parameters muddies that constraint up a bit.

So, for a REST system over HTTP, you're better to use the existing HTTP Authorization mechanism than working out something else. You could also use client specific SSL certs as well, that works fine also.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • When you say "existing HTTP Authorization mechanisms" do you mean basic/digest? But don't they take away the "fun" from creating pretty login screens? :) – PhD Aug 17 '11 at 20:50
  • 4
    Yea, instead you get the fun of trying to work around logging out web pages using Basic and Digest! Lots of fun all around for everyone. – Will Hartung Aug 17 '11 at 20:51
  • 1
    It's NO fun at all, you just land up scratching various parts of the human anatomy, just thinking about how to solve it ;) – PhD Aug 17 '11 at 20:53
  • What about a session-free cookie-based approach? Surely that could be considered RESTful? – DaveRandom Aug 18 '11 at 17:09
  • 5
    Of course. But if you're going to do that, why not simply use the Authorization header for this information instead of the Cookie header? – Will Hartung Aug 18 '11 at 19:00
10

Excellent question. I think that RESTful purists will likely say that having a URI associated with an action rather than a resource is what makes form-based auth not RESTful, which is something you pointed out yourself.

Honestly I think that the idea of a 100% pure RESTful application is somewhat of a Utopian ideal when it comes to web applications. I believe it is achievable, however, for RESTful web services, since the calling applications can pass credentials with the request header.

At the end of the day, I think that as long as your application works it is fine, and you should not worry about whether or not it is purely RESTful.

That's my $0.02.

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
7

From this answer:

To be RESTful, each HTTP request should carry enough information by itself for its recipient to process it to be in complete harmony with the stateless nature of HTTP.

It's not that form-based auth is not RESTful — it's not RESTful to have a session at all. The RESTful way is to send credentials with every request. This could easily be eavesdropped upon, however, but HTTPS/SSL/TLS closes that hole.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

Form-based authentication does not use the authentication techniques that are built into HTTP (e.g. basic authentication, digest authentication).

REST purists will ask you to use the functionality built into HTTP wherever possible. Even though form-based authentication is extremely common, it is not a part of the official protocol. So the REST purist sees form-based authentication as an instance of "building functionality on top of HTTP when that functionality already exists within HTTP itself."

Mike Clark
  • 10,027
  • 3
  • 40
  • 54
  • Errr what does it _not_ use? Just the fact that the credentials are not part of the "WWW-authorization" header?? You can "always" tweak the UI to send the data as such, right? – PhD Aug 17 '11 at 20:52
0

Now Forms come along and some folks say "not restful" - what is "not restful" about them?

The authentication credentials are not in the standard place.

REST doesn’t eliminate the need for a clue. What REST does is concentrate that need for prior knowledge into readily standardizable forms. -- Fielding, 2008

RFC 7235 describes the Authorization header; that gives us a standard way to distinguish authorized requests (which have the header) from anonymous requests (which don't have the header).

Because we can distinguish authorized and anonymous requests, general purpose components can do interesting things.

For example, if the origin server is restricting access to a resource, we probably don't want a shared cache to be re-using copies of the HTTP response to satisfy other requests. Because the authorization has a standardized form, we can define caching semantics that restrict what a shared cache is allowed to do.

Putting the same information into a cookie header, in effect, hides the authorization semantics from general purpose components.

(This is somewhat analogous to using POST requests for safe requests -- general purpose components can't distinguish semantically safe POST requests from semantically unsafe POST requests, and therefore can't do anything intelligently take advantage of the safe handling).

If we had smarter form processing -- form input controls that copied information into the Authorization header instead of the query string/request-body -- then forms would be fine. But (a) we don't have that and (b) we are unlikely to get it, because backwards compatibility.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91