작업 내용 저장
This commit is contained in:
@@ -2,10 +2,13 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import typer
|
||||
|
||||
from app.cli.core.output import mask_token_text, render_service_token_result
|
||||
from app.schemas.auth import ServiceTokenIssueCliResponse, ServiceTokenRevokeCliResponse
|
||||
from app.cli.core.utils import render_output
|
||||
from app.schemas.auth import AuthTokenResponse, ServiceTokenIssueCliResponse, ServiceTokenRevokeCliResponse
|
||||
from app.services.auth_service import AuthService
|
||||
|
||||
token_app = typer.Typer(help="Service token lifecycle commands.")
|
||||
@@ -23,6 +26,34 @@ def _with_service(ctx: typer.Context, callback):
|
||||
repository.close()
|
||||
|
||||
|
||||
@token_app.command("list")
|
||||
def list_service_tokens(
|
||||
ctx: typer.Context,
|
||||
user: Optional[str] = typer.Option(None, "--user", help="Filter by username."),
|
||||
show_revoked: bool = typer.Option(False, "--show-revoked", help="Include revoked tokens."),
|
||||
) -> None:
|
||||
def _list(service: AuthService, app_context):
|
||||
tokens = service.list_service_tokens(user, include_revoked=show_revoked)
|
||||
return [
|
||||
AuthTokenResponse(
|
||||
id=t.id,
|
||||
user_id=t.user_id,
|
||||
name=t.name,
|
||||
token_prefix=t.token_prefix,
|
||||
permission=t.permission,
|
||||
token_type=t.token_type,
|
||||
expires_at=t.expires_at,
|
||||
last_used_at=t.last_used_at,
|
||||
revoked_at=t.revoked_at,
|
||||
created_at=t.created_at,
|
||||
)
|
||||
for t in tokens
|
||||
]
|
||||
|
||||
result = _with_service(ctx, _list)
|
||||
render_output(ctx.obj.console, ctx.obj.output_format, result, empty_message="No service tokens found.")
|
||||
|
||||
|
||||
@token_app.command("issue")
|
||||
def issue_service_token(
|
||||
ctx: typer.Context,
|
||||
|
||||
@@ -13,6 +13,7 @@ from rich.panel import Panel
|
||||
from rich.table import Table
|
||||
|
||||
from app.core.errors import FeatureNotReadyError
|
||||
from app.schemas.auth import AuthTokenResponse
|
||||
from app.schemas.portfolio import CommentResponse, PortfolioDetailResponse, PortfolioListItem
|
||||
from app.schemas.user import UserResponse
|
||||
|
||||
@@ -162,6 +163,28 @@ def print_user_detail(console: Console, user: UserResponse) -> None:
|
||||
)
|
||||
|
||||
|
||||
def print_service_token_table(console: Console, tokens: list[AuthTokenResponse]) -> None:
|
||||
table = Table(title="Service Tokens", expand=True)
|
||||
table.add_column("Name", style="cyan", no_wrap=True)
|
||||
table.add_column("Permission", no_wrap=True)
|
||||
table.add_column("User ID", no_wrap=True)
|
||||
table.add_column("Prefix", no_wrap=True)
|
||||
table.add_column("Expires At", no_wrap=True)
|
||||
table.add_column("Revoked At", no_wrap=True)
|
||||
table.add_column("Created At", no_wrap=True)
|
||||
for token in tokens:
|
||||
table.add_row(
|
||||
token.name,
|
||||
token.permission,
|
||||
token.user_id,
|
||||
token.token_prefix,
|
||||
token.expires_at.isoformat(timespec="seconds") if token.expires_at else "never",
|
||||
token.revoked_at.isoformat(timespec="seconds") if token.revoked_at else "-",
|
||||
token.created_at.isoformat(timespec="seconds") if token.created_at else "N/A",
|
||||
)
|
||||
console.print(table)
|
||||
|
||||
|
||||
def print_comment_table(console: Console, comments: list[CommentResponse]) -> None:
|
||||
table = Table(title="Portfolio Comments", expand=True)
|
||||
table.add_column("ID", style="cyan", no_wrap=True)
|
||||
@@ -219,6 +242,9 @@ def render_output(console: Console, output_format: str, payload: Any, *, empty_m
|
||||
if all(isinstance(item, CommentResponse) for item in payload):
|
||||
print_comment_table(console, payload)
|
||||
return
|
||||
if all(isinstance(item, AuthTokenResponse) for item in payload):
|
||||
print_service_token_table(console, payload)
|
||||
return
|
||||
return
|
||||
|
||||
if isinstance(payload, PortfolioDetailResponse):
|
||||
|
||||
@@ -56,6 +56,13 @@ class PortfolioRepository(Protocol):
|
||||
|
||||
def list_auth_tokens(self, user_id: str) -> list[AuthToken]: ...
|
||||
|
||||
def list_service_tokens(
|
||||
self,
|
||||
user_id: str | None = None,
|
||||
*,
|
||||
include_revoked: bool = False,
|
||||
) -> list[AuthToken]: ...
|
||||
|
||||
def revoke_auth_token(self, user_id: str, token_id: str) -> None: ...
|
||||
|
||||
def get_service_token_by_name(self, name: str) -> AuthToken | None: ...
|
||||
|
||||
@@ -564,6 +564,26 @@ class PostgresPortfolioRepository:
|
||||
rows = cur.fetchall()
|
||||
return [self._row_to_auth_token(row) for row in rows]
|
||||
|
||||
def list_service_tokens(
|
||||
self,
|
||||
user_id: str | None = None,
|
||||
*,
|
||||
include_revoked: bool = False,
|
||||
) -> list[AuthToken]:
|
||||
self.ensure_schema()
|
||||
conditions = ["token_type = 'service'"]
|
||||
params: list = []
|
||||
if user_id is not None:
|
||||
conditions.append("user_id = %s")
|
||||
params.append(user_id)
|
||||
if not include_revoked:
|
||||
conditions.append("revoked_at IS NULL")
|
||||
where = "WHERE " + " AND ".join(conditions) + " "
|
||||
with self._connect().cursor() as cur:
|
||||
cur.execute(_SELECT_AUTH_TOKEN + where + "ORDER BY created_at DESC, id ASC", params)
|
||||
rows = cur.fetchall()
|
||||
return [self._row_to_auth_token(row) for row in rows]
|
||||
|
||||
def revoke_auth_token(self, user_id: str, token_id: str) -> None:
|
||||
self.ensure_schema()
|
||||
with self._connect().cursor() as cur:
|
||||
|
||||
@@ -152,6 +152,20 @@ class AuthService:
|
||||
self._repository.revoke_auth_token(existing.user_id, existing.id)
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
def list_service_tokens(
|
||||
self,
|
||||
username: str | None = None,
|
||||
*,
|
||||
include_revoked: bool = False,
|
||||
) -> list[AuthToken]:
|
||||
user_id: str | None = None
|
||||
if username is not None:
|
||||
user = self._repository.get_user_by_username(username)
|
||||
if user is None:
|
||||
raise ValueError(f"사용자를 찾을 수 없습니다: {username}")
|
||||
user_id = user.id
|
||||
return self._repository.list_service_tokens(user_id, include_revoked=include_revoked)
|
||||
|
||||
@staticmethod
|
||||
def _build_expiration(expires_in_days: int | None, default_days: int) -> datetime:
|
||||
days = normalize_expiry_days(expires_in_days, default_days=default_days)
|
||||
|
||||
Reference in New Issue
Block a user