allow one concurrent user per login with JWT

前端 未结 5 935
闹比i
闹比i 2020-12-16 15:13

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 concurre

相关标签:
5条回答
  • 2020-12-16 15:30

    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.

    0 讨论(0)
  • 2020-12-16 15:43

    Anyway you need to store token in db.
    For that purpose you will need super fast storage. Redis will be perfect.
    On new login just replace token with new one.
    Every time user makes request check if token matches the one you have in redis. If not then kick him out.

    0 讨论(0)
  • 2020-12-16 15:43

    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.

    0 讨论(0)
  • 2020-12-16 15:47

    Only answer i can think of:

    On login: "User" in your DB has a value = activeJwt

    User logs in and JWT token is created, copy the JWT string to value activeJWT in your DB and send it to user. If you login on another device same deal, and the activeJWT value is changed

    On all requests that require login match users JWT-string and activeJWT, if they dont match it means another device logged in after making the old token useless.

    0 讨论(0)
  • 2020-12-16 15:51

    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:

    1. A user logs in, you store the Refresh Token in a DB
    2. 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.
    3. 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.)
    4. 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.

    0 讨论(0)
提交回复
热议问题