I'm implementing my own 2 factor authentication using SMS.
We have a gift card service and the users are sent a link to their gift card. Every time they click on their link, we are verifying it's them by sending an SMS to their phone and prompting them for the code on our site.
I just want to make sure I haven't overlooked anything.
We first ask a user for their phone number and this is saved in a table called giftee_verification_details
:
orderId | phoneNumber
----------------------
1 | +35312345
The server then generates a random 4 digit code and saves in sms_codes
table:
id | orderId | code | createdTimestamp | codeValidUntil
---------------------------------------------------------------------------
1 | 255 | 8145 | 2019-01-17 10:28:58 | 2019-01-17 10:33:58
codeValidUntil
is the time the code is valid until - in this case 5 minutes from when the code was created.
We send this to the user's phone. When the user submits this, we check the code is correct and within the correct timeframe, then create a row in a table called sms_login
:
id | smsCodeId | createdTimestamp | loginValidUntil
-----------------------------------------------------------------------------
1 | 1 | 2019-01-17 10:39:25 | 2019-01-17 11:39:25
loginValidUntil
is the time this 'login' is valid for. So a user can view their gift card details if the current time is less than the last loginValidUntil
. If the current time is after this, we redirect, send them another sms code and ask for it in our UI.
Are there any problems with this approach?
sms_code
mainly). Once a code is validated, there is no reason to keep it around, its job is done and it can't be reused, so don't waste table space on it (keep it as a temp line in a memtable until then). That also means thesmsCodeId
column insms_login
is extra.