Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
OasisAkari committed Dec 28, 2024
2 parents f3f1edc + 2de667b commit 3aaac83
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
20 changes: 10 additions & 10 deletions bots/api/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import uuid
from datetime import datetime, timedelta, UTC

import hashlib
import jwt
import orjson as json
import psutil
import uvicorn
from cpuinfo import get_cpu_info
from argon2 import PasswordHasher
from fastapi import FastAPI, Request, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
Expand Down Expand Up @@ -55,6 +52,7 @@ def verify_access_token(request: Request):


app = FastAPI(dependencies=[Depends(verify_access_token)])
ph = PasswordHasher()


app.add_middleware(
Expand Down Expand Up @@ -101,8 +99,9 @@ async def auth(request: Request):
with open(PASSWORD_PATH, "r") as file:
stored_password = file.read().strip()

password = hashlib.sha256(password.encode()).hexdigest()
if stored_password != password:
try:
ph.verify(stored_password, password) # 验证输入的密码是否与存储的哈希匹配
except Exception:
raise HTTPException(status_code=401, detail="invalid password")

payload = {
Expand Down Expand Up @@ -145,20 +144,21 @@ async def change_password(request: Request):
if not os.path.exists(PASSWORD_PATH):
if new_password == "":
raise HTTPException(status_code=400, detail="new password required")
new_password_hashed = hashlib.sha256(new_password.encode()).hexdigest()
new_password_hashed = ph.hash(new_password)
with open(PASSWORD_PATH, "w") as file:
file.write(new_password_hashed)
return {"message": "success"}

with open(PASSWORD_PATH, "r") as file:
stored_password = file.read().strip()

hashed_password = hashlib.sha256(password.encode()).hexdigest()
if stored_password != hashed_password:
try:
ph.verify(stored_password, password)
except Exception:
raise HTTPException(status_code=401, detail="invalid password")

# 设置新密码
new_password_hashed = hashlib.sha256(new_password.encode()).hexdigest()
new_password_hashed = ph.hash(new_password)
with open(PASSWORD_PATH, "w") as file:
file.write(new_password_hashed)

Expand Down
76 changes: 66 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ qq-botpy = "^1.2.1"
botpy = {git = "https://github.com/Teahouse-Studios/botpy.git"}
tomlkit = "^0.13.2"
cattrs = "^24.1.2"
argon2-cffi = "^23.1.0"


[tool.poetry.group.dev.dependencies]
Expand Down
8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ annotated-types==0.7.0 ; python_full_version >= "3.12.0" and python_version < "4
anyio==4.6.2.post1 ; python_full_version >= "3.12.0" and python_version < "4.0"
appdirs==1.4.4 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
apscheduler==3.10.4 ; python_version >= "3.12" and python_version < "4.0"
argon2-cffi-bindings==21.2.0 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
argon2-cffi==23.1.0 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
asyncio-dgram==2.2.0 ; python_full_version >= "3.12.0" and python_version < "4"
attrs==24.2.0 ; python_version >= "3.12" and python_version < "4.0"
backoff==2.2.1 ; python_full_version >= "3.12.0" and python_version < "4.0"
Expand All @@ -19,7 +21,7 @@ botpy @ git+https://github.com/Teahouse-Studios/botpy.git@56c66983f36918fa433a71
caio==0.9.17 ; python_full_version >= "3.12.0" and python_version < "4"
cattrs==24.1.2 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
certifi==2024.8.30 ; python_full_version >= "3.12.0" and python_version < "4.0"
cffi==1.17.1 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0" and platform_python_implementation != "PyPy"
cffi==1.17.1 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
cfgv==3.4.0 ; python_version >= "3.12" and python_version < "4.0"
charset-normalizer==3.4.0 ; python_full_version >= "3.12.0" and python_version < "4.0"
click==8.1.7 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
Expand Down Expand Up @@ -100,7 +102,7 @@ propcache==0.2.0 ; python_version >= "3.12" and python_version < "4.0"
psutil==6.1.0 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
py-cord==2.6.1 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
py-cpuinfo==9.0.0 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
pycparser==2.22 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0" and platform_python_implementation != "PyPy"
pycparser==2.22 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
pycryptodome==3.21.0 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
pycryptodomex==3.21.0 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
pydantic-core==2.23.4 ; python_full_version >= "3.12.0" and python_version < "4.0"
Expand All @@ -116,7 +118,7 @@ pytz==2024.2 ; python_version >= "3.12" and python_version < "4.0"
pywin32==306 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0" and sys_platform == "win32"
pyyaml==6.0.2 ; python_version >= "3.12" and python_version < "4.0"
qq-botpy==1.2.1 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
quart==0.19.8 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
quart==0.20.0 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
referencing==0.35.1 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
regex==2024.9.11 ; python_full_version >= "3.12.0" and python_full_version < "4.0.0"
requests-toolbelt==1.0.0 ; python_full_version >= "3.12.0" and python_version < "4.0"
Expand Down

0 comments on commit 3aaac83

Please sign in to comment.