Advertisement
churkabes

Untitled

Mar 3rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.17 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. from validate_email import validate_email
  6. from uuid import uuid4
  7.  
  8. from flask import Flask, request, jsonify
  9. from pymysql import *
  10.  
  11. from time import *
  12. from json import *
  13. from traceback import *
  14. from logging import debug
  15.  
  16. app = Flask(__name__)
  17.  
  18. JSON_PRETTY = True
  19.  
  20. connection = connect(host = 'localhost', user = 'root', database = 'card_game', autocommit = True)
  21. cursor = connection.cursor()
  22.  
  23. def Log(userId, message):
  24.     try:
  25.         sql = "SELECT id FROM log WHERE id = %s"
  26.         cursor.execute(sql,userId)
  27.         data = cursor.fetchall()
  28.  
  29.         if len(str(data)) == 2:
  30.             sql = "INSERT INTO log (id, message) VALUES (%s,'')"
  31.             cursor.execute(sql, userId)
  32.  
  33.         sql = "UPDATE log SET message = CONCAT(message, %s) WHERE id = %s"
  34.         cursor.execute(sql, ("[{0}] {1} {2}".format(strftime('%d.%m.%y %X'), message, "\n"),userId))
  35.     except Exception as e:
  36.         print_exc()
  37.         print(e)
  38.  
  39.  
  40. def JSON(*args, **kwargs):
  41.     try:
  42.         if JSON_PRETTY:
  43.             return dumps(*args,**kwargs, sort_keys = True, indent = 4)
  44.         else:
  45.             return dumps(*args,**kwargs, sort_keys = False)
  46.     except Exception as e:
  47.         print_exc()
  48.         print(e)
  49.  
  50. def UpdateToken(username):
  51.     try:
  52.         with app.app_context():
  53.             sql = "UPDATE players SET token = %s WHERE username = %s "
  54.             cursor.execute(sql, (uuid4().hex, username))
  55.  
  56.             sql = "SELECT id, username, token FROM players WHERE username = %s"
  57.             cursor.execute(sql, username)
  58.             data = cursor.fetchall()
  59.  
  60.             for recive in data:
  61.                 rId, rName, rToken = recive
  62.                 return rId, rToken
  63.     except Exception as e:
  64.         print(e,print_exc())
  65.         return print_exc()
  66.  
  67. @app.errorhandler(500)
  68. def Error(e):
  69.     #debug(e)
  70.     return JSON({"error":"500 Server"})
  71.  
  72.  
  73. @app.errorhandler(404)
  74. def Error(e):
  75.     #debug(e)
  76.     return JSON({"error":"Invalid Method"})
  77.  
  78.  
  79.  
  80. @app.route('/api/reg', methods = ["POST"])
  81. def Registration():
  82.     try:
  83.         username = request.form.get("username")
  84.         password = request.form.get("password")
  85.         passwordAgain = request.form.get("passwordAgain")
  86.         mail = request.form.get("mail")
  87.  
  88.  
  89.         if username == None: # USED FIELDS
  90.             return 'username not USED'
  91.         elif password == None:
  92.             return 'password not USED'
  93.         elif passwordAgain == None:
  94.             return 'passwordAgain not USED'
  95.         elif mail == None:
  96.             return 'mail not USED'
  97.  
  98.         elif len(username) < 2:
  99.             return 'username len < 2 '
  100.         elif len(username) > 32:
  101.             return 'username > 32 sybmols'
  102.  
  103.         elif password != passwordAgain:
  104.             return 'password not equale passwordAgain'
  105.         elif len(password) < 6:
  106.             return 'password len < 6'
  107.         elif len(password) > 32:
  108.             return 'password > 32 sybmols'
  109.  
  110.         elif len(mail) < 3:
  111.             return 'mail < 3'
  112.         elif len(mail) > 32:
  113.             return 'mail > 32 sybmols'
  114.  
  115.         elif not validate_email(mail):
  116.             return 'mail not vaild'
  117.  
  118.         sql = "SELECT * FROM `players` WHERE username = %s LIMIT 1"
  119.         cursor.execute(sql, username)
  120.         data = cursor.fetchall()
  121.  
  122.         if len(str(data)) != 2:
  123.             return 'user already registred'
  124.  
  125.         sql = "SELECT * FROM players WHERE mail = %s LIMIT 1"
  126.         cursor.execute(sql, mail)
  127.         data = cursor.fetchall()
  128.  
  129.         if len(str(data)) != 2:
  130.             return 'mail already registred'
  131.  
  132.         sql = "INSERT INTO players (username, password, token, mail) VALUES (%s, %s, %s, %s)"
  133.         cursor.execute(sql, (username, password, uuid4().hex, mail))
  134.  
  135.  
  136.  
  137.         sql = "SELECT id, username, token FROM players WHERE username = %s"
  138.         cursor.execute(sql , username)
  139.         data = cursor.fetchall()
  140.  
  141.         for recive in data:
  142.             rId, rName, rToken = recive
  143.             Log(rId,"REGISTRED")
  144.             return JSON({"id": rId, "username": rName, "token": rToken})
  145.     except Exception as e:
  146.         print(e,print_exc())
  147.         return print_exc()
  148.  
  149.  
  150. @app.route('/api/auth', methods=["POST"])
  151. def Auth():
  152.     username = request.form.get("username")
  153.     password = request.form.get("password")
  154.  
  155.     if username == None: # USED FIELDS
  156.         return 'username not USED'
  157.     elif password == None:
  158.         return 'password not USED'
  159.  
  160.     elif len(username) < 2:
  161.         return 'username len < 2 '
  162.     elif len(username) > 32:
  163.         return 'username len > 32 '
  164.  
  165.     elif len(password) < 6:
  166.         return 'password len < 6'
  167.     elif len(password) > 32:
  168.         return 'password len > 32 '
  169.  
  170.  
  171.     sql = "SELECT token FROM players WHERE (username = %s AND password = %s)"
  172.     cursor.execute(sql, (username, password))
  173.     data = cursor.fetchall()
  174.  
  175.     if len(str(data)) == 2:
  176.         return JSON({"error": "incorrect data"})
  177.     else:
  178.         id, token = UpdateToken(username)
  179.         Log(id, "AUTH")
  180.         return JSON({"id": id, "token": token})
  181.  
  182.  
  183. if __name__ == '__main__':
  184.    # app.debug = True
  185.     app.run("127.0.0.2", 80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement