You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.0 KiB
Go

package libs
import (
"fmt"
"mygo/conf"
jwt "github.com/dgrijalva/jwt-go"
)
func JWTDecrypt(tokenString string) (jwt.MapClaims, bool) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method")
}
secret := fmt.Sprintf("%s-%s", conf.APPNAME, conf.APPVERSION)
return []byte(secret), nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return claims, true
} else {
fmt.Println(err)
return nil, false
}
}
func JWTEncrypt(id string, mytoken string, key string) string {
mySigningKey := []byte(key)
type MyCustomClaims struct {
ID string `json:"id"`
Token string `json:"token"`
jwt.StandardClaims
}
// Create the Claims
claims := MyCustomClaims{
id,
mytoken,
jwt.StandardClaims{
ExpiresAt: 0,
Issuer: "admin",
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
jwtStr, _ := token.SignedString(mySigningKey)
return jwtStr
}