2023-12-26 03:07:18 +00:00
|
|
|
package aid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-01-29 21:08:05 +00:00
|
|
|
"regexp"
|
2023-12-26 03:07:18 +00:00
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
)
|
|
|
|
|
2024-01-29 21:08:05 +00:00
|
|
|
var JWTCompile = regexp.MustCompile(`eg1~(.*)`)
|
|
|
|
|
2023-12-26 03:07:18 +00:00
|
|
|
func JWTSign(m JSON) (string, error) {
|
|
|
|
claims := jwt.MapClaims{}
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
claims[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
return token.SignedString([]byte(Config.JWT.Secret))
|
|
|
|
}
|
|
|
|
|
|
|
|
func JWTVerify(tokenString string) (JSON, error) {
|
2024-01-29 21:08:05 +00:00
|
|
|
compiled := JWTCompile.FindStringSubmatch(tokenString)
|
|
|
|
if len(compiled) > 0 {
|
|
|
|
tokenString = compiled[1]
|
|
|
|
}
|
|
|
|
|
2023-12-26 03:07:18 +00:00
|
|
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
return []byte(Config.JWT.Secret), nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !token.Valid {
|
|
|
|
return nil, fmt.Errorf("invalid token")
|
|
|
|
}
|
|
|
|
|
|
|
|
claims, ok := token.Claims.(jwt.MapClaims)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid claims")
|
|
|
|
}
|
|
|
|
|
|
|
|
json := JSON{}
|
|
|
|
for k, v := range claims {
|
|
|
|
json[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return json, nil
|
2024-01-29 21:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetSnowFromToken(token string) (string, error) {
|
|
|
|
claims, err := JWTVerify(token)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if claims["snow_id"] == nil {
|
|
|
|
return "", fmt.Errorf("invalid access token")
|
|
|
|
}
|
|
|
|
|
|
|
|
snowId, ok := claims["snow_id"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("invalid access token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return snowId, nil
|
2023-12-26 03:07:18 +00:00
|
|
|
}
|