Guest User

Untitled

a guest
Nov 2nd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from flask import Flask, render_template_string, request, Response, session, redirect
  3. import logging, os, sys, time
  4. import random
  5.  
  6. app = Flask(__name__)
  7.  
  8. app.config["SECRET_KEY"] = "SECRETKEYSECRETKEYSECRETKEYSECRETKEYSECRETKEY"
  9. app.config["DEBUG"] = os.environ.get("FLASK_DEBUG", True)
  10. app.config["JSON_AS_ASCII"] = False
  11.  
  12.  
  13.  
  14. html_login = """
  15. {% if auth_name %}
  16. {% set NAME = auth_name %}
  17. {% else %}
  18. {% set NAME = 'unknown' %}
  19. {% endif %}
  20. {% if not status %}
  21. {% set status = 'UNKNOWN' %}
  22. {% endif %}
  23. <!DOCTYPE html>
  24. <html>
  25. <head><title>AD AUTH</title></head>
  26. <body>
  27. <div> <h2>Hello, <small>{{NAME}}</small>.</h2><p>You are in {{status}} status.</p><p>and otp status is {{otp_status}}</p> </div>
  28. <div>
  29. <form action="/login" method="post">
  30. <input name="username" placeholder="username" type="text">
  31. <input type="password" name="password">
  32. <input type="submit" name="submit">
  33. </form>
  34. </div>
  35. </body>
  36. </html>
  37. """
  38.  
  39.  
  40. html_otp = """
  41. {% if auth_name %}
  42. {% set NAME = auth_name %}
  43. {% else %}
  44. {% set NAME = 'unknown' %}
  45. {% endif %}
  46. {% if not status %}
  47. {% set status = 'UNKNOWN' %}
  48. {% endif %}
  49. {% if not otp_status %}
  50. {% set otp_status = 'UNKNOWN' %}
  51. {% endif %}
  52. <!DOCTYPE html>
  53. <html>
  54. <head><title>AD AUTH</title></head>
  55. <body>
  56. <div> <h2>Hello, <small>{{NAME}}</small>.</h2><p>You are in {{status}} status.</p><p>and otp status is {{otp_status}}</p> </div>
  57. <div>
  58. <form action="/otp" method="post">
  59. <input name="otp" placeholder="otp" type="text">
  60. <input type="submit" name="submit">
  61. </form>
  62. </div>
  63. </body>
  64. </html>
  65. """
  66.  
  67. html_result = """
  68. {% if auth_name %}
  69. {% set NAME = auth_name %}
  70. {% else %}
  71. {% set NAME = 'unknown' %}
  72. {% endif %}
  73. {% if not status %}
  74. {% set status = 'UNKNOWN' %}
  75. {% endif %}
  76. {% if not otp_status %}
  77. {% set otp_status = 'UNKNOWN' %}
  78. {% endif %}
  79. <!DOCTYPE html>
  80. <html>
  81. <head><title>AD AUTH</title></head>
  82. <body>
  83. <div>
  84. <h2>Hello, <small>{{NAME}}</small>.</h2>
  85. <p>You are in {{status}} status.</p>
  86. <p>and otp status is {{otp_status}}</p>
  87. <p><a href="/">Go to home?</a></p>
  88. </div>
  89. </body>
  90. </html>
  91. """
  92.  
  93.  
  94. def generate_code():
  95. return str(random.randrange(100000, 999999))
  96.  
  97.  
  98. def send_otp_code():
  99. otp_code = generate_code()
  100. app.logger.critical('OTP: %s' % otp_code)
  101. session['otp_code'] = otp_code
  102. return otp_code
  103.  
  104.  
  105. def validate_user(username, password):
  106. if username == 'foo' and password == 'bar':
  107. return True
  108. else:
  109. return False
  110.  
  111.  
  112. def validate_otp(otp_password):
  113. if otp_password == session['otp_code']:
  114. return True
  115. else:
  116. return False
  117.  
  118.  
  119.  
  120. @app.route('/')
  121. def home():
  122. app.logger.info("route =>'/' - hit!")
  123. return render_template_string(html_login)
  124.  
  125.  
  126.  
  127. @app.route('/login', methods=['GET','POST'])
  128. def login():
  129. app.logger.info("route =>'/login' - hit!")
  130. if request.method == 'GET':
  131. return redirect('/')
  132. else:
  133. session['auth_user'] = request.form['username']
  134. session['auth_pass'] = request.form['password']
  135. app.logger.info("login: %s" % session['auth_user'])
  136. status = validate_user(username=session['auth_user'], password=session['auth_pass'])
  137. if not status:
  138. session['status'] = 'UNAUTHORIZED'
  139. return render_template_string(html_login, auth_name=session['auth_user'], status=session['status'])
  140. else:
  141. session['status'] = 'AUTHORIZED'
  142. return redirect('/otp')
  143.  
  144.  
  145.  
  146. @app.route('/otp', methods=['GET','POST'])
  147. def login_otp():
  148. app.logger.info("route =>'/otp' - hit!")
  149. if request.method == 'GET':
  150. send_otp_code()
  151. return render_template_string(html_otp, auth_name=session['auth_user'], status=session['status'])
  152. else:
  153. session['otp'] = request.form['otp']
  154. otp_status = validate_otp(session['otp'])
  155. if not otp_status:
  156. session['otp_status'] = 'INVALID'
  157. else:
  158. session['otp_status'] = 'VALID'
  159. return render_template_string(html_result, auth_name=session['auth_user'], status=session['status'], otp_status=session['otp_status'])
  160.  
  161.  
  162.  
  163. if __name__ == '__main__':
  164. app.run(debug=True)
Add Comment
Please, Sign In to add comment