The Remember Me feature is always an added security risk.
Because, like in a session, you have only one identifier, which is sufficient not only to identify the user (who is this?), But also to authenticate this user (is he / she really?) Without actual authentication.
But in contrast to a session that has (or should have) just a short lifespan (basically less than an hour), and the identifier (or should be) changed periodically (based on time and as necessary due to the authenticity / authority of the state change), the identifier "remember me" is valid for several days, if not even months or years! And this long duration creates an additional security risk.
So, before asking how to implement this āremember meā feature, you should ask yourself if you really need an extra security risk. It mainly depends on the assets that your application has, and what authentication is for, and if you want to risk the impersonations / theft of personal data that the "remember me" function represents.
If so, make sure you provide basic security using HTTPS and set the HTTPOnly flag and the safe flag in your cookies . Then you can do the following to create such a āremember meā function:
Authentication Request
If the user authenticated via HTTPS and set the āremember meā option, generate a random storage token, save it on the server side in the āremember meā database and set a cookie for me with a saved flag with this value. Then start a new session and set the Remember Me flag.
Any other requests
- If there is no current session, redirect to the Remember Me page via HTTPS, which checks if I have a cookie. If there is a token that I remember, it is valid, invalid, generates a new one, stores it in the āremember meā database, sets a cookie with this new token and creates a new session with the āremember meā checkbox selected. Otherwise, redirect to the login page.
- If the current session is invalid (be sure to use the invalid session ), redirect to the Remember Me page via HTTPS if the Remember Me flag is set; otherwise redirect to the login page.
At the same time, authentication is provided through HTTPS, both the initial authentication and the āremember meā authentication. And the user is authentic only during the current session; if it expires, the user must re-authenticate either with the remember me token or with his credentials. And since you remember that tokens are stored in the database, the user can cancel any existing commemorative token.
Gumbo
source share