package util import ( "fmt" "github.com/dgrijalva/jwt-go" "reflect" "time" "../../pkg/setting" ) var JwtSecret = []byte(setting.AppSetting.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(12 * time.Hour) claims := Claims{ username, EncodeMD5(password), jwt.StandardClaims{ ExpiresAt: expireTime.Unix(), Issuer: "https://github.com/kptyun/go-admin/", }, } tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) token, err := tokenClaims.SignedString(JwtSecret) 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 } func GetIdFromClaims(key string, claims jwt.Claims) string { v := reflect.ValueOf(claims) if v.Kind() == reflect.Map { for _, k := range v.MapKeys() { value := v.MapIndex(k) if fmt.Sprintf("%s", k.Interface()) == key { return fmt.Sprintf("%v", value.Interface()) } } } return "" }