hd1

otp-backend

hd1
Apr 23rd, 2015
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. import csv
  2. import datetime
  3. import json
  4. import logging
  5. import requests
  6. import threading
  7. import web
  8.  
  9. urls = ('/', 'Random')
  10. app = web.application(urls, globals())
  11.  
  12. class Random:        
  13.     def GET(self):
  14.         retval = None
  15.         with open('.otp.json') as fin:
  16.             retval_ = json.loads(fin.read())
  17.             retval = retval_.values()
  18.         return retval
  19.  
  20.     def HEAD(self):
  21.         # Get 6-digit random.org digit
  22.         param = {'num':'1', 'min': '0', 'max': '1000000', 'col':'1', 'base':'10','format': 'plain', 'rnd': 'new'}
  23.         random_six_digits = requests.get('https://www.random.org/integers/', params=param, verify=False).content
  24.         password = random_six_digits.strip()
  25.        
  26.         expiration_ = datetime.datetime.now() + datetime.timedelta(minutes=1)
  27.         logging.debug("OTP until {1} is {0}".format(password, expiration_))
  28.         expiration = expiration_.strftime('%s')
  29.         with open('.otp.json','w') as fout:
  30.             fout.write(json.dumps({'password': password, 'expiration': expiration}))
  31.  
  32. if __name__ == '__main__':
  33.     logging.basicConfig(level=logging.FATAL)
  34.     r = Random()
  35.     r.expiration = datetime.datetime.min
  36.     r.HEAD()
  37.     threading.Timer(60, (lambda:r.HEAD())).start()
  38.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment