git-subtree-dir: server git-subtree-mainline:c4e617ada7git-subtree-split:e64421295f
35 lines
695 B
Go
35 lines
695 B
Go
package common
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
const jwtSecret = "slogan-agent-jwt-secret-2026"
|
|
|
|
type JwtClaims struct {
|
|
UserId int64 `json:"user_id"`
|
|
Role string `json:"role"`
|
|
AgentId int64 `json:"agent_id,omitempty"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func GetJwtSecret() string {
|
|
return jwtSecret
|
|
}
|
|
|
|
func ParseToken(tokenStr string) (*JwtClaims, error) {
|
|
token, err := jwt.ParseWithClaims(tokenStr, &JwtClaims{}, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(jwtSecret), nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
claims, ok := token.Claims.(*JwtClaims)
|
|
if !ok || !token.Valid {
|
|
return nil, errors.New("invalid token")
|
|
}
|
|
return claims, nil
|
|
}
|