Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from validate_email import validate_email
- from uuid import uuid4
- from flask import Flask, request, jsonify
- from pymysql import *
- from time import *
- from json import *
- from traceback import *
- from logging import debug
- app = Flask(__name__)
- JSON_PRETTY = True
- connection = connect(host = 'localhost', user = 'root', database = 'card_game', autocommit = True)
- cursor = connection.cursor()
- def Log(userId, message):
- try:
- sql = "SELECT id FROM log WHERE id = %s"
- cursor.execute(sql,userId)
- data = cursor.fetchall()
- if len(str(data)) == 2:
- sql = "INSERT INTO log (id, message) VALUES (%s,'')"
- cursor.execute(sql, userId)
- sql = "UPDATE log SET message = CONCAT(message, %s) WHERE id = %s"
- cursor.execute(sql, ("[{0}] {1} {2}".format(strftime('%d.%m.%y %X'), message, "\n"),userId))
- except Exception as e:
- print_exc()
- print(e)
- def JSON(*args, **kwargs):
- try:
- if JSON_PRETTY:
- return dumps(*args,**kwargs, sort_keys = True, indent = 4)
- else:
- return dumps(*args,**kwargs, sort_keys = False)
- except Exception as e:
- print_exc()
- print(e)
- def UpdateToken(username):
- try:
- with app.app_context():
- sql = "UPDATE players SET token = %s WHERE username = %s "
- cursor.execute(sql, (uuid4().hex, username))
- sql = "SELECT id, username, token FROM players WHERE username = %s"
- cursor.execute(sql, username)
- data = cursor.fetchall()
- for recive in data:
- rId, rName, rToken = recive
- return rId, rToken
- except Exception as e:
- print(e,print_exc())
- return print_exc()
- @app.errorhandler(500)
- def Error(e):
- #debug(e)
- return JSON({"error":"500 Server"})
- @app.errorhandler(404)
- def Error(e):
- #debug(e)
- return JSON({"error":"Invalid Method"})
- @app.route('/api/reg', methods = ["POST"])
- def Registration():
- try:
- username = request.form.get("username")
- password = request.form.get("password")
- passwordAgain = request.form.get("passwordAgain")
- mail = request.form.get("mail")
- if username == None: # USED FIELDS
- return 'username not USED'
- elif password == None:
- return 'password not USED'
- elif passwordAgain == None:
- return 'passwordAgain not USED'
- elif mail == None:
- return 'mail not USED'
- elif len(username) < 2:
- return 'username len < 2 '
- elif len(username) > 32:
- return 'username > 32 sybmols'
- elif password != passwordAgain:
- return 'password not equale passwordAgain'
- elif len(password) < 6:
- return 'password len < 6'
- elif len(password) > 32:
- return 'password > 32 sybmols'
- elif len(mail) < 3:
- return 'mail < 3'
- elif len(mail) > 32:
- return 'mail > 32 sybmols'
- elif not validate_email(mail):
- return 'mail not vaild'
- sql = "SELECT * FROM `players` WHERE username = %s LIMIT 1"
- cursor.execute(sql, username)
- data = cursor.fetchall()
- if len(str(data)) != 2:
- return 'user already registred'
- sql = "SELECT * FROM players WHERE mail = %s LIMIT 1"
- cursor.execute(sql, mail)
- data = cursor.fetchall()
- if len(str(data)) != 2:
- return 'mail already registred'
- sql = "INSERT INTO players (username, password, token, mail) VALUES (%s, %s, %s, %s)"
- cursor.execute(sql, (username, password, uuid4().hex, mail))
- sql = "SELECT id, username, token FROM players WHERE username = %s"
- cursor.execute(sql , username)
- data = cursor.fetchall()
- for recive in data:
- rId, rName, rToken = recive
- Log(rId,"REGISTRED")
- return JSON({"id": rId, "username": rName, "token": rToken})
- except Exception as e:
- print(e,print_exc())
- return print_exc()
- @app.route('/api/auth', methods=["POST"])
- def Auth():
- username = request.form.get("username")
- password = request.form.get("password")
- if username == None: # USED FIELDS
- return 'username not USED'
- elif password == None:
- return 'password not USED'
- elif len(username) < 2:
- return 'username len < 2 '
- elif len(username) > 32:
- return 'username len > 32 '
- elif len(password) < 6:
- return 'password len < 6'
- elif len(password) > 32:
- return 'password len > 32 '
- sql = "SELECT token FROM players WHERE (username = %s AND password = %s)"
- cursor.execute(sql, (username, password))
- data = cursor.fetchall()
- if len(str(data)) == 2:
- return JSON({"error": "incorrect data"})
- else:
- id, token = UpdateToken(username)
- Log(id, "AUTH")
- return JSON({"id": id, "token": token})
- if __name__ == '__main__':
- # app.debug = True
- app.run("127.0.0.2", 80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement