|
1 | 1 | """구독 API 엔드포인트.""" |
2 | 2 |
|
3 | | -from datetime import datetime, timezone |
| 3 | +from datetime import datetime, timedelta, timezone |
4 | 4 |
|
5 | | -from fastapi import APIRouter |
| 5 | +from fastapi import APIRouter, HTTPException, status |
| 6 | +from pydantic import BaseModel |
| 7 | + |
| 8 | +from src.common.responses import RESPONSES_CRUD_WITH_AUTH, ERROR_403_FORBIDDEN |
| 9 | +from src.common.responses import create_responses |
| 10 | +from src.security import create_mcp_token |
6 | 11 |
|
7 | | -from src.common.responses import RESPONSES_CRUD_WITH_AUTH |
8 | 12 | from src.users.dependencies import ActiveUser, SessionDep |
9 | 13 |
|
10 | | -from .models import Subscription, SubscriptionType |
| 14 | +from .models import Subscription, SubscriptionPlan, SubscriptionType |
11 | 15 | from .service import get_active_subscription |
12 | 16 |
|
| 17 | + |
| 18 | +class McpTokenResponse(BaseModel): |
| 19 | + token: str |
| 20 | + |
| 21 | + |
13 | 22 | router = APIRouter() |
14 | 23 |
|
15 | 24 |
|
@@ -51,6 +60,70 @@ async def get_my_subscriptions( |
51 | 60 | return subs |
52 | 61 |
|
53 | 62 |
|
| 63 | +@router.get( |
| 64 | + "/me/mcp-token", |
| 65 | + response_model=McpTokenResponse, |
| 66 | + responses=create_responses( |
| 67 | + RESPONSES_CRUD_WITH_AUTH, |
| 68 | + {403: ERROR_403_FORBIDDEN}, |
| 69 | + ), |
| 70 | + summary="MCP 서버 토큰 발급", |
| 71 | +) |
| 72 | +async def get_mcp_token( |
| 73 | + session: SessionDep, |
| 74 | + current_user: ActiveUser, |
| 75 | +): |
| 76 | + """ |
| 77 | + 활성 MCP 구독이 있는 유저에게 MCP 서버 접속용 JWT 토큰을 발급합니다. |
| 78 | +
|
| 79 | + - 구독이 없거나 만료된 경우 403을 반환합니다. |
| 80 | + - 만료 여부는 호출 시점에 실시간으로 확인합니다. |
| 81 | + """ |
| 82 | + from sqlmodel import select |
| 83 | + |
| 84 | + result = await session.execute( |
| 85 | + select(Subscription).where( |
| 86 | + Subscription.user_id == current_user.id, |
| 87 | + Subscription.sub_type == SubscriptionType.MCP, |
| 88 | + ) |
| 89 | + ) |
| 90 | + sub = result.scalar_one_or_none() |
| 91 | + |
| 92 | + now = datetime.now(timezone.utc) |
| 93 | + |
| 94 | + if sub is None: |
| 95 | + # 구독 이력 없음 → 무료 체험 발급 (최초 1회) |
| 96 | + sub = Subscription( |
| 97 | + user_id=current_user.id, |
| 98 | + sub_type=SubscriptionType.MCP, |
| 99 | + is_active=True, |
| 100 | + plan=SubscriptionPlan.FREE_TRIAL, |
| 101 | + started_at=now, |
| 102 | + expires_at=now + timedelta(days=30), |
| 103 | + token=create_mcp_token(subject=str(current_user.id)), |
| 104 | + ) |
| 105 | + session.add(sub) |
| 106 | + await session.commit() |
| 107 | + await session.refresh(sub) |
| 108 | + return McpTokenResponse(token=sub.token) |
| 109 | + |
| 110 | + # 구독은 있지만 만료됨 → 결제 필요 |
| 111 | + if not sub.is_active or (sub.expires_at is not None and sub.expires_at < now): |
| 112 | + raise HTTPException( |
| 113 | + status_code=status.HTTP_403_FORBIDDEN, |
| 114 | + detail="MCP 구독이 만료되었습니다. 결제 후 이용해주세요.", |
| 115 | + ) |
| 116 | + |
| 117 | + # 활성 구독이지만 토큰 없음 (결제 등 다른 경로로 구독 생성된 경우) |
| 118 | + if not sub.token: |
| 119 | + sub.token = create_mcp_token(subject=str(current_user.id)) |
| 120 | + session.add(sub) |
| 121 | + await session.commit() |
| 122 | + await session.refresh(sub) |
| 123 | + |
| 124 | + return McpTokenResponse(token=sub.token) |
| 125 | + |
| 126 | + |
54 | 127 | @router.get( |
55 | 128 | "/me/{type}", |
56 | 129 | response_model=Subscription | None, |
|
0 commit comments