80

I am building a web application with a services layer. The services layer is going to be built using a RESTful design. The thinking is that some time in the future we may build other applications (iPhone, Android, etc.) that use the same services layer as the web application. My question is this - how do I implement login? I think I am having trouble moving from a more traditional verb based design to a resource based design. If I was building this with SOAP I would probably have a method called Login. In REST I should have a resource. I am having difficulty understanding how I should construct my URI for a login. Should it be something like this:

http://myservice/{username}?p={password}

EDIT: The front end web application uses the traditional ASP.NET framework for authentication. However at some point in the authentication process I need to validate the supplied credentials. In a traditional web application I would do a database lookup. But in this scenario I am calling a service instead of doing a database lookup. So I need something in the service that will validate the supplied credentials. And in addition to validating the supplied credentials I probably also need some sort of information about the user after they have successfully authenticated - things like their full name, their ID, etc. I hope this makes the question clearer.

Or am I not thinking about this the right way? I feel like I am having difficulty describing my question correctly.

Corey

6 Answers 6

65

As S.Lott pointed out already, we have a two folded things here: Login and authentication

Authentication is out-of-scope here, as this is widely discussed and there is common agreement. However, what do we actually need for a client successfully authenticate itself against a RESTful web service? Right, some kind of token, let's call it access-token.

Client) So, all I need is an access-token, but how to get such RESTfully?
Server) Why not simply creating it?
Client) How comes?
Server) For me an access-token is nothing else than a resource. Thus, I'll create one for you in exchange for your username and password.

Thus, the server could offer the resource URL "/accesstokens", for POSTing the username and password to, returning the link to the newly created resource "/accesstokens/{accesstoken}". Alternatively, you return a document containing the access-token and a href with the resource's link:

<access-token
  id="{access token id goes here; e.g. GUID}"
  href="/accesstokens/{id}"
/>

Most probably, you don't actually create the access-token as a subresource and thus, won't include its href in the response.
However, if you do so, the client could generate the link on its behalf or not? No!
Remember, truly RESTful web services link resources together in a way that the client can navigate itself without the need for generating any resource links.

The final question you probably have is if you should POST the username and password as a HTML form or as a document, e.g. XML or JSON - it depends... :-)

4
  • 4
    Not perfectly following REST, but a simple and measurably better than others. Plus shared with good humor. Commented Nov 23, 2012 at 14:35
  • 2
    Patrick, are you proposing the same thing as this answer? stackoverflow.com/a/1135995/14731
    – Gili
    Commented May 24, 2014 at 5:26
  • 1
    Is 403 correct status code when username and/or password does not match?
    – tsobe
    Commented Aug 16, 2015 at 9:44
  • What an idea. Creating 'accesstoken' resource.
    – Sorter
    Commented Nov 3, 2015 at 5:52
26

You don't "login". You "authenticate". World of difference.

You have lots of authentication alternatives.

HTTP Basic, Digest, NTLM and AWS S3 Authentication

  • HTTP Basic and Digest authentication. This uses the HTTP_AUTHORIZATION header. This is very nice, very simple. But can lead to a lot of traffic.

  • Username/Signature authentication. Sometimes called "ID and KEY" authentication. This can use a query string.

    ?username=this&signature=some-big-hex-digest

    This is what places like Amazon use. The username is the "id". The "key" is a digest, similar to the one used for HTTP Digest authentication. Both sides have to agree on the digest to proceed.

  • Some kind of cookie-based authentication. OpenAM, for example, can be configured as an agent to authenticate and provide a cookie that your RESTful web server can then use. The client would authenticate first, and then provide the cookie with each RESTful request.

14
  • 2
    @S.Lott @Corey Users absolutely can interact with RESTful systems. Most static HTML web sites are RESTful "services". Commented Jan 5, 2011 at 23:46
  • 7
    @Darrel Miller: "Your idea ... is flawed" doesn't provide a proper context or anything useful. It's kind of negative and not too helpful.
    – S.Lott
    Commented Jan 6, 2011 at 1:48
  • 5
    @Darrel Miller: "claim REST is limited to ... is just plain ridiculous". Whatever. Rather that repeat what's wrong, could you perhaps explain what's right? Could you provide a simple positive statement instead of negative statements? Could you explain what REST is instead of what it isn't? There are an infinite number of things it is not.
    – S.Lott
    Commented Jan 6, 2011 at 2:02
  • 4
    @Darrel Miller: 5.2.1.2 Representations seems pretty clear. Your comments are very negative. Could you provide positive corrections or updates instead of negativity? I can't figure out how to correct or modify my answer to satisfy you, since all you're doing is saying that I'm somehow "flawed" or "spreading incorrect information" or "ridiculous". What's less flawed, less incorrect or less ridiculous?
    – S.Lott
    Commented Jan 6, 2011 at 2:30
  • 5
    @S.Lott 1) Users interact with RESTful systems all the time. 2) HTML is perfectly valid media type for a RESTful system to return. 3) REST is not a subset of HTTP. REST is an architectural style, HTTP is a protcol. 4) RESTful systems are not limited to "web services" 5) RESTful systems can simulate a login by using some form of authorization token, without suffering the problems related to sessions. Commented Jan 6, 2011 at 2:40
1

Great question, well posed. I really like Patrick's answer. I use something like

-/users/{username}/loginsession

With POST and GET being handled. So I post a new login session with credentials and I can then view the current session as a resource via the GET.

The resource is a login session, and that may have an access token or auth code, expiry, etc.

Oddly enough, my MVC caller must itself present a key/bearer token via a header to prove that it has the right to try and create new login sessions since the MVC site is a client of the API.

Edit

I think some other answers and comments here are solving the issue with an out-of-band shared secret and just authenticating with a header. That's fine in many situations or for service-to-service calls.

The other solution is to flow a token, OAuth or JWT or otherwise, which means the "login" has already taken place by another process, probably a normal login UI in a browser which is based around a form POST.

My answer is for the service that sits behind that UI, assuming you want login and auth and user management placed in a REST service and not in the site MVC code. It IS the user login service.

It also allows other services to "login" and get an expiring token, instead of using a pre-shared key, as well as test scripts in a CLI or Postman.

1
  • 2
    Pass the token in a header, yes. Pass it as part of the URL, no. The URL is encrypted in-transit when you use HTTPS. However; the URL is also stored in the browser history and in server logs. There a lot of good reasons to avoid passing security-sensitive data in URL query parameters. Commented Jun 30, 2015 at 5:11
0

Since quite a bit has changed since 2011...

If you're open to using a 3rd party tool, and slightly deviating from REST slightly for the web UI, consider http://shiro.apache.org.

Shiro basically gives you a servlet filter purposed for authentication as well as authorization. You can utilize all of the login methods listed by @S.Lott, including a simple form based authentication.

Filter the rest URLs that require authentication, and Shiro will do the rest.

I'm currently using this in my own project and it has worked pretty well for me thus far.

Here's something else people may be interested in. https://github.com/PE-INTERNATIONAL/shiro-jersey#readme

0

The first thing to understand about REST is that its a Token based resource access.Unlike traditional ways, access is granted based on token validation. In simple words if you have right token, you can access resources.Now there is lot of whole other stuff for token creation and manipulation.

For your first question, you can design a Restfull API. Credentials(Username and password) will be passed to your service layer.Service layer then validates these credentials and grant a token.Credentials can be either simple username/password or can be SSL certificates. SSL certificates uses the OAUTH protocol and are more secure.

You can design your URI like this- URI for token request-> http://myservice/some-directory/token? (You can pass Credentilals in this URI for Token)

To use this token for resource access you can add this [Authorization:Bearer (token)] to your http header.

This token can be utilized by the customer to access different component of your service layer. You can also change the expiry period of this token to prevent misuse.

For your second question one thing you can do is that you grant different token to access different resource components of your service layer. For this you can specify resource parameter in your token, and grand permission based on this field.

You can also follow these links for more information- http://www.codeproject.com/Articles/687647/Detailed-Tutorial-for-Building-ASP-NET-WebAPI-REST

http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

-4

I have faced the same problem before. Login does not translate nicely to resource based design.

The way I usually handle it is by having Login resource and passing username and password on the parameter string, basically doing

GET on http://myservice/login?u={username}&p={password}

The response is some kind of session or auth string that can then be passed to other APIs for validation.

An alternative to doing GET on the login resource is doing a POST, REST purists will probably not like me now :), and passing in the creds in the body. The response would be the same.

6
  • 11
    Password? Plain text password? As a query string? Did you really mean that, or do you mean a digest of the password?
    – S.Lott
    Commented Jan 5, 2011 at 19:23
  • Thanks. That makes sense. Here's a follow up question - for a large application would you create one large RESTful service for everything or break things up in to different services? I was thinking of having a service just for authentication and then different services for the different modules of my application. Are there any reasons why you would or would not do it one way or the other? Commented Jan 5, 2011 at 19:25
  • 3
    S. Lott: It depends on what you are trying to do. Of course if you can do a digest, then by all means. Sometimes a digest is not possible. If the only option open to you is sending a plain text password please do it over SSL, in this case it is also better to use a POST rather then GET to prevent the browser from remembering what you sent.
    – Alex
    Commented Jan 5, 2011 at 19:33
  • Corey: I am not sure I understand the distinction between one large webservice and many different ones. You usually define your service in terms of resources, adding only as few as makes sense. I think I am missing your point though.
    – Alex
    Commented Jan 5, 2011 at 19:35
  • Alex: let's say that I had 4 different main sections of my web application - Reports, Orders, Downloads and Invoices. Would it make sense to have 4 different service definitions or only 1 service definition? Are there any specific reasons why you would not want to break things up in to many different services? Commented Jan 5, 2011 at 19:44

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