From 0528a80f7bd57e274542ae198be75b6248ec024a Mon Sep 17 00:00:00 2001 From: Stanly Date: Mon, 29 Nov 2021 21:41:44 +0800 Subject: [PATCH] Add jwt EncodeRS256 --- jwt/jwt.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/jwt/jwt.go b/jwt/jwt.go index 90285f1..acf5f90 100644 --- a/jwt/jwt.go +++ b/jwt/jwt.go @@ -1,6 +1,7 @@ package jwt import ( + "crypto/rsa" "fmt" "git.simts.cc/common/lib/types" @@ -25,6 +26,23 @@ func Encode(values types.Data, key string) (string, error) { 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解碼 func Decode(tokenString string, key string) (types.Data, error) { token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {