#!/usr/bin/env python3
"""
测试Token生成和验证
"""

import base64
import json

# 从测试中获取的Token
token = "eyJhbGciOiJIUzM4NCJ9.eyJyb2xlcyI6W10sInVzZXJJZCI6MTA2NCwidXNlcm5hbWUiOiJhZG1pbiIsInN1YiI6ImFkbWluIiwiaWF0IjoxNzc1MDkxNzg4LCJleHAiOjE3NzUxNzgxODh9"

# 解析Token header
def decode_jwt_header(token):
    parts = token.split('.')
    if len(parts) < 1:
        return None
    
    header = parts[0]
    # 添加padding
    padding = len(header) % 4
    if padding:
        header += '=' * (4 - padding)
    
    decoded = base64.b64decode(header)
    return json.loads(decoded)

header = decode_jwt_header(token)
print("Token Header:")
print(json.dumps(header, indent=2))

print("\n算法: " + header.get('alg', 'Unknown'))

# Gateway secret
gateway_secret = "U2FsdGVkX1+vZ5Y9QmKxL8nN3rP7tW2jH4fG6dA8sB1cE5yN0zX3qV7wM4"
print(f"\nGateway secret长度: {len(gateway_secret)} bytes")
print(f"Gateway secret支持算法: HS384 (因为长度 >= 48 bytes)")

print("\n问题分析:")
print("1. manage-app使用JwtTokenProvider生成Token")
print("2. JwtTokenProvider使用Keys.hmacShaKeyFor()自动选择算法")
print("3. Gateway secret长度58 bytes，自动选择HS384算法")
print("4. Gateway使用JwtUtil验证Token")
print("5. JwtUtil使用new SecretKeySpec()创建密钥")
print("6. 需要确保JwtUtil也使用相同的算法")
