-
Notifications
You must be signed in to change notification settings - Fork 3
/
sign.go
47 lines (41 loc) · 1.36 KB
/
sign.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
package jwtKit
import (
"github.com/golang-jwt/jwt/v5"
"github.com/richelieu-yang/chimera/v3/src/core/interfaceKit"
"github.com/richelieu-yang/chimera/v3/src/core/mapKit"
"github.com/richelieu-yang/chimera/v3/src/crypto/caesarKit"
)
// Sign 生成JWT字符串.
/*
@param key 密钥(secret)
@param method e.g. jwt.SigningMethodHS256 || jwt.SigningMethodHS384 || jwt.SigningMethodHS512
*/
func Sign(key []byte, method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (string, error) {
if err := interfaceKit.AssertNotNil(key, "key"); err != nil {
return "", err
}
if err := interfaceKit.AssertNotNil(method, "method"); err != nil {
return "", err
}
if err := mapKit.AssertNotEmpty(claims, "claims"); err != nil {
return "", err
}
/*
Create a new token object, specifying signing method and the claims
you would like it to contain.
*/
token := jwt.NewWithClaims(method, claims, options...)
/*
Sign and get the complete encoded token as a string using the secret
*/
return token.SignedString(key)
}
func SignWithCaesar(caesarShift int, key []byte, method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (cipherText string, err error) {
var tokenString string
tokenString, err = Sign(key, method, claims, options...)
if err != nil {
return
}
cipherText = caesarKit.Encrypt(tokenString, caesarShift)
return
}