saaymeen
05/25/2023, 7:45 PMCREATE TABLE password_reset_tokens (
user_id VARCHAR(36) NOT NULL,
token VARCHAR(128) NOT NULL UNIQUE,
token_expiry BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (user_id, token),
); โ
This is likely to prevent users from having to include their ID in a request payload, because on password reset actions, generated tokens can directly be mapped back to the actual accounts. While I noticed that many popular websites actually include the account ID on password reset (e.g. Reddit in a JWT that is exposed in the reset URL sent to the user), this is just an observation and not my point.
How do you actually ensure that there are no collisions when generating the tokens before inserting them into a database? While it is statistically very, very unlikely to find collisions in a truly random 128 character string (or is it that just the hash for a token with less chars?), there still is a theoretical chance. Futhermore, if those tokens are actually simpler (4 or 6 character strings, think of app input forms or similar), chances of collisions become much higher.
Now I am wondering, does the approach in the blog post actually differ much from those where the strings are actually used to be directly entered in forms, does the application logic generate random tokens until a unused one is found (which would be one iteration most of the time anyway), or am I missing something?
Cheers!rp
05/26/2023, 5:49 AMsaaymeen
05/26/2023, 6:03 AM