Search code examples
securitysessionauthenticationtoken

Is session authentication more secure than token-based authentication?


I've been trying to understand the real differences between session and token authentication.

What I have gathered so far:

  1. In token authentication, nothing is stored in the server side. What this means is, that the actual token includes the password and username, as well as other possible information. And the server just decrypts the token, and then checks whether the username and password are correct. Am I right about this?? If the token includes the password and username, then how can the token still be different everytime?

  2. In session-based authentication, the session token is just a random (unique in time) id, that is mapped to the user in the server side. So that when the server receives the session_id (in cookie for example), it will check whether it maps to any user, and if it does, then the user is authenticated. So the session_id does not contain any user related information, that could be decrypted?

  3. In session authentication, the server will send back the user related information (not password) without encryption (unless https is used).

  4. In token authentication, the server will not send back direct user information, but just the token, which contains the user information, once decrypted?

I have a feeling that I haven't really understood how token and session authentication works. Something is definitely wrong in the statements above.

But, let's play along that the statements would be correct. Then wouldn't session-based authentication be more secure? Because in session based authentication, you do not reveal user password (in browser for example). Since it's just a random id, one cannot get information from it. But this is not the case with Token authentication. Since token authentication contains the password, if someone manages to decrypt it, he will get your password. So isn't the session authentication actually more safe than the token authentication, as it doesn't reveal password nor username information?


Solution

  • Your question has not an absolute answer YES/NO. For example session cookies are vulnerable to CSRF and tokens can be stolen with XSS injection. Both mechanism are also vulnerable to ManInTheMiddle if you do not use HTTPS. Therefore additional security measures are needed usually for each solutions. Depends on your use case.

    I guess you are talking about a token mechanism like JWT which is self-contained and protected to alterations because you said

    In token authentication, nothing is stored in the server side.

    But you are confusing some concepts. I will try to answer your additional questions using JWT tokens as reference. If not, most concepts also can be applied to opaque tokens

    In token authentication, nothing is stored in the server side. What this means is, that the actual token includes the password and username, as well as other possible information. And the server just decrypts the token, and then checks whether the username and password are correct. Am I right about this??

    The token is issued by server (not client) requiring users to present their credentials and digitally signed with server private key. The token includes an identifier of the principal in the sub claim and other fields of interest like expiration time or issuer. Never the password

    When the client send to token to authenticate, the server verifies the signature to determine the authenticity an has not been altered

    If the token includes the password and username, then how can the token still be different everytime?

    The token does not include the password. The token will be different due to some variant claims like expiration time exp or issued at iat. Also the computed signature will be different

    So the session_id does not contain any user related information, that could be decrypted?

    Yes, it is a ramdom sequence. Relationship with user server is stored on server

    In token authentication, the server will not send back direct user information, but just the token, which contains the user information, once decrypted?

    The JWT token includes some user information, but it is not encrypted, it is signed. If you need to hide the payload, JWT also allows to use JWE encryption

    But, let's play along that the statements would be correct. Then wouldn't session-based authentication be more secure? Because in session based authentication, you do not reveal user password (in browser for example). Since it's just a random id, one cannot get information from it. But this is not the case with Token authentication. Since token authentication contains the password, if someone manages to decrypt it, he will get your password. So isn't the session authentication actually more safe than the token authentication, as it doesn't reveal password nor username information?

    The base approach is wrong. Password is never included in the token. If you do not want to reveal user data you can use opaque tokens or JWE encryption with JWT. The proper solution depends on your use case. See my first paragraph