I.T. Security and Linux Administration

Sep 30 2013   5:35PM GMT

Create Your Own Two-Factor Authentication System: Saving Tokens

Eric Hansen Eric Hansen Profile: Eric Hansen

We’re almost there! Now we need to save the tokens we’ve generated as well as the phone number requesting it. While can be done anywhere I chose to plop it into the generate_token() method because we’d have to write the code twice otherwise. Luckily its a small fix, and we’ll finally be able to use our database stuff now.

Just before the “return token” line in the method add these lines:

        up = phone.select().where(phone.digits==number).get()
        up = phone.create(digits=number)
    records = SelectQuery(tokens).where(tokens.phone==up.id).count()
    if records:

The first try/except block attempts to get the phone information from the database and creates a record of it instead if nothing exists. We then try to update the token for the phone number and if that doesn’t work then we create a new record of it. Very simple and easy but is also vital to our service.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: