We developing an application with Laravel/PHP and we want to use an pay-per-user pricing model. For that we have to ensure that an account can only used by only one concurrent user. We use JWT for authentication and it is stateless so i cant use sessions.
To ensure one concurrent login i can enclose the browseragent or IP, but both aren't unique and it is possible that they occur multiple times in e.g. an office. Also i can send the MAC address, but that is not the easiest way.
Are there other solutions to ensure one concurrent login per user with JWT?
I think that the simple answer to this is NO, you cannot do that by JWT and keep the server stateless. However, if you use the setup with an Access Token and a Refresh Token, you can probably achieve something like this:
- A user logs in, you store the Refresh Token in a DB
- The Access Token expires. Before you issue a new Access Token from your Refresh Token, make the standard check that the account is still OK, but also compare the Refresh Token to the one in your DB. Make sure they match.
- A second user logs in with the same account. Store the issues Refresh Token in the DB and over write the old Refresh Token. (One stored Refresh Token per account.)
- First users Access Token expires again. This time there is another Refresh Token in the DB, and no new Access Token is issued for that user.
This will result in a login flow where the latest user to login can use your service. This is handy if it actually is the same user that change device or restart browser session. Compare to for example Spotify's "chasing the stream" way of handling concurrent listening.
I now testing without JWT and with OAuth2 authentication with password grant tokens. Within 1 client the user can only use the app with one login, if he logins on another session/device, the other login (token) will not be valid anymore. When i want to allow one user to login multiple times (e.g. web app and mobile app) i can use multiple clients.
The only I can think of to do this without saving the state on the server is to disable the feature to sign new tokens for the life span of the token.