|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+package util
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "fmt"
|
|
|
5
|
+ "time"
|
|
|
6
|
+
|
|
|
7
|
+ "github.com/golang-jwt/jwt/v5"
|
|
|
8
|
+)
|
|
|
9
|
+
|
|
|
10
|
+// Claims 结构定义
|
|
|
11
|
+type Claims struct {
|
|
|
12
|
+ UserID string `json:"user_id"`
|
|
|
13
|
+ Username string `json:"username"`
|
|
|
14
|
+ TenantID string `json:"tenant_id"`
|
|
|
15
|
+ ProjectID string `json:"project_id"`
|
|
|
16
|
+ Extra map[string]interface{} `json:"extra,omitempty"`
|
|
|
17
|
+ jwt.RegisteredClaims
|
|
|
18
|
+}
|
|
|
19
|
+
|
|
|
20
|
+// CreateToken 创建Token
|
|
|
21
|
+// claims: Claims对象
|
|
|
22
|
+// expiresHours: 过期小时数(0表示默认7天)
|
|
|
23
|
+// secretKey: 密钥
|
|
|
24
|
+func CreateToken(claims *Claims, expiresHours int, secretKey string) (string, error) {
|
|
|
25
|
+ return createTokenInternal(claims, 0, expiresHours, secretKey)
|
|
|
26
|
+}
|
|
|
27
|
+
|
|
|
28
|
+// CreateTokenDays 创建Token(按天)
|
|
|
29
|
+// claims: Claims对象
|
|
|
30
|
+// expiresDays: 过期天数(优先使用)
|
|
|
31
|
+// secretKey: 密钥
|
|
|
32
|
+func CreateTokenDays(claims *Claims, expiresDays int, secretKey string) (string, error) {
|
|
|
33
|
+ return createTokenInternal(claims, expiresDays, 0, secretKey)
|
|
|
34
|
+}
|
|
|
35
|
+
|
|
|
36
|
+// 内部实现
|
|
|
37
|
+func createTokenInternal(claims *Claims, expiresDays, expiresHours int, secretKey string) (string, error) {
|
|
|
38
|
+ if secretKey == "" {
|
|
|
39
|
+ return "", fmt.Errorf("secret key is required")
|
|
|
40
|
+ }
|
|
|
41
|
+ if claims == nil {
|
|
|
42
|
+ return "", fmt.Errorf("claims cannot be nil")
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
|
45
|
+ // 计算过期时间
|
|
|
46
|
+ var expiresDuration time.Duration
|
|
|
47
|
+ if expiresDays > 0 {
|
|
|
48
|
+ expiresDuration = time.Duration(expiresDays) * 24 * time.Hour
|
|
|
49
|
+ } else if expiresHours > 0 {
|
|
|
50
|
+ expiresDuration = time.Duration(expiresHours) * time.Hour
|
|
|
51
|
+ } else {
|
|
|
52
|
+ // 默认7天
|
|
|
53
|
+ expiresDuration = 7 * 24 * time.Hour
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ // 设置时间字段(内部处理)
|
|
|
57
|
+ now := time.Now()
|
|
|
58
|
+ claims.RegisteredClaims = jwt.RegisteredClaims{
|
|
|
59
|
+ ExpiresAt: jwt.NewNumericDate(now.Add(expiresDuration)),
|
|
|
60
|
+ IssuedAt: jwt.NewNumericDate(now),
|
|
|
61
|
+ NotBefore: jwt.NewNumericDate(now),
|
|
|
62
|
+ Issuer: "jwt-app",
|
|
|
63
|
+ Subject: claims.UserID,
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ // 生成Token
|
|
|
67
|
+ token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
68
|
+ return token.SignedString([]byte(secretKey))
|
|
|
69
|
+}
|
|
|
70
|
+
|
|
|
71
|
+// ParseToken 解析Token
|
|
|
72
|
+// tokenString: Token字符串
|
|
|
73
|
+// secretKey: 密钥
|
|
|
74
|
+func ParseToken(tokenString, secretKey string) (*Claims, error) {
|
|
|
75
|
+ if secretKey == "" {
|
|
|
76
|
+ return nil, fmt.Errorf("secret key is required")
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ claims := &Claims{}
|
|
|
80
|
+ token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
|
|
|
81
|
+ return []byte(secretKey), nil
|
|
|
82
|
+ })
|
|
|
83
|
+
|
|
|
84
|
+ if err != nil {
|
|
|
85
|
+ return nil, fmt.Errorf("parse token failed: %w", err)
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ if !token.Valid {
|
|
|
89
|
+ return nil, fmt.Errorf("invalid token")
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ return claims, nil
|
|
|
93
|
+}
|
|
|
94
|
+
|
|
|
95
|
+// ValidateToken 验证Token是否有效
|
|
|
96
|
+func ValidateToken(tokenString, secretKey string) bool {
|
|
|
97
|
+ _, err := ParseToken(tokenString, secretKey)
|
|
|
98
|
+ return err == nil
|
|
|
99
|
+}
|
|
|
100
|
+
|
|
|
101
|
+// RefreshToken 刷新Token
|
|
|
102
|
+func RefreshToken(tokenString string, expiresHours int, secretKey string) (string, error) {
|
|
|
103
|
+ // 解析原Token
|
|
|
104
|
+ claims, err := ParseToken(tokenString, secretKey)
|
|
|
105
|
+ if err != nil {
|
|
|
106
|
+ return "", err
|
|
|
107
|
+ }
|
|
|
108
|
+
|
|
|
109
|
+ // 重新创建Token
|
|
|
110
|
+ return CreateToken(claims, expiresHours, secretKey)
|
|
|
111
|
+}
|