|
@@ -6,16 +6,13 @@ from fastapi_jwt_auth import AuthJWT
|
|
from fastapi_jwt_auth.exceptions import AuthJWTException
|
|
from fastapi_jwt_auth.exceptions import AuthJWTException
|
|
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
|
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
|
import util.models
|
|
import util.models
|
|
-import pymysql
|
|
|
|
from passlib.context import CryptContext
|
|
from passlib.context import CryptContext
|
|
SECRET_KEY = "df2f77bd544240801a048bd4293afd8eeb7fff3cb7050e42c791db4b83ebadcd"
|
|
SECRET_KEY = "df2f77bd544240801a048bd4293afd8eeb7fff3cb7050e42c791db4b83ebadcd"
|
|
ALGORITHM = "HS256"
|
|
ALGORITHM = "HS256"
|
|
-pymysql.install_as_MySQLdb()
|
|
|
|
ACCESS_TOKEN_EXPIRE_DAYS = 5
|
|
ACCESS_TOKEN_EXPIRE_DAYS = 5
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
-db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
|
def get_user_id(token):
|
|
def get_user_id(token):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
credentials_exception = HTTPException(
|
|
credentials_exception = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Could not validate credentials",
|
|
detail="Could not validate credentials",
|
|
@@ -36,14 +33,14 @@ def get_user_id(token):
|
|
return user_id
|
|
return user_id
|
|
|
|
|
|
def check_user_exists( username):
|
|
def check_user_exists( username):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
if int(next(iter(db.query('SELECT COUNT(*) FROM AI_anchor.users WHERE username = "'+username+'"')))['COUNT(*)']) > 0:
|
|
if int(next(iter(db.query('SELECT COUNT(*) FROM AI_anchor.users WHERE username = "'+username+'"')))['COUNT(*)']) > 0:
|
|
return True
|
|
return True
|
|
else:
|
|
else:
|
|
return False
|
|
return False
|
|
|
|
|
|
def get_user( username: str):
|
|
def get_user( username: str):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
if not check_user_exists(username): # if user don't exist
|
|
if not check_user_exists(username): # if user don't exist
|
|
return False
|
|
return False
|
|
user_dict = next(
|
|
user_dict = next(
|
|
@@ -52,7 +49,7 @@ def get_user( username: str):
|
|
return user
|
|
return user
|
|
|
|
|
|
def user_register( user):
|
|
def user_register( user):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
table = db['users']
|
|
table = db['users']
|
|
user.password = get_password_hash(user.password)
|
|
user.password = get_password_hash(user.password)
|
|
table.insert(dict(user))
|
|
table.insert(dict(user))
|
|
@@ -64,7 +61,7 @@ def verify_password( plain_password, hashed_password):
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
|
|
|
|
def authenticate_user( username: str, password: str):
|
|
def authenticate_user( username: str, password: str):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
if not check_user_exists(username): # if user don't exist
|
|
if not check_user_exists(username): # if user don't exist
|
|
return False
|
|
return False
|
|
user_dict = next(iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
|
|
user_dict = next(iter(db.query('SELECT * FROM AI_anchor.users where username ="'+username+'"')))
|
|
@@ -74,7 +71,7 @@ def authenticate_user( username: str, password: str):
|
|
return user
|
|
return user
|
|
|
|
|
|
def get_user_role(id):
|
|
def get_user_role(id):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
state = 'SELECT * FROM user_role '\
|
|
state = 'SELECT * FROM user_role '\
|
|
'INNER JOIN users on user_role.user_id= users.id '\
|
|
'INNER JOIN users on user_role.user_id= users.id '\
|
|
'INNER JOIN role on user_role.role_id = role.id '\
|
|
'INNER JOIN role on user_role.role_id = role.id '\
|
|
@@ -85,25 +82,25 @@ def get_user_role(id):
|
|
return role_list
|
|
return role_list
|
|
|
|
|
|
def get_avatar_by_role(id):
|
|
def get_avatar_by_role(id):
|
|
-
|
|
|
|
- state = 'SELECT * FROM ('\
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
|
|
+ state = 'SELECT * FROM (
|
|
'SELECT role_id FROM AI_anchor.user_role '\
|
|
'SELECT role_id FROM AI_anchor.user_role '\
|
|
'INNER JOIN users on user_role.user_id= users.id '\
|
|
'INNER JOIN users on user_role.user_id= users.id '\
|
|
'INNER JOIN role on user_role.role_id = role.id '\
|
|
'INNER JOIN role on user_role.role_id = role.id '\
|
|
'WHERE AI_anchor.users.id=36) a'\
|
|
'WHERE AI_anchor.users.id=36) a'\
|
|
'INNER JOIN role on a.role_id = role.id'\
|
|
'INNER JOIN role on a.role_id = role.id'\
|
|
-'INNER JOIN avatar on role_avatar.id = avatar.id'
|
|
|
|
|
|
+'INNER JOIN avatar on role_avatar.id = avatar.id'\
|
|
role_list = []
|
|
role_list = []
|
|
for row in db.query(state):
|
|
for row in db.query(state):
|
|
role_list.append({'id':row['role_id'],'name':row['name']})
|
|
role_list.append({'id':row['role_id'],'name':row['name']})
|
|
return role_list
|
|
return role_list
|
|
#def add_role( username,role_id):
|
|
#def add_role( username,role_id):
|
|
- #
|
|
|
|
|
|
+ #db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
#user_role_table = db['user_role']
|
|
#user_role_table = db['user_role']
|
|
#user_role_table.insert({'user_id':,'role_id':role_id})
|
|
#user_role_table.insert({'user_id':,'role_id':role_id})
|
|
|
|
|
|
def get_user_id(token):
|
|
def get_user_id(token):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
credentials_exception = HTTPException(
|
|
credentials_exception = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Could not validate credentials",
|
|
detail="Could not validate credentials",
|
|
@@ -124,12 +121,12 @@ def get_user_id(token):
|
|
return user_id
|
|
return user_id
|
|
|
|
|
|
def get_id_by_email(email):
|
|
def get_id_by_email(email):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
user_dict = next(iter(db.query('SELECT * FROM users where email ="'+email+'"')))
|
|
user_dict = next(iter(db.query('SELECT * FROM users where email ="'+email+'"')))
|
|
return user_dict['id']
|
|
return user_dict['id']
|
|
|
|
|
|
def email_veri_pass(name):
|
|
def email_veri_pass(name):
|
|
-
|
|
|
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
|
|
user_dict = next(iter(db.query('SELECT * FROM users where username ="'+name+'"')))
|
|
user_dict = next(iter(db.query('SELECT * FROM users where username ="'+name+'"')))
|
|
user_obj = first(db.query('SELECT * FROM register_veri_code where user_id ="'+str(user_dict['id'])+'"'))
|
|
user_obj = first(db.query('SELECT * FROM register_veri_code where user_id ="'+str(user_dict['id'])+'"'))
|
|
if user_obj == None:
|
|
if user_obj == None:
|