2026-08-28 15:06:01 +08:00
|
|
|
|
package tokenx
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-11-24 22:11:12 +08:00
|
|
|
|
"crypto/sha256"
|
|
|
|
|
|
"encoding/hex"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-11-24 22:11:12 +08:00
|
|
|
|
"bj_power_mes/common/clock"
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Claims JWT 自定义负载
|
|
|
|
|
|
type Claims struct {
|
2026-08-31 08:06:55 +08:00
|
|
|
|
UserId int `json:"userId"`
|
|
|
|
|
|
Username string `json:"username"`
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
RoleId int `json:"roleId"`
|
|
|
|
|
|
RoleCode string `json:"roleCode"`
|
|
|
|
|
|
RoleName string `json:"roleName"`
|
2026-11-11 14:36:16 +08:00
|
|
|
|
StationNo int `json:"stationNo,omitempty"` // 工位终端登录时的工位号
|
|
|
|
|
|
// Sid 后台登录会话标识(同账号互踢,2026-11-11):后台 Login 生成并写入 user.active_token;
|
|
|
|
|
|
// 滑动续签(renew.go)原样保留 Sid,只有新登录会更换——因此不能比对原始 token 串(每请求重签会误踢并发请求)。
|
|
|
|
|
|
Sid string `json:"sid,omitempty"`
|
2026-08-28 15:06:01 +08:00
|
|
|
|
jwt.RegisteredClaims
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Issue 签发 JWT
|
2026-11-24 22:11:12 +08:00
|
|
|
|
// 2026-11-24:统一写 Issuer=bj_power_mes,验签方据此取 MES 服务器时间验 exp(谁签发谁的时间为准)。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func Issue(secret string, expire time.Duration, c Claims) (string, error) {
|
|
|
|
|
|
c.IssuedAt = jwt.NewNumericDate(time.Now())
|
|
|
|
|
|
c.ExpiresAt = jwt.NewNumericDate(time.Now().Add(expire))
|
|
|
|
|
|
c.Subject = fmt.Sprintf("%d", c.UserId)
|
2026-11-24 22:11:12 +08:00
|
|
|
|
c.Issuer = "bj_power_mes"
|
2026-08-28 15:06:01 +08:00
|
|
|
|
t := jwt.NewWithClaims(jwt.SigningMethodHS256, c)
|
|
|
|
|
|
return t.SignedString([]byte(secret))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-24 22:11:12 +08:00
|
|
|
|
// Fingerprint 返回密钥的 SHA256 前 8 位十六进制指纹(2026-11-24 诊断用):
|
|
|
|
|
|
// 日志只打指纹不打密钥本体,现场把 MES 与工位终端两条 [鉴权诊断] 指纹一比对,
|
|
|
|
|
|
// 不一致即两端密钥不统一——工位终端验签必 401「登录已失效」,秒定位不再隔空猜。
|
|
|
|
|
|
func Fingerprint(secret string) string {
|
|
|
|
|
|
sum := sha256.Sum256([]byte(secret))
|
|
|
|
|
|
return hex.EncodeToString(sum[:4])
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-28 15:06:01 +08:00
|
|
|
|
// Parse 校验 JWT 并返回负载
|
2026-11-24 22:11:12 +08:00
|
|
|
|
// 2026-11-24 拍板:谁签发 JWT 就以谁的时间验 exp——本端签发的用本机时间,外部签发的
|
|
|
|
|
|
// 先向签发方取 /api/server-time(30 秒缓存,取不到降级本机)。10 分钟容差继续兜底。
|
2026-08-28 15:06:01 +08:00
|
|
|
|
func Parse(secret, tokenStr string) (*Claims, error) {
|
2026-11-24 22:11:12 +08:00
|
|
|
|
parser := jwt.NewParser(jwt.WithoutClaimsValidation())
|
|
|
|
|
|
token, err := parser.ParseWithClaims(tokenStr, &Claims{}, func(t *jwt.Token) (any, error) {
|
2026-08-28 15:06:01 +08:00
|
|
|
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
|
|
|
|
return nil, errors.New("unexpected signing method")
|
|
|
|
|
|
}
|
|
|
|
|
|
return []byte(secret), nil
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2026-11-24 22:11:12 +08:00
|
|
|
|
claims, ok := token.Claims.(*Claims)
|
|
|
|
|
|
if !ok || !token.Valid {
|
|
|
|
|
|
return nil, errors.New("invalid token")
|
|
|
|
|
|
}
|
|
|
|
|
|
// 按签发方服务器时间验过期,再叠加 10 分钟容差兜底
|
|
|
|
|
|
authority := clock.AuthorityNow(claims.Issuer)
|
|
|
|
|
|
if !claims.VerifyExpiresAt(authority.Add(10*time.Minute), true) {
|
|
|
|
|
|
return nil, errors.New("token 已过期")
|
2026-08-28 15:06:01 +08:00
|
|
|
|
}
|
2026-11-24 22:11:12 +08:00
|
|
|
|
return claims, nil
|
2026-10-06 19:36:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|