document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import argparse
  2. import hashlib
  3. import logging
  4. import threading
  5.  
  6. from bottle import abort, error, HTTPResponse, request, response, route, run, static_file, template
  7. import requests
  8.  
  9. def success():
  10.     print \'OK...\'
  11.  
  12. def test():
  13.     PARAMS = {
  14.         \'password\': \'foo\',
  15.     }
  16.     resp = requests.post(\'http://localhost:58080/login/test\', data=PARAMS)
  17.     if resp.status_code != 201:
  18.         raise Exception(\'User creation failed\')
  19.     success()
  20.  
  21.     resp = requests.get(\'http://localhost:58080/login/test\', params=PARAMS)
  22.     if resp.status_code != 200:
  23.         raise Exception(\'login failed\')
  24.     success()
  25.  
  26.     resp = requests.get(\'http://localhost:58080/login/test\', params={\'password\':\'foobarbaz\'})
  27.     if resp.status_code != 401:
  28.         raise Exception(\'login failed with status code {0}\'.format(resp.status_code))
  29.     success()
  30.                        
  31.  
  32. def get_nonce():
  33.     PARAMS = {
  34.         \'num\': \'4\',
  35.         \'len\': \'16\',
  36.         \'lowerlpha\': \'on\',
  37.         \'rnd\': \'new\',
  38.         \'upperalpha\': \'on\',
  39.         \'digits\': \'on\',
  40.         \'format\': \'plain\',
  41.         \'unique\': \'on\'}
  42.     resp = requests.get(\'https://www.random.org/strings/\', params=PARAMS).content
  43.     ret = resp.replace(\'\\n\',\'\').replace(\' \',\'\')
  44.     logging.debug(ret)
  45.     return ret
  46.  
  47. NONCE = get_nonce()
  48.  
  49. @route(\'/login/<name>\', method=\'POST\')
  50. def greet(name):
  51.     if request.remote_addr != \'127.0.0.1\':
  52.         raise "Your IP, {0}, is banned from this endpoint".format(request.remote_addr)
  53.     pwd = request.POST.get(\'password\')
  54.     m = hashlib.sha256()
  55.     m.update(pwd)
  56.     USERS[name] = m.hexdigest()
  57.     response.status = 201
  58.     return response
  59.  
  60. @route(\'/\', method=\'GET\')
  61. @route(\'/login/<user>\', method=\'GET\')
  62. def login(user):
  63.     pwd = request.GET.get(\'password\')
  64.     if USERS[user] == my_hash(pwd):
  65.         response.set_cookie("account", user, secret=NONCE)
  66.         response.status = 200
  67.     else:
  68.         response.status = 401
  69.     return response
  70.  
  71. @error(404)
  72. def error404(error):
  73.     return \'Nothing here, sorry\'
  74.  
  75. def my_hash(passwd_candidate):
  76.     m = hashlib.sha256()
  77.     m.update(passwd_candidate)
  78.     password_candidate = m.hexdigest()
  79.     return password_candidate
  80.  
  81. def runthr():
  82.     run(host=\'localhost\', port=58080, debug=parsed.debug, reloader=parsed.debug)
  83.  
  84. USERS = {}
  85.  
  86. parser = argparse.ArgumentParser(description=\'Run the finance web application\')
  87. parser.add_argument(\'-v\',\'--debug\', action=\'store_true\', dest=\'debug\')
  88. parsed = parser.parse_args()
  89. if parsed.debug:
  90.     logging.basicConfig(level=logging.DEBUG)
  91.     test()
  92. else:
  93.     logging.basicConfig()
  94.     runthr()
');