diff --git a/internal/domain/models.go b/internal/domain/models.go index 59a458f..8073704 100644 --- a/internal/domain/models.go +++ b/internal/domain/models.go @@ -104,11 +104,10 @@ type UserToken struct { // VerifyRequest represents a token verification request type VerifyRequest struct { - AppID string `json:"app_id" validate:"required"` - Type TokenType `json:"type" validate:"required,oneof=static user"` - UserID string `json:"user_id,omitempty"` // Required for user tokens - Token string `json:"token" validate:"required"` - Permissions []string `json:"permissions,omitempty"` + AppID string `json:"app_id" validate:"required"` + UserID string `json:"user_id,omitempty"` // Required for user tokens + Token string `json:"token" validate:"required"` + Permissions []string `json:"permissions,omitempty"` } // VerifyResponse represents a token verification response diff --git a/internal/handlers/auth.go b/internal/handlers/auth.go index 8389408..4917d52 100644 --- a/internal/handlers/auth.go +++ b/internal/handlers/auth.go @@ -98,7 +98,7 @@ func (h *AuthHandler) Verify(c *gin.Context) { return } - h.logger.Debug("Verifying token", zap.String("app_id", req.AppID), zap.String("type", string(req.Type))) + h.logger.Debug("Verifying token", zap.String("app_id", req.AppID)) response, err := h.tokenService.VerifyToken(c.Request.Context(), &req) if err != nil { diff --git a/internal/services/token_service.go b/internal/services/token_service.go index c26b3b2..44c7d5b 100644 --- a/internal/services/token_service.go +++ b/internal/services/token_service.go @@ -265,10 +265,47 @@ func (s *tokenService) GenerateUserToken(ctx context.Context, appID, userID stri return finalToken, nil } +// detectTokenType detects the token type based on its prefix +func (s *tokenService) detectTokenType(token string, app *domain.Application) domain.TokenType { + // Check for user token pattern first (UT- suffix) + if app.TokenPrefix != "" { + userPrefix := app.TokenPrefix + "UT-" + if strings.HasPrefix(token, userPrefix) { + return domain.TokenTypeUser + } + + staticPrefix := app.TokenPrefix + "T-" + if strings.HasPrefix(token, staticPrefix) { + return domain.TokenTypeStatic + } + } + + // Check for custom prefix pattern in case app prefix is not set + // Look for pattern: 2-4 uppercase letters + "UT-" or "T-" + if len(token) >= 6 { + dashIndex := strings.Index(token, "-") + if dashIndex >= 3 && dashIndex <= 6 { // 2-4 chars + "T" or "UT" + prefixPart := token[:dashIndex+1] + if strings.HasSuffix(prefixPart, "UT-") { + return domain.TokenTypeUser + } + if strings.HasSuffix(prefixPart, "T-") { + return domain.TokenTypeStatic + } + } + } + + // Check for default kms_ prefix + if strings.HasPrefix(token, "kms_") { + return domain.TokenTypeStatic // Default tokens are static + } + + // Default to static if pattern is unclear + return domain.TokenTypeStatic +} + // VerifyToken verifies a token and returns verification response func (s *tokenService) VerifyToken(ctx context.Context, req *domain.VerifyRequest) (*domain.VerifyResponse, error) { - s.logger.Debug("Verifying token", zap.String("app_id", req.AppID), zap.String("type", string(req.Type))) - // Validate request if req.Token == "" { return &domain.VerifyResponse{ @@ -289,7 +326,15 @@ func (s *tokenService) VerifyToken(ctx context.Context, req *domain.VerifyReques }, nil } - switch req.Type { + // Always auto-detect token type from prefix + tokenType := s.detectTokenType(req.Token, app) + s.logger.Debug("Auto-detected token type", + zap.String("app_id", req.AppID), + zap.String("detected_type", string(tokenType))) + + s.logger.Debug("Verifying token", zap.String("app_id", req.AppID), zap.String("type", string(tokenType))) + + switch tokenType { case domain.TokenTypeStatic: return s.verifyStaticToken(ctx, req, app) case domain.TokenTypeUser: diff --git a/kms-frontend/src/components/TokenTester.tsx b/kms-frontend/src/components/TokenTester.tsx index d59ca28..52e51b5 100644 --- a/kms-frontend/src/components/TokenTester.tsx +++ b/kms-frontend/src/components/TokenTester.tsx @@ -182,10 +182,9 @@ const TokenTester: React.FC = () => { console.log('Testing callback with token verification:', values); - // Verify the token received in the callback + // Verify the token received in the callback (type will be auto-detected) const verifyResponse = await apiService.verifyToken({ app_id: values.app_id, - type: 'user', token: values.token, permissions: values.permissions || [], }); diff --git a/kms-frontend/src/components/TokenTesterCallback.tsx b/kms-frontend/src/components/TokenTesterCallback.tsx index 2ebfd40..1191aa6 100644 --- a/kms-frontend/src/components/TokenTesterCallback.tsx +++ b/kms-frontend/src/components/TokenTesterCallback.tsx @@ -111,7 +111,6 @@ const TokenTesterCallback: React.FC = () => { const verifyRequest = { app_id: appId, - type: 'user', token: token, permissions: [], // We'll verify without specific permissions }; diff --git a/kms-frontend/src/components/Tokens.tsx b/kms-frontend/src/components/Tokens.tsx index 5252316..470cb54 100644 --- a/kms-frontend/src/components/Tokens.tsx +++ b/kms-frontend/src/components/Tokens.tsx @@ -184,7 +184,7 @@ const Tokens: React.FC = () => { const verifyRequest: VerifyRequest = { app_id: values.app_id, - type: values.token_type || 'static', + // Remove explicit type - it will be auto-detected from token prefix token: values.token, permissions: values.permissions || [], }; @@ -505,41 +505,30 @@ const Tokens: React.FC = () => { width={800} > +
- - - - - - - - - - - - + + + { export interface VerifyRequest { app_id: string; - type: string; user_id?: string; token: string; permissions?: string[]; diff --git a/test/integration_test.go b/test/integration_test.go index d0788d7..4b26aba 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -96,7 +96,7 @@ func (suite *IntegrationTestSuite) setupServer() { // Initialize services appService := services.NewApplicationService(appRepo, logger) - tokenService := services.NewTokenService(tokenRepo, appRepo, permRepo, grantRepo, suite.cfg.GetString("INTERNAL_HMAC_KEY"), logger) + tokenService := services.NewTokenService(tokenRepo, appRepo, permRepo, grantRepo, suite.cfg.GetString("INTERNAL_HMAC_KEY"), suite.cfg, logger) authService := services.NewAuthenticationService(suite.cfg, logger) // Initialize handlers