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:

    try:
        up = phone.select().where(phone.digits==number).get()
    except:
        up = phone.create(digits=number)
    
    records = SelectQuery(tokens).where(tokens.phone==up.id).count()
    
    if records:
        tokens.update(token=token).where(tokens.phone==up.id).execute()
    else:
        tokens.create(token=token,phone=up.id)

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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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: