@@ -3,6 +3,7 @@ package sail
33import (
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
2244type 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+ }
0 commit comments