feat: 토스판 토스 로그인(mTLS 토큰 교환·연결 끊기 콜백)과 좋아요·댓글 다시 열기

This commit is contained in:
minaChoi committed 2026-10-14 15:36:36 +09:00
1 parent e180d9651c
commit ea5376820c
23 files changed
+530 -56

No files matched your search

+35 -2
View File
@@ -1,10 +1,17 @@
from fastapi import APIRouter, Depends
import base64
import binascii
import secrets
from typing import Optional
from fastapi import APIRouter, Depends, Header, HTTPException
from common.auth import current_user
from common.database.model.models import User
from common.enums import AuthProvider, ErrorType
from common.logger import LOG
from common.models.errors import error_info
from router.v1.auth.protocol import Me, Req_Login, Req_Refresh, Req_Signup, Req_UpdateMe, Res_Auth, Res_Logout, Res_Me
from config.settings import settings
from router.v1.auth.protocol import Me, Req_Login, Req_Refresh, Req_Signup, Req_TossLogin, Req_TossUnlink, Req_UpdateMe, Res_Auth, Res_Logout, Res_Me, Res_TossUnlink
from services.auth_service import AuthService
router = APIRouter(prefix="/v1/auth", tags=["auth"])
@@ -33,6 +40,32 @@ async def login(req: Req_Login, service: AuthService = Depends()) -> Res_Auth:
return _auth(err, user, service)
@router.post("/toss", response_model=Res_Auth, response_model_exclude_none=True, summary="토스 로그인 (앱인토스 TossAuth.login 의 authorizationCode·referrer)")
async def toss(req: Req_TossLogin, service: AuthService = Depends()) -> Res_Auth:
err, user = await service.toss_login(req.authorization_code, req.referrer)
return _auth(err, user, service)
def _check_toss_basic(authorization: Optional[str]) -> None:
"""콘솔에 넣은 "아이디:비밀번호" 와 같은지 본다. 값이 비어 있으면 모두 거절한다."""
given = ""
if authorization and authorization.startswith("Basic "):
try:
given = base64.b64decode(authorization.removeprefix("Basic "), validate=True).decode()
except (binascii.Error, UnicodeDecodeError):
given = ""
if not settings.toss_unlink_auth or not secrets.compare_digest(given.encode(), settings.toss_unlink_auth.encode()):
raise HTTPException(status_code=401, detail=ErrorType.AUTH_FAILED.name)
@router.post("/toss/unlink", response_model=Res_TossUnlink, response_model_exclude_none=True, summary="토스 연결 끊기 콜백 (토스 서버가 Basic Auth 로 부름)")
async def toss_unlink(req: Req_TossUnlink, authorization: Optional[str] = Header(None), service: AuthService = Depends()) -> Res_TossUnlink:
_check_toss_basic(authorization)
LOG.info(f"[auth] toss unlink: {req.referrer}")
err = await service.toss_unlink(str(req.userKey))
return Res_TossUnlink() if err is ErrorType.SUCCESS else Res_TossUnlink(result=error_info(err))
@router.post("/refresh", response_model=Res_Auth, response_model_exclude_none=True, summary="토큰 재발급")
async def refresh(req: Req_Refresh, service: AuthService = Depends()) -> Res_Auth:
err, user = await service.refresh(req.refresh_token)