Advertisement
Guest User

Untitled

a guest
Sep 11th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. # roughly based on the logic shown at:
  2. # https://pythonspot.com/en/login-authentication-with-flask/
  3.  
  4. from flask import Flask, redirect, render_template, request, session
  5. from functools import wraps
  6. import os
  7.  
  8. app = Flask(__name__)
  9.  
  10. def widoLoginRequired(f):
  11. #this is the decorator for a route that is to be protected
  12. @wraps(f)
  13. def decorated_function(*args, **kwargs):
  14. if not session.get('logged_in'):
  15. session['next'] = request.endpoint
  16. return render_template('login.html')
  17. return f(*args, **kwargs)
  18. return decorated_function
  19.  
  20. @app.route('/login', methods=['POST'])
  21. #if this route is called as POST, it means that a password was submitted. Login and forward, or show error+ login screen
  22. def do_admin_login():
  23. # this should be replaced by hashed values, and/or a database function:
  24. passwords = {
  25. "admin": "enter passwords here",
  26. "user1": "then remove the the next line which says <passwords = {} > ",
  27. };
  28. passwords = {}
  29. if app.debug:
  30. passwords = {
  31. "user1": "pass1",
  32. "user2": "pass2",
  33. };
  34.  
  35. # validation of password:
  36. rightPassword = passwords.get(request.form['username']) # this is the password which the user *should* enter
  37. try:
  38. givenPassword = request.form['password'] # this is the password which the user *did* enter
  39. except:
  40. givenPassword = ""
  41. if (rightPassword is None):
  42. valid = False
  43. elif (rightPassword == givenPassword):
  44. valid = True
  45. else:
  46. valid = False
  47.  
  48. # act upon right or wrong user/pass combination:
  49. session.pop('wrong_password', None) # clear this flag
  50. if valid:
  51. session['logged_in'] = True
  52. session['user'] = request.form['username']
  53. else:
  54. session['wrong_password'] = "Password wrong...." # the value of this is irrelevant actually
  55. return render_template('login.html')
  56. return redirect (session.get('next'), code=302) # after successful login, make sure the browser shows the right URL
  57.  
  58. @app.route('/login', methods=['GET'])
  59. # direct call of this route - just displays the login form, will then forward to index, and automatically logs out if there was already a login
  60. def show_login_page():
  61. session['next']="/"
  62. session['logged_in'] = False
  63. return render_template('login.html')
  64.  
  65.  
  66. @app.route("/logout")
  67. def logout():
  68. session['logged_in'] = False
  69. return "Logged out!"
  70.  
  71.  
  72. #######################################################################
  73.  
  74.  
  75. @app.route('/')
  76. def home():
  77. return "This is the index page, unprotected"
  78.  
  79.  
  80. @app.route("/status")
  81. def showstatus():
  82. return """
  83. <h3>Status:</h3>
  84. <p>Logged in: %s</p>
  85. <p>User: %s</p>
  86. <p>Next: %s</p>
  87.  
  88. """ % (session.get('logged_in'), session.get('user'), session.get('next'))
  89.  
  90. @app.route("/protect1")
  91. @widoLoginRequired
  92. def protect1():
  93. return "This is the first protected page protect1"
  94.  
  95. @app.route("/protect2")
  96. @widoLoginRequired
  97. def protect2():
  98. return "This is the second protected page protect2"
  99.  
  100.  
  101. @app.route("/protect3")
  102. @widoLoginRequired
  103. def protect3():
  104. return "This is the third protected page protect3"
  105.  
  106.  
  107. if __name__ == "__main__":
  108. app.secret_key = os.urandom(24)
  109. app.run(debug=True)
  110.  
  111. <link rel="stylesheet" href="/static/loginstyle.css" type="text/css">
  112.  
  113. {% block body %}
  114.  
  115. {% if session['wrong_password'] %}
  116. <div class="login-screen">
  117. <p>Wrong password!</p>
  118. </div>
  119. {% endif %}
  120.  
  121. {% if session['logged_in'] %}
  122. <div class="login-screen">
  123. <p>You're logged in already!</p>
  124. </div>
  125. {% endif %}
  126.  
  127. <form action="/login" method="POST">
  128. <input type="text" name="next" value="12341234" hidden>
  129.  
  130. <div class="login">
  131. <div class="login-screen">
  132. <div class="app-title">
  133. <h1>Login</h1>
  134. </div>
  135.  
  136. <div class="login-form">
  137. <div class="control-group">
  138. <input type="text" class="login-field" value="" placeholder="username" name="username">
  139. <label class="login-field-icon fui-user" for="login-name"></label>
  140. </div>
  141.  
  142. <div class="control-group">
  143. <input type="password" class="login-field" value="" placeholder="password" name="password">
  144. <label class="login-field-icon fui-lock" for="login-pass"></label>
  145. </div>
  146.  
  147. <input type="submit" value="Log in" class="btn btn-primary btn-large btn-block" >
  148. <br>
  149. </div>
  150. </div>
  151. </div>
  152. </form>
  153.  
  154. {% endblock %}
  155.  
  156. * {
  157. box-sizing: border-box;
  158. }
  159.  
  160. *:focus {
  161. outline: none;
  162. }
  163. body {
  164. font-family: Arial;
  165. background-color: #3498DB;
  166. padding: 50px;
  167. }
  168. .login {
  169. margin: 20px auto;
  170. width: 300px;
  171. }
  172. .login-screen {
  173. background-color: #FFF;
  174. padding: 20px;
  175. border-radius: 5px
  176. }
  177.  
  178. .app-title {
  179. text-align: center;
  180. color: #777;
  181. }
  182.  
  183. .login-form {
  184. text-align: center;
  185. }
  186. .control-group {
  187. margin-bottom: 10px;
  188. }
  189.  
  190. input {
  191. text-align: center;
  192. background-color: #ECF0F1;
  193. border: 2px solid transparent;
  194. border-radius: 3px;
  195. font-size: 16px;
  196. font-weight: 200;
  197. padding: 10px 0;
  198. width: 250px;
  199. transition: border .5s;
  200. }
  201.  
  202. input:focus {
  203. border: 2px solid #3498DB;
  204. box-shadow: none;
  205. }
  206.  
  207. .btn {
  208. border: 2px solid transparent;
  209. background: #3498DB;
  210. color: #ffffff;
  211. font-size: 16px;
  212. line-height: 25px;
  213. padding: 10px 0;
  214. text-decoration: none;
  215. text-shadow: none;
  216. border-radius: 3px;
  217. box-shadow: none;
  218. transition: 0.25s;
  219. display: block;
  220. width: 250px;
  221. margin: 0 auto;
  222. }
  223.  
  224. .btn:hover {
  225. background-color: #2980B9;
  226. }
  227.  
  228. .login-link {
  229. font-size: 12px;
  230. color: #444;
  231. display: block;
  232. margin-top: 12px;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement