Add jwt EncodeRS256

tags/v1.3.3 v1.3.3
Stanly 3 years ago
parent 9a3d7a87fd
commit 0528a80f7b

@ -1,6 +1,7 @@
package jwt package jwt
import ( import (
"crypto/rsa"
"fmt" "fmt"
"git.simts.cc/common/lib/types" "git.simts.cc/common/lib/types"
@ -25,6 +26,23 @@ func Encode(values types.Data, key string) (string, error) {
return tokenString, nil return tokenString, nil
} }
// EncodeRS256 jwt編碼
func EncodeRS256(values types.Data, key *rsa.PrivateKey) (string, error) {
claims := jwt.MapClaims{}
for key, value := range values {
claims[key] = value
}
tokenString, err := jwt.
NewWithClaims(jwt.SigningMethodRS256, claims).
SignedString(key)
if err != nil {
return "", err
}
return tokenString, nil
}
// Decode jwt解碼 // Decode jwt解碼
func Decode(tokenString string, key string) (types.Data, error) { func Decode(tokenString string, key string) (types.Data, error) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {

Loading…
Cancel
Save