Skip to content

Commit 18a073d

Browse files
committed
PreRev 5.6 - 0727 密码加密,自登陆,云端凭据保存
1 parent b6b0df6 commit 18a073d

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

models/user.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ class Login_Form(BaseModel):
99
password: str
1010

1111

12-
class FSC_UserDeposit(BaseModel):
13-
isAccepted: bool
12+
class Cloud_Reigister_Form(BaseModel):
13+
pica_form: Login_Form
14+
jm_form: Login_Form
15+
username: str
16+
password: str
1417

1518

16-
class Pica_Register_Form(FSC_UserDeposit, BaseModel):
19+
class Pica_Register_Form(BaseModel):
1720
# 哔咔注册不再需要真实的邮箱和邮箱验证,用户忘记密码将直接导致账户遗失
1821
email: str
1922
password: str

router/FSCDatabase.py

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import bcrypt
2+
from Crypto.Cipher import AES
3+
from Crypto.Util.Padding import pad, unpad
4+
from binascii import b2a_hex, a2b_hex
15
from fastapi import APIRouter, Response
26

37
from core.ComikNetDB import AsyncMySQL
48
from models.requests import StandardResponse
59
from models.album import Origin
6-
from models.user import UserHistory
10+
from models.user import UserHistory, Login_Form, Cloud_Reigister_Form
711

812
router = APIRouter(prefix="/cloud")
913

@@ -20,6 +24,44 @@ async def shutdown():
2024
await db.close()
2125

2226

27+
@router.post("/login")
28+
async def user_login(body: Login_Form):
29+
res = await db.search(
30+
"UID, Password",
31+
"user",
32+
f'Username="{body.username}"',
33+
"1",
34+
)
35+
36+
if res == ():
37+
return StandardResponse(status_code=401, error_msg="User not found")
38+
39+
if bcrypt.checkpw(body.password.encode(), res[0]["Password"].encode()):
40+
return StandardResponse(status_code=200, data={"UID": res[0]["UID"]})
41+
else:
42+
return StandardResponse(status_code=401, error_msg="Wrong password")
43+
44+
45+
@router.post("/register")
46+
async def user_register(body: Cloud_Reigister_Form):
47+
cloud_hashed = bcrypt.hashpw(body.password.encode(), bcrypt.gensalt()).decode()
48+
aes = AES.new(pad(body.password.encode(), 32), AES.MODE_ECB)
49+
jm_encrypted_pwd = b2a_hex(
50+
aes.encrypt(pad(body.jm_form.password.encode(), 32))
51+
).decode()
52+
pica_encrypted_pwd = b2a_hex(
53+
aes.encrypt(pad(body.pica_form.password.encode(), 32))
54+
).decode()
55+
56+
await db.insert(
57+
"user",
58+
"Username, Password, JM_Username, JM_Password, Pica_Username, Pica_Password",
59+
f'"{body.username}","{cloud_hashed}","{body.jm_form.username}","{jm_encrypted_pwd}","{body.pica_form.username}","{pica_encrypted_pwd}"',
60+
)
61+
62+
return Response(status_code=201)
63+
64+
2365
@router.get("/history/user")
2466
async def get_history(
2567
uid: str, page: int = 1, isReverse: bool = False
@@ -46,18 +88,24 @@ async def delete_history(uid: str):
4688
@router.post("/record")
4789
async def update_history(uid: str, origin: Origin, aid: int, cid: int):
4890
res = await db.search(
49-
"*", "history", f"UID={uid} AND Origin=\"{origin.value}\" AND AID={aid}", "1", "update_time DESC"
91+
"*",
92+
"history",
93+
f'UID={uid} AND Origin="{origin.value}" AND AID={aid}',
94+
"1",
95+
"update_time DESC",
5096
)
5197

5298
if res == ():
5399
await db.insert(
54100
"history",
55101
"UID, Origin, AID, CID, Update_Time",
56-
f"{uid},\"{origin.value}\",{aid},{cid},NOW()",
102+
f'{uid},"{origin.value}",{aid},{cid},NOW()',
57103
)
58104
else:
59105
await db.update(
60-
"history", f"CID={cid} AND Origin=\"{origin.value}\", update_time=NOW()", f"UID={uid} AND AID={aid}"
106+
"history",
107+
f'CID={cid} AND Origin="{origin.value}", update_time=NOW()',
108+
f"UID={uid} AND AID={aid}",
61109
)
62110

63111
return Response(status_code=201)

0 commit comments

Comments
 (0)