fix: 코드 재검토 반영 — 댓글 더 보기, 긴 비밀번호 500 방지, 토큰 갱신 실패 처리, 연타 막기
This commit is contained in:
1 parent
ae6b06ac1f
commit
be8f827556
7 files changed
+88
-30
No files matched your search
@@ -21,7 +21,12 @@ async def hash_password(password: str) -> str:
|
||||
return (await asyncio.to_thread(bcrypt.hashpw, password.encode(), bcrypt.gensalt())).decode()
|
||||
|
||||
|
||||
PASSWORD_MAX_BYTES = 72 # bcrypt 가 받는 최대 길이
|
||||
|
||||
|
||||
async def verify_password(password: str, hashed: str) -> bool:
|
||||
if len(password.encode()) > PASSWORD_MAX_BYTES:
|
||||
return False
|
||||
return await asyncio.to_thread(bcrypt.checkpw, password.encode(), hashed.encode())
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ from typing import Optional
|
||||
|
||||
from fastapi import Depends
|
||||
|
||||
from common.auth import decode, hash_password, issue_tokens, verify_password
|
||||
from common.auth import PASSWORD_MAX_BYTES, decode, hash_password, issue_tokens, verify_password
|
||||
from common.database.db_session_manager import DB_SESSION_MNG
|
||||
from common.database.model.models import User
|
||||
from common.enums import ErrorType
|
||||
@@ -24,7 +24,7 @@ class AuthService:
|
||||
self.crud = crud
|
||||
|
||||
async def signup(self, login_id: str, password: str, nickname: str) -> tuple[ErrorType, Optional[User]]:
|
||||
if not LOGIN_ID.match(login_id) or len(password) < PASSWORD_MIN or not NICKNAME.match(nickname):
|
||||
if not LOGIN_ID.match(login_id) or len(password) < PASSWORD_MIN or len(password.encode()) > PASSWORD_MAX_BYTES or not NICKNAME.match(nickname):
|
||||
return ErrorType.INVALID_INPUT, None
|
||||
err, exists = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.by_login_id(s, login_id))
|
||||
if err is not ErrorType.SUCCESS:
|
||||
@@ -33,7 +33,11 @@ class AuthService:
|
||||
return ErrorType.AUTH_DUPLICATE_ID, None
|
||||
hashed = await hash_password(password)
|
||||
err, user = await DB_SESSION_MNG.execute_lambda_write(lambda s: self.crud.create(s, login_id, hashed, nickname))
|
||||
return (err, user) if err is ErrorType.SUCCESS else (ErrorType.AUTH_DUPLICATE_ID, None)
|
||||
if err is ErrorType.SUCCESS:
|
||||
return err, user
|
||||
# 같은 아이디가 동시에 가입된 경우만 중복으로, 나머지는 DB 오류 그대로
|
||||
_, exists = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.by_login_id(s, login_id))
|
||||
return (ErrorType.AUTH_DUPLICATE_ID if exists is not None else err), None
|
||||
|
||||
async def login(self, login_id: str, password: str) -> tuple[ErrorType, Optional[User]]:
|
||||
err, user = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.by_login_id(s, login_id))
|
||||
|
||||
@@ -31,6 +31,17 @@ async def test_signup_login_refresh_me():
|
||||
assert (await c.post("/v1/auth/refresh", json={"refresh_token": ok["access_token"]})).json()["result"]["desc"] == "AUTH_TOKEN_INVALID"
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_password_over_72_bytes_is_rejected_not_500():
|
||||
long_pw = "비밀번호" * 7 # 84바이트
|
||||
async with client() as c:
|
||||
r = await c.post("/v1/auth/signup", json={"login_id": "mina_long", "password": long_pw, "nickname": "긴비번"})
|
||||
assert r.status_code == 200 and r.json()["result"]["desc"] == "INVALID_INPUT"
|
||||
await _signup(c, login_id="mina_02")
|
||||
r = await c.post("/v1/auth/login", json={"login_id": "mina_02", "password": long_pw})
|
||||
assert r.status_code == 200 and r.json()["result"]["desc"] == "AUTH_FAILED"
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_signup_rejects_bad_input():
|
||||
async with client() as c:
|
||||
|
||||
Reference in new issue
Block a user