main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/python2
  2. import json
  3. from flask import Flask
  4. from flask import request
  5. from flask import redirect
  6. from flask import make_response
  7. from flask import send_from_directory
  8. from flask import send_file
  9. from base64 import b64encode,b64decode
  10. from config import *
  11. from cookie_handler import *
  12. from crypt import *
  13. app = Flask(__name__)
  14. challs = {
  15. 'po1':validate_cookie_po1,
  16. 'po2':validate_cookie_po2,
  17. 'po3':validate_cookie_po3
  18. }
  19. '''
  20. This displays the dashboard of the crypto challanges.
  21. Used to redirect to the actual challanges
  22. '''
  23. @app.route("/")
  24. def index():
  25. return send_file('dashboard/po.html')
  26. '''
  27. Login Mask for all challanges, responds with the login mask and return the appropriate cookie validation function (for the challange)
  28. '''
  29. @app.route("/<path>")
  30. def login_mask(path):
  31. # check if there is already a cookie
  32. cookie = request.cookies.get("auth", False)
  33. if not cookie or path not in challs.keys():
  34. return send_from_directory('static', 'login.html')
  35. # if cookie - validate the cookie with the appropr. function
  36. return challs.get(path, notfound)(cookie)
  37. def notfound():
  38. return "404"
  39. '''
  40. Authentication function, takes username and password and generates the cookie.
  41. '''
  42. @app.route("/authenticate", methods=["POST"])
  43. def authenticate():
  44. referrer = request.referrer[-3:]
  45. username = request.form["user"]
  46. password = request.form["pass"]
  47. for user in app_users:
  48. if user.username == username and user.pwhash == hashlib.sha256(password + user.SALT).hexdigest():
  49. # correct username/password combination
  50. print('Debug: Got correct username/password combination')
  51. resp = make_response()
  52. resp.headers.add("Location", request.referrer)
  53. resp.status_code=302
  54. # crafting the user object, which will get decrypted as the auth cookie
  55. #user_obj = {"username":username, "access":app_users.get(username), 'secret':FLAGS[referrer]}
  56. #user_obj = str(user)
  57. user_obj = user
  58. break
  59. else:
  60. # wrong username/password combination, Error
  61. print('Debug: Wrong username/password combination, throwing error')
  62. resp = make_response("{\"Error\":\"Login failed\"}")
  63. resp.headers.add("Location", request.referrer)
  64. resp.status_code=302
  65. return resp
  66. # Challange selector
  67. if referrer == 'po1':
  68. cookie = create_cookie_po1(user_obj)
  69. elif referrer == 'po2':
  70. cookie = create_cookie_po2(user_obj)
  71. elif referrer == 'po3':
  72. print('itse le referer')
  73. cookie = create_cookie_po3(user_obj)
  74. else:
  75. return resp
  76. print('Debug: generated cookie for (%s): %s' %(referrer, cookie))
  77. # Setting the auth cookie and returning the response
  78. resp.set_cookie("auth", cookie)
  79. return resp
  80. @app.route("/logout")
  81. def logout():
  82. res = make_response()
  83. res.set_cookie("auth", "", expires=0)
  84. res.headers.add("Location", request.referrer)
  85. res.status_code=302
  86. return res
  87. app.run(debug=False)