package utils

import (
	"github.com/golang-jwt/jwt/v5"
	"github.com/pkg/errors"
	"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
)

type MyClaims struct {
	models.JccUserInfo
	jwt.RegisteredClaims // 内嵌标准声明
}

// ParseTokenWithoutVerify 解析JWT
func ParseTokenWithoutVerify(tokenString string) (*models.JccUserInfo, error) {
	//忽略token里面的Bearer前缀
	if len(tokenString) > 7 && tokenString[:7] == "Bearer " {
		tokenString = tokenString[7:]
	} else {
		return nil, errors.New("token格式错误")
	}

	parser := jwt.NewParser()
	token, _, err := parser.ParseUnverified(tokenString, &MyClaims{})
	if err != nil {
		return nil, err
	}
	if claims, ok := token.Claims.(*MyClaims); ok {
		return &claims.JccUserInfo, nil
	}
	return nil, errors.New("token解析失败")
}
