Skip to content

Commit 737b694

Browse files
committed
sail:jwt新增加解密方法
1 parent c70b1c7 commit 737b694

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

sail/jwt.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sail
33
import (
44
"github.com/keepchen/go-sail/v3/lib/jwt"
55
"github.com/keepchen/go-sail/v3/sail/config"
6+
"github.com/keepchen/go-sail/v3/utils"
67
)
78

89
// IJwt Jwt接口
@@ -16,7 +17,28 @@ type IJwt interface {
1617
// otherFields 其他可扩展字段
1718
MakeToken(uid string, exp int64, otherFields ...map[string]interface{}) (string, error)
1819
// ValidToken 验证JWT令牌
20+
//
21+
// # ⚠️注意⚠️
22+
//
23+
// 此方法继承 jwt.Validator 的 Validate 方法,因此只验证了存在且格式正确的字段,
24+
// 如果调用方有其他的验证规则,需要自行处理验证逻辑。
1925
ValidToken(token string) (bool, jwt.MapClaims, error)
26+
//Encrypt 加密字符
27+
//
28+
// 此方法可以与 Decrypt 方法对应进行加解密
29+
//
30+
// # Note
31+
//
32+
// 此方法调用 utils.RSA 方法并使用 jwt.Conf 配置中的公钥PublicKey进行加密
33+
Encrypt(plaintext string) (string, error)
34+
//Decrypt 解密字符
35+
//
36+
// 此方法可以与 Encrypt 方法对应进行加解密
37+
//
38+
// # Note
39+
//
40+
// 此方法调用 utils.RSA 方法并使用 jwt.Conf 配置中的私钥PrivateKey进行解密
41+
Decrypt(encryptedStr string) (string, error)
2042
}
2143

2244
type jwtImpl struct{}
@@ -68,3 +90,27 @@ func (jwtImpl) ValidToken(token string) (bool, jwt.MapClaims, error) {
6890

6991
return err == nil, mp, err
7092
}
93+
94+
// Encrypt 加密字符
95+
//
96+
// 此方法可以与 Decrypt 方法对应进行加解密
97+
//
98+
// # Note
99+
//
100+
// 此方法调用 utils.RSA 方法并使用 jwt.Conf 配置中的公钥PublicKey进行加密
101+
func (jwtImpl) Encrypt(plaintext string) (string, error) {
102+
conf := config.Get()
103+
return utils.RSA().Encrypt(plaintext, []byte(conf.JwtConf.PublicKey))
104+
}
105+
106+
// Decrypt 解密字符
107+
//
108+
// 此方法可以与 Encrypt 方法对应进行加解密
109+
//
110+
// # Note
111+
//
112+
// 此方法调用 utils.RSA 方法并使用 jwt.Conf 配置中的私钥PrivateKey进行解密
113+
func (jwtImpl) Decrypt(encryptedStr string) (string, error) {
114+
conf := config.Get()
115+
return utils.RSA().Decrypt(encryptedStr, []byte(conf.JwtConf.PrivateKey))
116+
}

sail/jwt_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,42 @@ func TestValidTokenExpired(t *testing.T) {
121121
t.Log(claims)
122122
})
123123
}
124+
125+
func TestEncrypt(t *testing.T) {
126+
t.Run("Encrypt", func(t *testing.T) {
127+
conf := &config.Config{
128+
JwtConf: &jwt.Conf{
129+
Enable: true,
130+
Algorithm: string(jwt.SigningMethodRS256),
131+
PublicKey: string(publicKey),
132+
PrivateKey: string(privateKey),
133+
},
134+
}
135+
conf.JwtConf.MustLoad()
136+
config.Set(conf)
137+
encryptedStr, err := JWT().Encrypt(uid)
138+
t.Log("Encrypt:", encryptedStr)
139+
assert.NoError(t, err)
140+
})
141+
}
142+
143+
func TestDecrypt(t *testing.T) {
144+
t.Run("Decrypt", func(t *testing.T) {
145+
conf := &config.Config{
146+
JwtConf: &jwt.Conf{
147+
Enable: true,
148+
Algorithm: string(jwt.SigningMethodRS256),
149+
PublicKey: string(publicKey),
150+
PrivateKey: string(privateKey),
151+
},
152+
}
153+
conf.JwtConf.MustLoad()
154+
config.Set(conf)
155+
encryptedStr, err := JWT().Encrypt(uid)
156+
t.Log("Encrypt:", encryptedStr)
157+
plaintext, err := JWT().Decrypt(encryptedStr)
158+
t.Log("Decrypt:", plaintext)
159+
assert.NoError(t, err)
160+
assert.Equal(t, plaintext, uid)
161+
})
162+
}

0 commit comments

Comments
 (0)