Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, request, jsonify, session
- import sqlite3
- from captcha import create_captcha
- import base64
- app = Flask(__name__)
- app.secret_key = '63b726a34c5945e4af65bced5d268456'
- conn = sqlite3.connect('local.db', isolation_level=None, check_same_thread=False)
- cur = conn.cursor()
- def login_required(f):
- def func(*args, **kwargs):
- if not session.get('loggedin'):
- return jsonify({
- 'msg': 'please login first'
- }), 401
- return f(*args, **kwargs)
- return func
- @app.route('/')
- @login_required
- def main():
- return 'Hello World, This is the locked endpoint'
- @app.route('/login')
- def login():
- captha_id = request.args.get('cid')
- captha_text = request.args.get('ctext')
- username = request.args.get('username')
- password = request.args.get('password')
- captcha_query = 'SELECT 1 FROM captchas where id=? and text=?'
- login_query = 'SELECT 1 FROM users where username=? and password=?'
- success = False
- msg = ''
- # if the captcha exists and valid
- if cur.execute(captcha_query, (captha_id, captha_text)).fetchone():
- # if the username and the password exists and valid
- if cur.execute(login_query, (username, password)).fetchone():
- session['loggedin'] = True
- msg = 'Logged in successfully'
- success = True
- else:
- msg = 'User details error'
- success = False
- else:
- msg = 'Captcha Error'
- success = False
- return jsonify({
- 'msg' msg,
- 'success': success
- })
- @app.route('/logout')
- def logout():
- session.clear()
- return jsonify({
- 'msg': 'ok'
- })
- @app.route('/generate')
- def generate():
- img, text = create_captcha()
- cur.execute('INSERT INTO captchas(text) values (?)', (text, ))
- return jsonify({
- 'id': cur.lastrowid,
- 'b64img': base64.b64encode(img.getvalue()).decode()
- })
- app.run(host='0.0.0.0', debug=True, port=80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement