utils

package
v4.0.0-alpha.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SensitiveKeys = []string{
	"password",
	"secret",
	"api_key",
	"apikey",
	"api-key",
	"token",
	"access_token",
	"refresh_token",
	"auth_key",
	"private_key",
	"credential",
}

SensitiveKeys 敏感字段名列表

Functions

func CheckWebSocketOrigin

func CheckWebSocketOrigin(r *http.Request) bool

CheckWebSocketOrigin applies the server CORS origin allowlist to WebSocket upgrades while still allowing same-host browser upgrades and non-browser clients that do not send an Origin header.

func GenerateToken

func GenerateToken(userID uint, email string, isAdmin bool, secret string, expireSeconds int) (string, error)

GenerateToken 生成JWT token

func IsWebSocketOriginAllowed

func IsWebSocketOriginAllowed(r *http.Request, allowedOrigins []string) bool

IsWebSocketOriginAllowed validates a WebSocket Origin header against the request host and an explicit origin allowlist.

func Redact

func Redact(s string) string

Redact 脱敏字符串 规则:保留前4位和后4位,中间用 **** 替代 如果长度不足8位,全部用 **** 替代

func RedactEmail

func RedactEmail(email string) string

RedactEmail 脱敏邮箱 规则:保留 @ 前2位和 @ 后完整域名 例如:admin@example.com -> ad***@example.com

func RedactIP

func RedactIP(ip string) string

RedactIP 脱敏 IP 地址 IPv4: 192.168.1.100 -> 192.168.*.* IPv6: 保留前两段

func RedactJSON

func RedactJSON(jsonStr string) string

RedactJSON 脱敏 JSON 字符串中的敏感字段

func RedactMap

func RedactMap(data map[string]any) map[string]any

RedactMap 脱敏 Map 中的敏感字段

Types

type Claims

type Claims struct {
	UserID  uint   `json:"user_id"`
	Email   string `json:"email"`
	IsAdmin bool   `json:"is_admin"`
	jwt.RegisteredClaims
}

func ParseToken

func ParseToken(tokenString string) (*Claims, error)

ParseToken 解析JWT token (自动从配置获取secret)

func ParseTokenWithSecret

func ParseTokenWithSecret(tokenString string, secret string) (*Claims, error)

ParseTokenWithSecret 使用指定secret解析JWT token

type LogSafe

type LogSafe struct {
	// contains filtered or unexported fields
}

LogSafe 生成安全的日志字符串

func NewLogSafe

func NewLogSafe() *LogSafe

NewLogSafe 创建安全日志对象

func (*LogSafe) Fields

func (l *LogSafe) Fields() map[string]any

Fields 获取所有字段

func (*LogSafe) Set

func (l *LogSafe) Set(key string, value any) *LogSafe

Set 设置字段(自动判断是否脱敏)

func (*LogSafe) SetEmail

func (l *LogSafe) SetEmail(key, email string) *LogSafe

SetEmail 设置邮箱字段(脱敏)

func (*LogSafe) SetIP

func (l *LogSafe) SetIP(key, ip string) *LogSafe

SetIP 设置 IP 字段(脱敏)

func (*LogSafe) SetRaw

func (l *LogSafe) SetRaw(key string, value any) *LogSafe

SetRaw 设置字段(不脱敏,用于非敏感字段)

func (*LogSafe) String

func (l *LogSafe) String() string

String 输出 JSON 格式

type SensitiveString

type SensitiveString string

SensitiveString 敏感字符串类型,打印时自动脱敏

func (SensitiveString) MarshalJSON

func (s SensitiveString) MarshalJSON() ([]byte, error)

MarshalJSON 实现 JSON 序列化,返回脱敏值

func (SensitiveString) Raw

func (s SensitiveString) Raw() string

Raw 获取原始值(慎用)

func (SensitiveString) String

func (s SensitiveString) String() string

String 实现 Stringer 接口,自动脱敏

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL