|
|
|
@ -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) {
|
|
|
|
|