|
|
@ -2,6 +2,7 @@ import jwt |
|
|
|
import time |
|
|
|
import logging |
|
|
|
import sqlite3 as sqlite |
|
|
|
from sqlalchemy.exc import SQLAlchemyError |
|
|
|
from model import error |
|
|
|
from model import postgreSQLORM |
|
|
|
from model.postgreSQLORM import User |
|
|
@ -44,6 +45,7 @@ class User(db_conn.DBConn): |
|
|
|
def __check_token(self, user_id, db_token, token) -> bool: |
|
|
|
try: |
|
|
|
if db_token != token: |
|
|
|
# print('touch') |
|
|
|
return False |
|
|
|
jwt_text = jwt_decode(encoded_token=token, user_id=user_id) |
|
|
|
ts = jwt_text["timestamp"] |
|
|
@ -66,6 +68,7 @@ class User(db_conn.DBConn): |
|
|
|
## 为新注册的用户创建对象 |
|
|
|
new_user = postgreSQLORM.User(user_id=user_id,password=password,balance=0,token=token,terminal=terminal) |
|
|
|
self.session.add(new_user) |
|
|
|
self.session.commit() |
|
|
|
|
|
|
|
# self.conn.execute( |
|
|
|
# "INSERT into user(user_id, password, balance, token, terminal) " |
|
|
@ -76,104 +79,124 @@ class User(db_conn.DBConn): |
|
|
|
# return error.error_exist_user_id(user_id) |
|
|
|
return 200, "ok" |
|
|
|
|
|
|
|
# def check_token(self, user_id: str, token: str) -> (int, str): |
|
|
|
# cursor = self.conn.execute("SELECT token from user where user_id=?", (user_id,)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
# if row is None: |
|
|
|
# return error.error_authorization_fail() |
|
|
|
# db_token = row[0] |
|
|
|
# if not self.__check_token(user_id, db_token, token): |
|
|
|
# return error.error_authorization_fail() |
|
|
|
# return 200, "ok" |
|
|
|
|
|
|
|
# def check_password(self, user_id: str, password: str) -> (int, str): |
|
|
|
# cursor = self.conn.execute("SELECT password from user where user_id=?", (user_id,)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
# if row is None: |
|
|
|
# return error.error_authorization_fail() |
|
|
|
|
|
|
|
# if password != row[0]: |
|
|
|
# return error.error_authorization_fail() |
|
|
|
|
|
|
|
# return 200, "ok" |
|
|
|
|
|
|
|
# def login(self, user_id: str, password: str, terminal: str) -> (int, str, str): |
|
|
|
# token = "" |
|
|
|
# try: |
|
|
|
# code, message = self.check_password(user_id, password) |
|
|
|
# if code != 200: |
|
|
|
# return code, message, "" |
|
|
|
|
|
|
|
# token = jwt_encode(user_id, terminal) |
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "UPDATE user set token= ? , terminal = ? where user_id = ?", |
|
|
|
# (token, terminal, user_id), ) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
# return error.error_authorization_fail() + ("", ) |
|
|
|
# self.conn.commit() |
|
|
|
# except sqlite.Error as e: |
|
|
|
# return 528, "{}".format(str(e)), "" |
|
|
|
# except BaseException as e: |
|
|
|
# return 530, "{}".format(str(e)), "" |
|
|
|
# return 200, "ok", token |
|
|
|
|
|
|
|
# def logout(self, user_id: str, token: str) -> bool: |
|
|
|
# try: |
|
|
|
# code, message = self.check_token(user_id, token) |
|
|
|
# if code != 200: |
|
|
|
# return code, message |
|
|
|
|
|
|
|
# terminal = "terminal_{}".format(str(time.time())) |
|
|
|
# dummy_token = jwt_encode(user_id, terminal) |
|
|
|
|
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "UPDATE user SET token = ?, terminal = ? WHERE user_id=?", |
|
|
|
# (dummy_token, terminal, user_id), ) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
# return error.error_authorization_fail() |
|
|
|
|
|
|
|
# self.conn.commit() |
|
|
|
# except sqlite.Error as e: |
|
|
|
# return 528, "{}".format(str(e)) |
|
|
|
# except BaseException as e: |
|
|
|
# return 530, "{}".format(str(e)) |
|
|
|
# return 200, "ok" |
|
|
|
|
|
|
|
# def unregister(self, user_id: str, password: str) -> (int, str): |
|
|
|
# try: |
|
|
|
# code, message = self.check_password(user_id, password) |
|
|
|
# if code != 200: |
|
|
|
# return code, message |
|
|
|
|
|
|
|
# cursor = self.conn.execute("DELETE from user where user_id=?", (user_id,)) |
|
|
|
# if cursor.rowcount == 1: |
|
|
|
# self.conn.commit() |
|
|
|
# else: |
|
|
|
# return error.error_authorization_fail() |
|
|
|
# except sqlite.Error as e: |
|
|
|
# return 528, "{}".format(str(e)) |
|
|
|
# except BaseException as e: |
|
|
|
# return 530, "{}".format(str(e)) |
|
|
|
# return 200, "ok" |
|
|
|
|
|
|
|
# def change_password(self, user_id: str, old_password: str, new_password: str) -> bool: |
|
|
|
# try: |
|
|
|
# code, message = self.check_password(user_id, old_password) |
|
|
|
# if code != 200: |
|
|
|
# return code, message |
|
|
|
|
|
|
|
# terminal = "terminal_{}".format(str(time.time())) |
|
|
|
# token = jwt_encode(user_id, terminal) |
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "UPDATE user set password = ?, token= ? , terminal = ? where user_id = ?", |
|
|
|
# (new_password, token, terminal, user_id), ) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
# return error.error_authorization_fail() |
|
|
|
|
|
|
|
# self.conn.commit() |
|
|
|
# except sqlite.Error as e: |
|
|
|
# return 528, "{}".format(str(e)) |
|
|
|
# except BaseException as e: |
|
|
|
# return 530, "{}".format(str(e)) |
|
|
|
# return 200, "ok" |
|
|
|
def check_token(self, user_id: str, token: str) -> (int, str): |
|
|
|
row = self.session.query(postgreSQLORM.User.token).filter(postgreSQLORM.User.user_id==user_id).first() |
|
|
|
# cursor = self.conn.execute("SELECT token from user where user_id=?", (user_id,)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
# print(row) |
|
|
|
if row is None: |
|
|
|
# print('touch') |
|
|
|
return error.error_authorization_fail() |
|
|
|
db_token = row[0] |
|
|
|
# print(db_token) |
|
|
|
# print(token) |
|
|
|
if not self.__check_token(user_id, db_token, token): |
|
|
|
# print('touch') |
|
|
|
return error.error_authorization_fail() |
|
|
|
return 200, "ok" |
|
|
|
|
|
|
|
def check_password(self, user_id: str, password: str) -> (int, str): |
|
|
|
row = self.session.query(postgreSQLORM.User.password).filter(postgreSQLORM.User.user_id==user_id).first() |
|
|
|
# cursor = self.conn.execute("SELECT password from user where user_id=?", (user_id,)) |
|
|
|
# row = cursor.fetchone() |
|
|
|
if row is None: |
|
|
|
return error.error_authorization_fail() |
|
|
|
|
|
|
|
if password != row[0]: |
|
|
|
return error.error_authorization_fail() |
|
|
|
|
|
|
|
return 200, "ok" |
|
|
|
|
|
|
|
def login(self, user_id: str, password: str, terminal: str) -> (int, str, str): |
|
|
|
token = "" |
|
|
|
try: |
|
|
|
code, message = self.check_password(user_id, password) |
|
|
|
if code != 200: |
|
|
|
return code, message, "" |
|
|
|
|
|
|
|
token = jwt_encode(user_id, terminal) |
|
|
|
row = self.session.query(postgreSQLORM.User).filter_by(user_id=user_id).update({'token':token,'terminal':terminal}) |
|
|
|
|
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "UPDATE user set token= ? , terminal = ? where user_id = ?", |
|
|
|
# (token, terminal, user_id), ) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
if row == 0: |
|
|
|
return error.error_authorization_fail() + ("", ) |
|
|
|
# self.conn.commit() |
|
|
|
self.session.commit() |
|
|
|
except SQLAlchemyError as e: |
|
|
|
return 528, "{}".format(str(e)), "" |
|
|
|
except BaseException as e: |
|
|
|
return 530, "{}".format(str(e)), "" |
|
|
|
return 200, "ok", token |
|
|
|
|
|
|
|
def logout(self, user_id: str, token: str) -> bool: |
|
|
|
try: |
|
|
|
code, message = self.check_token(user_id, token) |
|
|
|
if code != 200: |
|
|
|
return code, message |
|
|
|
|
|
|
|
terminal = "terminal_{}".format(str(time.time())) |
|
|
|
dummy_token = jwt_encode(user_id, terminal) |
|
|
|
|
|
|
|
row = self.session.query(postgreSQLORM.User).filter_by(user_id=user_id).update({'token':dummy_token,'terminal':terminal}) |
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "UPDATE user SET token = ?, terminal = ? WHERE user_id=?", |
|
|
|
# (dummy_token, terminal, user_id), ) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
# print(row) |
|
|
|
if row == 0: |
|
|
|
return error.error_authorization_fail() |
|
|
|
|
|
|
|
# self.conn.commit() |
|
|
|
self.session.commit() |
|
|
|
except SQLAlchemyError as e: |
|
|
|
return 528, "{}".format(str(e)) |
|
|
|
except BaseException as e: |
|
|
|
return 530, "{}".format(str(e)) |
|
|
|
return 200, "ok" |
|
|
|
|
|
|
|
def unregister(self, user_id: str, password: str) -> (int, str): |
|
|
|
try: |
|
|
|
code, message = self.check_password(user_id, password) |
|
|
|
if code != 200: |
|
|
|
return code, message |
|
|
|
|
|
|
|
row = self.session.query(postgreSQLORM.User).filter(postgreSQLORM.User.user_id==user_id).delete() |
|
|
|
# cursor = self.conn.execute("DELETE from user where user_id=?", (user_id,)) |
|
|
|
# if cursor.rowcount == 1: |
|
|
|
if row == 1: |
|
|
|
self.session.commit() |
|
|
|
# self.conn.commit() |
|
|
|
else: |
|
|
|
return error.error_authorization_fail() |
|
|
|
except SQLAlchemyError as e: |
|
|
|
return 528, "{}".format(str(e)) |
|
|
|
except BaseException as e: |
|
|
|
return 530, "{}".format(str(e)) |
|
|
|
return 200, "ok" |
|
|
|
|
|
|
|
def change_password(self, user_id: str, old_password: str, new_password: str) -> bool: |
|
|
|
try: |
|
|
|
code, message = self.check_password(user_id, old_password) |
|
|
|
if code != 200: |
|
|
|
return code, message |
|
|
|
|
|
|
|
terminal = "terminal_{}".format(str(time.time())) |
|
|
|
token = jwt_encode(user_id, terminal) |
|
|
|
row = self.session.query(postgreSQLORM.User).filter_by(user_id=user_id).update({'password':new_password,'token':token,'terminal':terminal}) |
|
|
|
# cursor = self.conn.execute( |
|
|
|
# "UPDATE user set password = ?, token= ? , terminal = ? where user_id = ?", |
|
|
|
# (new_password, token, terminal, user_id), ) |
|
|
|
# if cursor.rowcount == 0: |
|
|
|
if row == 0: |
|
|
|
return error.error_authorization_fail() |
|
|
|
self.session.commit() |
|
|
|
# self.conn.commit() |
|
|
|
except SQLAlchemyError as e: |
|
|
|
return 528, "{}".format(str(e)) |
|
|
|
except BaseException as e: |
|
|
|
return 530, "{}".format(str(e)) |
|
|
|
return 200, "ok" |
|
|
|
|