package util import ( "fmt" "github.com/dgrijalva/jwt-go" "github.com/google/uuid" "github.com/viletyy/potato/pkg/setting" "time" ) var jwtSecret = []byte(setting.JwtSecret) type Claims struct { Username string `json:"username"` Password string `json:"password"` jwt.StandardClaims } func GenerateToken(username, password string) (string, error) { nowTime := time.Now() expireTime := nowTime.Add(time.Minute * 30) loginUUID := uuid.New().String() claims := Claims{ Username: username, Password: password, StandardClaims: jwt.StandardClaims{ ExpiresAt: expireTime.Unix(), Issuer : "data_govern", Id : loginUUID, }, } tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) token, err := tokenClaims.SignedString(jwtSecret) result, err := Redis.Set("login:" + loginUUID, username, 1*time.Hour).Result() fmt.Println(result, err) return token, err } func ParseToken(token string) (*Claims, error) { tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) { return jwtSecret, nil }) if tokenClaims != nil { if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid { return claims, nil } } return nil, err }