mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-05 03:39:59 +08:00
修改优化开箱显示图片
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
from configs.path_config import DATA_PATH
|
||||
from typing import Optional
|
||||
from starlette import status
|
||||
|
||||
import nonebot
|
||||
from fastapi import Depends, HTTPException
|
||||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||
from pydantic import BaseModel
|
||||
from configs.config import Config
|
||||
from jose import JWTError, jwt
|
||||
import nonebot
|
||||
from pydantic import BaseModel
|
||||
from starlette import status
|
||||
|
||||
from ..config import Result
|
||||
from configs.config import Config
|
||||
from configs.path_config import DATA_PATH
|
||||
|
||||
from ..config import Result, router
|
||||
|
||||
app = nonebot.get_app()
|
||||
|
||||
@@ -19,14 +21,14 @@ SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
|
||||
ALGORITHM = "HS256"
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="webui/login")
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="api/login")
|
||||
|
||||
|
||||
token_file = DATA_PATH / "web_ui" / "token.json"
|
||||
token_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
token_data = {"token": []}
|
||||
if token_file.exists():
|
||||
token_data = json.load(open(token_file, 'r', encoding='utf8'))
|
||||
token_data = json.load(open(token_file, "r", encoding="utf8"))
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
@@ -39,11 +41,6 @@ class Token(BaseModel):
|
||||
token_type: str
|
||||
|
||||
|
||||
# USER_LIST = [
|
||||
# User(username="admin", password="123")
|
||||
# ]
|
||||
|
||||
|
||||
def get_user(uname: str) -> Optional[User]:
|
||||
username = Config.get_config("web-ui", "username")
|
||||
password = Config.get_config("web-ui", "password")
|
||||
@@ -59,24 +56,26 @@ form_exception = HTTPException(
|
||||
|
||||
|
||||
def create_token(user: User, expires_delta: Optional[timedelta] = None):
|
||||
expire = datetime.utcnow() + expires_delta or timedelta(minutes=15)
|
||||
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=15))
|
||||
return jwt.encode(
|
||||
claims={"sub": user.username, "exp": expire},
|
||||
key=SECRET_KEY,
|
||||
algorithm=ALGORITHM
|
||||
algorithm=ALGORITHM,
|
||||
)
|
||||
|
||||
|
||||
@app.post("/webui/login")
|
||||
@router.post("/login")
|
||||
async def login_get_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
||||
user: User = get_user(form_data.username)
|
||||
user = get_user(form_data.username)
|
||||
if not user or user.password != form_data.password:
|
||||
raise form_exception
|
||||
access_token = create_token(user=user, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
|
||||
access_token = create_token(
|
||||
user=user, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
)
|
||||
token_data["token"].append(access_token)
|
||||
if len(token_data["token"]) > 3:
|
||||
token_data["token"] = token_data["token"][1:]
|
||||
with open(token_file, 'w', encoding="utf8") as f:
|
||||
with open(token_file, "w", encoding="utf8") as f:
|
||||
json.dump(token_data, f, ensure_ascii=False, indent=4)
|
||||
return {"access_token": access_token, "token_type": "bearer"}
|
||||
|
||||
@@ -88,20 +87,21 @@ credentials_exception = HTTPException(
|
||||
)
|
||||
|
||||
|
||||
@app.post("/webui/auth")
|
||||
@app.post("/auth")
|
||||
def token_to_user(token: str = Depends(oauth2_scheme)):
|
||||
if token not in token_data["token"]:
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
username, expire = payload.get("sub"), payload.get("exp")
|
||||
user = get_user(username)
|
||||
user = get_user(username) # type: ignore
|
||||
if user is None:
|
||||
raise JWTError
|
||||
except JWTError:
|
||||
return Result(code=401)
|
||||
return Result(code=200, data="ok")
|
||||
return Result(code=200, info="登录成功")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="127.0.0.1", port=8080)
|
||||
|
||||
Reference in New Issue
Block a user