I am working on adding a REST API to a legacy PHP site. This is to provide an endpoint for an internal app, so I am quite free in how I design things and what do and don't support.
What I now need to add to this API is a way to login, and then perform actions as a specific user. The site has been built years ago and not necessarily with the best practices at the time, so I am unfortunately a bit restricted in how I do this. All of this needs to run in PHP 5.4 with MySQL 5.6.
I have been reading up on common designs for this and OAuth1/2 looks like the most common standard. However, this seems like massive overkill for my purposes, since it has various features that I do not need and seems very complicated to implement.
Instead, I am planning on just doing something like this:
- The client calls a
get_sessionAPI endpoint, which generates a random session ID, saves that to a table in the database and returns it to the client. - The client saves this session ID.
- Then the client authenticates by sending a request to the
loginendpoint, sending the username, password and session ID (via HTTPS obviously). - The server compares the data to the user table and, if the login is correct, updates the session table to associate the session ID with the corresponding user ID. This needs to be rate-limited in some way to prevent brute forcing.
- Now the client can call any other endpoints providing only its session ID for authorization.
- On each request, the server looks up the session ID, sees which user it has been associated with and performs the correct action.
- The client can remember the session ID for future use, until it either gets removed manually or expires after some amount of time.
- To log out, the client sends a request to the
logoutendpoint and the server removes the association with the user account.
Is this a reasonable design? It's obviously not very sophisticated, but I am looking for something that I can implement without a huge hassle or requiring third-party libraries.