/*
* @Description:
* @Version: 1.0
* @Autor: solid
* @Date: 2022-02-23 15:40:14
* @LastEditors: solid
* @LastEditTime: 2022-02-23 15:40:47
*/package utils
import("errors""vpnserver/config""github.com/golang-jwt/jwt/v4")//设置JWTfuncSetJwtData(data map[string]interface{}, expiresAt int64)(string,error){
jwtData := jwt.MapClaims{"exp": expiresAt}for k, v :=range data {
jwtData[k]= v
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwtData)// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString([]byte(config.Config.AdminConfig.JwtSecret))return tokenString, err
}//获取JWTfuncGetJwtData(jwtToken string)(map[string]interface{},error){
token, err := jwt.Parse(jwtToken,func(token *jwt.Token)(interface{},error){// since we only use the one private key to sign the tokens,// we also only use its public counter part to verifyreturn[]byte(config.Config.AdminConfig.JwtSecret),nil})if err !=nil||!token.Valid {returnnil, err
}
claims, ok := token.Claims.(jwt.MapClaims)if!ok {returnnil, errors.New("data is parse err")}return claims,nil}