in my system users register with their email address and receive a temporary link to log in.
hostname/login/OYkwIJt6be1V5kMg10G
- The token must be generated from the email adress of user and valid for 2 hours.
- I don't want to store a session or token in a given database
In my mind I imagine creating the link like this:
$token = md5(((email + time() + SALT))
$link = $host.$path.$token
Is this a good way to process? What would be the cleanest, simplest and easiest method to manage?
The USER table schematic looks like this:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`email` varchar(100) NOT NULL,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`ip_adress` varchar(100) NOT NULL,
`status` varchar(100) NOT NULL,
`created` date NOT NULL,
`last_login` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
thank you for your recommendations
Edit
I opted for this solution according to your recommendations.
the best solution I found is using OpenSSL. It is built into PHP and you don't need any external library.
sample:
function encrypt($key, $payload) {
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($payload, 'aes-256-cbc', $key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}
function decrypt($key, $garble) {
list($encrypted_data, $iv) = explode('::', base64_decode($garble), 2);
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
}
$email = "[email protected]";
$expire = time()+7200;
$data = $email.'-'.$expire;
$secret = 'h6k8TG2x03';
/* encrypt */
$token = encrypt($secret, $data);
/* user auth */
http://domain.com/?token=YSBzbGlnaHRseSBsb25nZXIgdGVzdCBmb3IgdGV2ZXIK==
/* decrypt */
$decrypted = decrypt($secret, $_GET['token']);