Advertisement
avramit

my shit

Sep 12th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. from flask import Flask, request, jsonify, session
  2. import sqlite3
  3. from captcha import create_captcha
  4. import base64
  5.  
  6. app = Flask(__name__)
  7. app.secret_key = '63b726a34c5945e4af65bced5d268456'
  8. conn = sqlite3.connect('local.db', isolation_level=None, check_same_thread=False)
  9. cur = conn.cursor()
  10.  
  11. def login_required(f):
  12.     def func(*args, **kwargs):
  13.         if not session.get('loggedin'):
  14.             return jsonify({
  15.                 'msg': 'please login first'
  16.             }), 401
  17.         return f(*args, **kwargs)
  18.     return func
  19.  
  20.  
  21. @app.route('/')
  22. @login_required
  23. def main():
  24.     return 'Hello World, This is the locked endpoint'
  25.  
  26.  
  27. @app.route('/login')
  28. def login():
  29.     captha_id = request.args.get('cid')
  30.     captha_text = request.args.get('ctext')
  31.     username = request.args.get('username')
  32.     password = request.args.get('password')
  33.  
  34.     captcha_query = 'SELECT 1 FROM captchas where id=? and text=?'
  35.     login_query = 'SELECT 1 FROM users where username=? and password=?'
  36.  
  37.     success = False
  38.     msg = ''
  39.  
  40.     # if the captcha exists and valid
  41.     if cur.execute(captcha_query, (captha_id, captha_text)).fetchone():
  42.         # if the username and the password exists and valid
  43.         if cur.execute(login_query, (username, password)).fetchone():
  44.             session['loggedin'] = True
  45.             msg = 'Logged in successfully'
  46.             success = True
  47.         else:
  48.             msg = 'User details error'
  49.             success = False
  50.     else:
  51.         msg = 'Captcha Error'
  52.         success = False
  53.        
  54.     return jsonify({
  55.         'msg' msg,
  56.         'success': success 
  57.     })
  58.  
  59.  
  60. @app.route('/logout')
  61. def logout():
  62.     session.clear()
  63.     return jsonify({
  64.         'msg': 'ok'
  65.     })
  66.  
  67.  
  68. @app.route('/generate')
  69. def generate():
  70.     img, text = create_captcha()
  71.     cur.execute('INSERT INTO captchas(text) values (?)', (text, ))
  72.  
  73.     return jsonify({
  74.         'id': cur.lastrowid,
  75.         'b64img': base64.b64encode(img.getvalue()).decode()
  76.     })
  77.  
  78.  
  79. app.run(host='0.0.0.0', debug=True, port=80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement