Skip to content

Instantly share code, notes, and snippets.

@cecepm
Created January 9, 2016 01:17
Show Gist options
  • Select an option

  • Save cecepm/2dcb965ffd6f826dc90b to your computer and use it in GitHub Desktop.

Select an option

Save cecepm/2dcb965ffd6f826dc90b to your computer and use it in GitHub Desktop.
playing with python otpauth
#!/usr/bin/env python
import click
import sys
from datetime import datetime, timedelta
from otpauth import OtpAuth
SECRET = "youHoyahc4Xee7geo6oo"
@click.command()
@click.argument('token')
def authenticate(token):
"""This script will validate otp token."""
auth = OtpAuth(SECRET)
# all token generated +/- 10 seconds will be valid
now1 = datetime.now() - timedelta(seconds=10)
now2 = datetime.now() + timedelta(seconds=10)
timestamp1 = int(now1.strftime("%s"))
timestamp2 = int(now2.strftime("%s"))
if (auth.valid_totp(token, timestamp=timestamp1) or
auth.valid_totp(token, timestamp=timestamp2)):
print "Congratulation, your token is valid"
sys.exit(0)
else:
print "Sorry, your token is not valid"
sys.exit(1)
if __name__ == '__main__':
authenticate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment