Spaces:
Sleeping
Sleeping
auth and is_admin
Browse files- auth.py +17 -7
- db_model/__init__.py +0 -0
- db_model/user.py +9 -0
- routers/users_v1.py +18 -9
auth.py
CHANGED
@@ -2,29 +2,39 @@ from fastapi import Depends, HTTPException, status
|
|
2 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
3 |
from global_state import get
|
4 |
from db.tbs_db import TbsDb
|
|
|
5 |
|
6 |
# 创建一个 HTTPBearer 实例
|
7 |
security = HTTPBearer()
|
8 |
|
9 |
-
def
|
10 |
token = credentials.credentials
|
11 |
token = credentials.credentials
|
12 |
-
# 假设你有一个函数来验证Token并返回用户
|
13 |
-
|
14 |
-
|
|
|
15 |
raise HTTPException(
|
16 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
17 |
detail="Invalid authentication credentials",
|
18 |
headers={"WWW-Authenticate": "Bearer"},
|
19 |
)
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def validate_token(token: str):
|
23 |
db_module_filename = f"{get('project_root')}/db/cloudflare.py"
|
24 |
query = f"SELECT * FROM users where api_key='{token}'"
|
25 |
response = TbsDb(db_module_filename, "Cloudflare").get_item(query)
|
|
|
26 |
result = response['result'][0]['results']
|
27 |
if len(result) == 0:
|
28 |
return None
|
29 |
-
|
30 |
-
|
|
|
|
2 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
3 |
from global_state import get
|
4 |
from db.tbs_db import TbsDb
|
5 |
+
from db_model.user import UserModel
|
6 |
|
7 |
# 创建一个 HTTPBearer 实例
|
8 |
security = HTTPBearer()
|
9 |
|
10 |
+
def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
11 |
token = credentials.credentials
|
12 |
token = credentials.credentials
|
13 |
+
# 假设你有一个函数来验证Token并返回用户
|
14 |
+
user = validate_token(token)
|
15 |
+
print(f"\n\n\n\n{user}")
|
16 |
+
if user is None:
|
17 |
raise HTTPException(
|
18 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
19 |
detail="Invalid authentication credentials",
|
20 |
headers={"WWW-Authenticate": "Bearer"},
|
21 |
)
|
22 |
+
if user.is_admin == 0:
|
23 |
+
raise HTTPException(
|
24 |
+
status_code=status.HTTP_403_FORBIDDEN,
|
25 |
+
detail="Have no permission",
|
26 |
+
headers={"WWW-Authenticate": "Bearer"},
|
27 |
+
)
|
28 |
+
return user
|
29 |
|
30 |
def validate_token(token: str):
|
31 |
db_module_filename = f"{get('project_root')}/db/cloudflare.py"
|
32 |
query = f"SELECT * FROM users where api_key='{token}'"
|
33 |
response = TbsDb(db_module_filename, "Cloudflare").get_item(query)
|
34 |
+
print(f"\n\n\n\n{response}")
|
35 |
result = response['result'][0]['results']
|
36 |
if len(result) == 0:
|
37 |
return None
|
38 |
+
result = result[0]
|
39 |
+
result=UserModel(**result)
|
40 |
+
return result
|
db_model/__init__.py
ADDED
File without changes
|
db_model/user.py
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel
|
2 |
+
|
3 |
+
class UserModel(BaseModel):
|
4 |
+
id: int = 0
|
5 |
+
username: str
|
6 |
+
password: str
|
7 |
+
email: str
|
8 |
+
nikename: str = None
|
9 |
+
is_admin: int = 0
|
routers/users_v1.py
CHANGED
@@ -4,26 +4,33 @@ import uuid
|
|
4 |
|
5 |
from global_state import get
|
6 |
from db.tbs_db import TbsDb
|
7 |
-
from auth import
|
|
|
8 |
|
9 |
router = APIRouter()
|
10 |
|
11 |
db_module_filename = f"{get('project_root')}/db/cloudflare.py"
|
12 |
|
13 |
-
@router.get("/users")
|
14 |
-
async def read_users(current_user_id: int = Depends(get_current_user_id)):
|
15 |
-
query = "SELECT * FROM users"
|
16 |
-
response = TbsDb(db_module_filename, "Cloudflare").get_list(query)
|
17 |
-
return response
|
18 |
-
|
19 |
class User(BaseModel):
|
|
|
20 |
username: str
|
21 |
password: str
|
22 |
email: str
|
23 |
nikename: str = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
@router.post("/users")
|
26 |
-
async def create_user(user:
|
27 |
username = user.username
|
28 |
password = user.password
|
29 |
email = user.email
|
@@ -34,10 +41,12 @@ async def create_user(user: User, current_user_id: int = Depends(get_current_use
|
|
34 |
api_key = f'airs-{uuid.uuid4()}'
|
35 |
query = f"INSERT INTO users (username, password, email, nikename, api_key) VALUES ('{username}', '{password}', '{email}', '{nikename}', '{api_key}')"
|
36 |
response = TbsDb(db_module_filename, "Cloudflare").add_item(query)
|
|
|
|
|
37 |
return response
|
38 |
|
39 |
@router.get("/users/{id}")
|
40 |
-
async def read_user(id:int,
|
41 |
query = f"SELECT * FROM users where id={id}"
|
42 |
response = TbsDb(db_module_filename, "Cloudflare").get_item(query)
|
43 |
return response
|
|
|
4 |
|
5 |
from global_state import get
|
6 |
from db.tbs_db import TbsDb
|
7 |
+
from auth import get_current_user
|
8 |
+
from db_model.user import UserModel
|
9 |
|
10 |
router = APIRouter()
|
11 |
|
12 |
db_module_filename = f"{get('project_root')}/db/cloudflare.py"
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
class User(BaseModel):
|
15 |
+
id: int = 0
|
16 |
username: str
|
17 |
password: str
|
18 |
email: str
|
19 |
nikename: str = None
|
20 |
+
is_admin: int = 0
|
21 |
+
|
22 |
+
@router.get("/users")
|
23 |
+
# async def read_users(current_user_id: int = Depends(get_current_user_id)):
|
24 |
+
# query = "SELECT * FROM users"
|
25 |
+
# response = TbsDb(db_module_filename, "Cloudflare").get_list(query)
|
26 |
+
# return response
|
27 |
+
async def read_user(current_user: UserModel = Depends(get_current_user)):
|
28 |
+
query = "SELECT * FROM users"
|
29 |
+
response = TbsDb(db_module_filename, "Cloudflare").get_list(query)
|
30 |
+
return response
|
31 |
|
32 |
@router.post("/users")
|
33 |
+
async def create_user(user: UserModel):
|
34 |
username = user.username
|
35 |
password = user.password
|
36 |
email = user.email
|
|
|
41 |
api_key = f'airs-{uuid.uuid4()}'
|
42 |
query = f"INSERT INTO users (username, password, email, nikename, api_key) VALUES ('{username}', '{password}', '{email}', '{nikename}', '{api_key}')"
|
43 |
response = TbsDb(db_module_filename, "Cloudflare").add_item(query)
|
44 |
+
if response['success']==True:
|
45 |
+
response['api_key'] = api_key
|
46 |
return response
|
47 |
|
48 |
@router.get("/users/{id}")
|
49 |
+
async def read_user(id:int, current_user: UserModel = Depends(get_current_user)):
|
50 |
query = f"SELECT * FROM users where id={id}"
|
51 |
response = TbsDb(db_module_filename, "Cloudflare").get_item(query)
|
52 |
return response
|