-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.go
50 lines (41 loc) · 1.22 KB
/
jwt.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
package main
import (
"crypto/rsa"
"time"
"github.com/golang-jwt/jwt/v5"
)
// 依赖注入
type JWT struct {
privateKey *rsa.PrivateKey
issuer string
// nowFunc is used to mock time in tests
nowFunc func() time.Time
}
func NewJWT(issuer string, privateKey *rsa.PrivateKey) *JWT {
return &JWT{
privateKey: privateKey,
issuer: issuer,
nowFunc: time.Now,
}
}
func (j *JWT) GenerateToken(userId string, expire time.Duration) (string, error) {
nowSec := j.nowFunc().Unix()
token := jwt.NewWithClaims(jwt.SigningMethodRS512, jwt.MapClaims{
// map 会对其进行重新排序,排序结果影响签名结果,签名结果验证网址:https://jwt.io/
"issuer": j.issuer,
"issuedAt": nowSec,
"expiresAt": nowSec + int64(expire.Seconds()),
"subject": userId,
})
return token.SignedString(j.privateKey)
}
func GenerateJWT(issuer string, userId string, nowFunc func() time.Time, expire time.Duration, privateKey *rsa.PrivateKey) (string, error) {
nowSec := nowFunc().Unix()
token := jwt.NewWithClaims(jwt.SigningMethodRS512, jwt.MapClaims{
"expiresAt": nowSec + int64(expire.Seconds()),
"issuedAt": nowSec,
"issuer": issuer,
"subject": userId,
})
return token.SignedString(privateKey)
}