I'm currently designing a small website and I'd like to implement the "forgot your password?" feature. Here is my idea:
forgot your password web page will have this:
Email: (input box)
(button: "Make temp password")
On "Make temp password" button click:
- Sanitize email input for SQLi/XSS
- Check to see that the email address exists in the database
- if not, tell user to make new account or check email spelling
- Create random password (ie “xh5MvQe”) and put it in database in the user’s temp password field.
- overwrite if already exists. do not touch the main password. the user will be able to log in with two passwords for the next 48 hours.
- Get the current UTC time plus 48 hours and put it in the user's temp password expiry date field in database. overwrite if already exists.
- Send email to user with the temp password
My database has these fields for the user table:
(primary key) user id
name
nickname
(non null, unique) email
(non null) password (encrypted)
temporary password
temporary password expiration
Once the user has the password, they can log in and change their main password.
Question is: is this a secure way to implement this feature? My main concern is sending a plain text password via email. I've seen the other way of generating a link with email hashed/timestamp/random id as a GET parameter, but I don't see how that's any more secure. Please correct me if I'm wrong.