User registration/authentication flow on a REST API

REGISTER

Tokens that expire/tokens per session are not complying with the statelessness of REST, right?

No, there’s nothing wrong with that. Many HTTP authentication schemes do have expiring tokens. OAuth2 is super popular for REST services, and many OAuth2 implementations force the client to refresh the access token from time to time.

My idea is that at the time to create a new user, create a new token for the user, to immediately return it with the Response, and thus, improving the UX. The user will immediately be able to start using the web app. However, returning the token for such response would break the rule of returning just the resource. Should I instead make two requests together? One to create the user and one to retrieve the token without the user needing to reenter credentials?

Typically, if you create a new resource following REST best practices, you don’t return something in response to a POST like this. Doing this would make the call more RPC-like, so I would agree with you here… it’s not perfectly RESTful. I’ll offer two solutions to this:

  1. Ignore this, break the best practices. Maybe it’s for the best in this case, and making exceptions if they make a lot more sense is sometimes the best thing to do (after careful consideration).
  2. If you want be more RESTful, I’ll offer an alternative.

Lets assume you want to use OAuth2 (not a bad idea!). The OAuth2 API is not really RESTful for a number of reasons. I’m my mind it is still better to use a well-defined authentication API, over rolling your own for the sake of being RESTful.

That still leaves you with the problem of creating a user on your API, and in response to this (POST) call, returning a secret which can be used as an access/refresh token.

My alternative is as follows:

You don’t need to have a user in order to start a session.

What you can do instead is start the session before you create the user. This guarantees that for any future call, you know you are talking to the same client.

If you start your OAuth2 process and receive your access/refresh token, you can simply do an authenticated POST request on /users. What this means is that your system needs to be aware of 2 types of authenticated users:

  1. Users that logged in with a username/password (`grant_type = passsword1).
  2. Users that logged in ‘anonymously’ and intend to create a user after the fact. (grant_type = client_credentials).

Once the user is created, you can assign your previously anonymous session with the newly created user entity, thus you don’t need to do any access/refresh token exchanges after creation.

EMAIL VALIDATION

Both your suggestions to either:

  • Prevent the user from using the application until email validation is completed.
  • Allow the user to use the application immediately

Are done by applications. Which one is more appropriate really depends on your application and what’s best for you. Is there any risk associated with a user starting to use an account with an email they don’t own? If no, then maybe it’s fine to allow the user in right away.

Here’s an example where you don’t want to do this: Say if the email address is used by other members of your system to add a user as a friend, the email address is a type of identity. If you don’t force users to validate their emails, it means I can act on behalf of someone with a different email address. This is similar to being able to receive invitations, etc. Is this an attack vector? Then you might want to consider blocking the user from using the application until the email is validated.

You might also consider only blocking certain features in your application for which the email address might be sensitive. In the previous example, you could prevent people from seeing invitations from other users until the email is validated.

There’s no right answer here, it just depends on how you intend to use the email address.

LOGIN

Please just use OAuth2. The flow you describe is already fairly close to how OAuth2 works. Take it one step further an actually use OAuth2. It’s pretty great and once you get over the initial hurdle of understanding the protocol, you’ll find that it’s easier than you thought and fairly straightforward to just implement the bits you specifically need for your API.

Most of the PHP OAuth2 server implementations are not great. They do too much and are somewhat hard to integrate with. Rolling your own is not that hard and you’re already fairly close to building something similar.

LOGOUT

The two reasons you might want a logout endpoint are:

  1. If you use cookie/session based authentication and want to tell the server to forget the session. It sounds like this is not an issue for you.
  2. If you want to tell the server to expire the access/refresh token earlier. Yes, you can just remove them from localstorage, and that might be good enough. Forcing to expire them server-side might give you that little extra confidence. What if someone was able to MITM your browser and now has access to your tokens? I might want to quickly logout and expire all existing tokens. It’s an edge case, and I personally have never done this, but that could be a reason why you would want it.

REMEMBER ME

Yea, implementing “remember me” with local storage sounds like a good idea.

Leave a Comment