In order to customize a user's application experience, it is common to create a user account and require that the user login or authenticate in order to gain access to the account. Such customization is undoubtedly useful, perhaps even central to application functionality. However, it immediately creates privacy issues and, where elevated access privileges are granted to certain users, it also creates security issues. Where elevated privileges enable access to valuable resources, attackers will attempt impersonation.So, how can we prevent impersonation? There are two main attack strategies: credential capture and post-authentication session hijacking. We'll look at session hijacking next post. Credential capture can take a number of forms. Simple guessing can usually be defeated by enforcing a delay between login attempts, and the recent practice of adding a captcha after every nth failed attempt is a wise practice to adopt. SSL encryption is the best defense against password sniffing. At one time, there wouldn't have been a whole lot more to say as part of a "basics" discussion, however today, persistent login has become a common user expectation.
A persistent login is simply the persistence of authentication across multiple sessions. That is, requiring reauthentication only for privilege escalation (as well as financial transactions, etc.) or after some extended period of time has passed (typically seven to fourteen days). Of course, cookies are the only method of persisting information between sessions, so persistent logins are implemented with a cookie that, at least temporarily, plays the role of authentication credentials. Improperly implemented, this can be a security disaster. In particular, storing a user's username and password in the cookie is a critical security breach.
So what is the secure way to implement persistent login? Well, there isn't a secure way to implement persistent login, but there are ways that are more secure than others. Best practice is to create an identifier corresponding to the username specifically for the authentication cookie, as well as a one-time authentication token that takes the place of the password, using code like the following:
Make sure you enforce persistent login timeouts on the server, and regenerate the one-time token after every successful authentication (i.e., make sure that it is a one-time token). If a user explicitly logs out, be sure to explicitly "delete" the authentication cookie by setting its value to
$salt = 'mysalt'; /* replace mysalt with a string unique to your app */
$persist_user_id = md5($salt . md5($username . $salt));
$persist_auth_token = md5(uniqid(rand(), TRUE));
deleted or the like.Image credit: tomswift46


