Skip to content

Commit 973233b

Browse files
committed
fix
1 parent 97cf649 commit 973233b

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dependencies = [
77
"fastapi>=0.115.0",
88
"uvicorn[standard]>=0.30.0",
99
"sqlalchemy>=2.0.0",
10-
"passlib[bcrypt]>=1.7.4",
10+
"bcrypt>=4.0.0",
1111
"pydantic>=2.0.0",
1212
]
1313

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fastapi>=0.115.0
22
uvicorn[standard]>=0.30.0
33
sqlalchemy>=2.0.0
4-
passlib[bcrypt]>=1.7.4
4+
bcrypt>=4.0.0
55
pydantic>=2.0.0
66
pydantic[email]>=2.0.0

routes/comments.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import bcrypt
2+
13
from fastapi import APIRouter, Depends, HTTPException, status
24
from sqlalchemy.orm import Session
3-
from passlib.context import CryptContext
45

56
from database import get_db
67
from models import Comment
78
from schemas import CommentCreate, CommentResponse, CommentDelete
89

910
router = APIRouter(prefix="/posts", tags=["comments"])
1011

11-
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
12+
13+
def _hash_password(password: str) -> str:
14+
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
15+
16+
17+
def _verify_password(password: str, hashed: str) -> bool:
18+
return bcrypt.checkpw(password.encode(), hashed.encode())
1219

1320

1421
@router.get("/{slug:path}/comments", response_model=list[CommentResponse])
@@ -28,7 +35,7 @@ def create_comment(slug: str, body: CommentCreate, db: Session = Depends(get_db)
2835
comment = Comment(
2936
post_slug=slug,
3037
nickname=body.nickname,
31-
password_hash=pwd_context.hash(body.password),
38+
password_hash=_hash_password(body.password),
3239
content=body.content,
3340
)
3441
db.add(comment)
@@ -53,7 +60,7 @@ def delete_comment(
5360
if not comment:
5461
raise HTTPException(status_code=404, detail="댓글을 찾을 수 없습니다.")
5562

56-
if not pwd_context.verify(body.password, comment.password_hash):
63+
if not _verify_password(body.password, comment.password_hash):
5764
raise HTTPException(status_code=403, detail="비밀번호가 올바르지 않습니다.")
5865

5966
db.delete(comment)

0 commit comments

Comments
 (0)