-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
60 lines (50 loc) · 1.22 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// @Author xiaozhaofu 2023/8/15 11:02:00
package zhipu
import (
"errors"
"strings"
"time"
"github.com/golang-jwt/jwt"
)
const (
DEFULTTIMES = 12
)
type ZpClaims struct {
APIKey string `json:"api_key"`
Exp int64 `json:"exp"`
Timestamp int64 `json:"timestamp"`
}
// GenerateToken 生成一个token.
func GenerateToken(apiKey string, duration time.Duration) (string, error) {
if apiKey == "" {
return "", errors.New("密钥不能为空")
}
if !strings.Contains(apiKey, ".") {
return "", errors.New("密钥格式不正确")
}
apiKeyInfo := strings.Split(apiKey, ".")
key, secret := apiKeyInfo[0], apiKeyInfo[1]
if duration == 0 {
duration = DEFULTTIMES * time.Hour
}
return createToken(ZpClaims{
key,
time.Now().Add(duration).Unix(),
time.Now().Unix(),
}, secret)
}
// createToken 生成一个token.
func createToken(claims ZpClaims, secret string) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"api_key": claims.APIKey,
"exp": claims.Exp,
"timestamp": claims.Timestamp,
})
token.Header["alg"] = "HS256"
token.Header["sign_type"] = "SIGN"
res, err := token.SignedString([]byte(secret))
if err != nil {
return "", err
}
return res, nil
}