Created
January 9, 2016 01:17
-
-
Save cecepm/2dcb965ffd6f826dc90b to your computer and use it in GitHub Desktop.
playing with python otpauth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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