feat(backend): 로그아웃 + stateful 토큰 검증 (단일 세션 즉시 폐기)

- POST /v1/auth/logout: 저장된 access/refresh 토큰 행 삭제 → 즉시 로그아웃
- /me, refresh 에서 제시된 토큰을 저장된 토큰과 대조(불일치 시 TOKEN_REVOKED=1203)
  → 로그아웃/타기기 재로그인으로 교체된 토큰을 만료 전이라도 차단
- crud get_token 추가, ErrorType.TOKEN_REVOKED(1203) 추가, Res_Logout 프로토콜
- 로그아웃/단일세션 무효화 e2e 테스트 추가 (test_auth.py)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
민헌andClaude Opus 4.8 committed 2026-06-18 11:26:08 +09:00
1 parent 23cb8291bc
commit 698d4e72d4
6 files changed
+148 -13

No files matched your search

+52
View File
@@ -270,3 +270,55 @@ async def test_refresh_inactive_after_token(client, account_seed, db_engine):
assert body["result"]["success"] is False
assert body["result"]["code"] == 1202
assert body.get("access_token", "") == ""
# ---- 로그아웃 / stateful 토큰 검증 -------------------------------------------
async def test_logout_revokes_tokens(client, account_seed, db_engine):
body = (await _login(client)).json()
su_id, access, refresh = body["su_id"], body["access_token"], body["refresh_token"]
# 로그아웃 성공
r = await client.post("/v1/auth/logout", headers={"Authorization": f"Bearer {access}"})
assert r.status_code == 200
assert r.json()["result"]["success"] is True
# 저장 토큰이 모두 삭제됨
async with db_engine.begin() as conn:
count = (
await conn.execute(
text("SELECT count(*) FROM supplier.supplier_user_tokens WHERE su_id = :sid AND deleted = false"),
{"sid": uuid.UUID(su_id)},
)
).scalar()
assert count == 0
# 로그아웃 후 같은 access 로 /me → TOKEN_REVOKED(1203)
r2 = await client.get("/v1/auth/me", headers={"Authorization": f"Bearer {access}"})
assert r2.status_code == 200
assert r2.json()["result"]["code"] == 1203
# 로그아웃 후 같은 refresh 로 재발급 → TOKEN_REVOKED(1203)
r3 = await client.post("/v1/auth/refresh_token", headers={"Authorization": f"Bearer {refresh}"})
assert r3.json()["result"]["code"] == 1203
async def test_logout_without_token(client):
r = await client.post("/v1/auth/logout")
assert r.status_code in (401, 403)
async def test_relogin_invalidates_previous_access(client, account_seed):
# 단일 세션: 재로그인하면 이전 세션의 access 가 무효화된다(저장 토큰이 교체됨).
import asyncio
first = (await _login(client)).json()
await asyncio.sleep(1.1) # exp(초 단위)가 달라져 토큰이 실제로 바뀌도록
second = (await _login(client)).json()
assert first["access_token"] != second["access_token"]
# 이전 access → 무효(TOKEN_REVOKED)
r_old = await client.get("/v1/auth/me", headers={"Authorization": f"Bearer {first['access_token']}"})
assert r_old.json()["result"]["code"] == 1203
# 새 access → 정상
r_new = await client.get("/v1/auth/me", headers={"Authorization": f"Bearer {second['access_token']}"})
assert r_new.json()["result"]["success"] is True