From 4b68641043cca47d6063e4487afae7bec4ff49a9 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 15 Feb 2026 01:15:50 +0800 Subject: [PATCH] move the user agent constants of special token into the core package --- pkg/api/tokens.go | 8 ++++---- pkg/core/token_claims.go | 9 +++++++++ pkg/services/tokens.go | 13 ++----------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/api/tokens.go b/pkg/api/tokens.go index 596a527a..4649ba4b 100644 --- a/pkg/api/tokens.go +++ b/pkg/api/tokens.go @@ -69,10 +69,10 @@ func (a *TokensApi) TokenListHandler(c *core.WebContext) (any, *errs.Error) { tokenResp.IsCurrent = true } - if token.TokenType == core.USER_TOKEN_TYPE_API && token.UserAgent != services.TokenUserAgentCreatedViaCli { - tokenResp.UserAgent = services.TokenUserAgentForAPI - } else if token.TokenType == core.USER_TOKEN_TYPE_MCP && token.UserAgent != services.TokenUserAgentCreatedViaCli { - tokenResp.UserAgent = services.TokenUserAgentForMCP + if token.TokenType == core.USER_TOKEN_TYPE_API && token.UserAgent != core.TokenUserAgentCreatedViaCli { + tokenResp.UserAgent = core.TokenUserAgentForAPI + } else if token.TokenType == core.USER_TOKEN_TYPE_MCP && token.UserAgent != core.TokenUserAgentCreatedViaCli { + tokenResp.UserAgent = core.TokenUserAgentForMCP } tokenResps[i] = tokenResp diff --git a/pkg/core/token_claims.go b/pkg/core/token_claims.go index cd98c5e0..ca5c34c5 100644 --- a/pkg/core/token_claims.go +++ b/pkg/core/token_claims.go @@ -6,6 +6,15 @@ import ( "github.com/golang-jwt/jwt/v5" ) +// TokenUserAgentCreatedViaCli is the user agent of token created via cli +const TokenUserAgentCreatedViaCli = ApplicationName + " Cli" + +// TokenUserAgentForAPI is the user agent for API token +const TokenUserAgentForAPI = ApplicationName + " API" + +// TokenUserAgentForMCP is the user agent for MCP token +const TokenUserAgentForMCP = ApplicationName + " MCP" + // TokenType represents token type type TokenType byte diff --git a/pkg/services/tokens.go b/pkg/services/tokens.go index f77a44c1..1a02728c 100644 --- a/pkg/services/tokens.go +++ b/pkg/services/tokens.go @@ -20,15 +20,6 @@ import ( "github.com/mayswind/ezbookkeeping/pkg/utils" ) -// TokenUserAgentCreatedViaCli is the user agent of token created via cli -const TokenUserAgentCreatedViaCli = core.ApplicationName + " Cli" - -// TokenUserAgentForAPI is the user agent for API token -const TokenUserAgentForAPI = core.ApplicationName + " API" - -// TokenUserAgentForMCP is the user agent for MCP token -const TokenUserAgentForMCP = core.ApplicationName + " MCP" - const tokenMaxExpiredAtUnixTime = int64(253402300799) // 9999-12-31 23:59:59 UTC // TokenService represents user token service @@ -140,7 +131,7 @@ func (s *TokenService) CreateAPITokenViaCli(c *core.CliContext, user *models.Use tokenExpiredTimeDuration = time.Unix(tokenMaxExpiredAtUnixTime, 0).Sub(time.Now()) } - token, _, tokenRecord, err := s.createToken(c, user, core.USER_TOKEN_TYPE_API, TokenUserAgentCreatedViaCli, "", tokenExpiredTimeDuration) + token, _, tokenRecord, err := s.createToken(c, user, core.USER_TOKEN_TYPE_API, core.TokenUserAgentCreatedViaCli, "", tokenExpiredTimeDuration) return token, tokenRecord, err } @@ -168,7 +159,7 @@ func (s *TokenService) CreateMCPTokenViaCli(c *core.CliContext, user *models.Use tokenExpiredTimeDuration = time.Unix(tokenMaxExpiredAtUnixTime, 0).Sub(time.Now()) } - token, _, tokenRecord, err := s.createToken(c, user, core.USER_TOKEN_TYPE_MCP, TokenUserAgentCreatedViaCli, "", tokenExpiredTimeDuration) + token, _, tokenRecord, err := s.createToken(c, user, core.USER_TOKEN_TYPE_MCP, core.TokenUserAgentCreatedViaCli, "", tokenExpiredTimeDuration) return token, tokenRecord, err }