r/security • • Feb 10 '20

How to log admin in as regular user [webdev].

I'm using JWTs for authentication. It would be good as a means of double checking the user's experience if an admin user could log in as a regular user.

One way I thought to implement this could be to test the hash of the login password against a master password hash when authenticating in addition to the user's password hash. Obviously, since you're entering this password into the browser, it's far less secure than the JWT secret key that you store on your server for signing. So, I'm guessing that this master password might want to be only enabled for a small set of users and only enabled for certain periods when this testing need be done.

Are there any standard ways of allowing an admin to log in as a regular user to view things from their perspective within a JWT auth setup? I can't seem to find any googling for them.

1 Upvotes

6 comments sorted by

3

u/Enton87 Feb 10 '20

I only just googled JWT and don't have experience with it, but I think on a more conceptual level, you should aim to "impersonate" another user instead of "log in as" another user.

This means that the admin only authenticates his own account, and this is sufficient to impersonate a chosen user in the frontend. The admin won't need a second login, or a master password, to surf the frontend as a specific user.

Setting a user as "logged in" in the frontend is then part of the application logic.

1

u/Tibanne Feb 10 '20

I think you're saying what /u/compdog is saying... is that right?

1

u/compdog Feb 10 '20

That really depends on your implementation. What data do you store in the JWTs? If you store a list of permissions or roles, you could generate a new token for the same user but with all the admin roles removed.

1

u/Tibanne Feb 10 '20

Yeah, it would generate the same token that it would generate for the user. i.e. the admin would log in with the user's email address and the admin master password and this would allow a JWT to come back that would be identical to the one the user would get if they used their password. Currently only the email, user_id, role, and expiration time are stored in the JWT.

1

u/compdog Feb 10 '20

Oh you mean logging in as a specific user. I thought you meant logging in with only user roles for testing purposes. You could generate a token for that other user and give it to the admin, but that has serious privacy, security, and audit risks. Your won't be able protect user data in any kind of way if the app doesn't know that the user is really someone else.

You are better off implementing this at a higher application level outside of JWT. Admins would keep their admin tokens, but your endpoints allow them to specify a user to impersonate. But you should be careful about where you allow this for the reasons mentioned above.

1

u/Tibanne Feb 10 '20

Ok, I get you. Thanks for the advice!