Files
backend_v2/internal/model/refresh_token.go
toom1996 74ba700598 update
2026-09-07 00:01:48 +08:00

27 lines
1.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import "time"
// RefreshToken 刷新令牌(服务端状态),用于无状态 JWT 的续期与吊销(踢下线)。
//
// refresh_token 本身是随机串,这里只存其 SHA256 哈希,原始串仅返回给客户端一次。
// revoked=1 表示已吊销(登出 / 全设备登出 / 改密)。
type RefreshToken struct {
ID uint64 `gorm:"primaryKey;column:id" json:"-"`
UserID uint32 `gorm:"column:user_id;index:idx_user_id" json:"-"`
TokenHash string `gorm:"column:token_hash;size:64;uniqueIndex:uk_token_hash" json:"-"`
ExpiresAt uint32 `gorm:"column:expires_at" json:"-"`
Revoked uint8 `gorm:"column:revoked" json:"-"`
CreatedAt uint32 `gorm:"column:created_at" json:"-"`
// 与 GORM 自动时间字段区分:CreatedAt 由业务层显式写入 Unix 秒。
}
// TableName 指定表名。
func (RefreshToken) TableName() string { return "refresh_tokens" }
// IsValid 未被吊销且未过期。
func (r *RefreshToken) IsValid() bool {
return r.Revoked == 0 && r.ExpiresAt >= uint32(time.Now().Unix())
}