mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 23:06:36 +08:00
refactor: extract magic numbers to named constants for mnd lint compliance
This commit is contained in:
@@ -15,6 +15,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Package model 定义数据模型与 GORM 实体
|
||||
package model
|
||||
|
||||
import (
|
||||
@@ -25,6 +26,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
tokenByteLength = 24 // Token 随机字节长度
|
||||
maskThreshold = 8 // 脱敏显示阈值
|
||||
)
|
||||
|
||||
// AccessToken 个人访问令牌实体
|
||||
type AccessToken struct {
|
||||
ID uint64 `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
UserID uint64 `json:"user_id" gorm:"index;not null"`
|
||||
@@ -38,7 +45,7 @@ type AccessToken struct {
|
||||
|
||||
// GenerateTokenString 生成加密安全的随机 Token 值
|
||||
func GenerateTokenString() (string, error) {
|
||||
bytes := make([]byte, 24)
|
||||
bytes := make([]byte, tokenByteLength)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -54,7 +61,7 @@ func HashToken(token string) string {
|
||||
|
||||
// MaskTokenString 生成脱敏显示的 Token,仅保留前缀和最后四位
|
||||
func MaskTokenString(token string) string {
|
||||
if len(token) <= 8 {
|
||||
if len(token) <= maskThreshold {
|
||||
return "at_****"
|
||||
}
|
||||
return fmt.Sprintf("%s...%s", token[:7], token[len(token)-4:])
|
||||
|
||||
Reference in New Issue
Block a user