utils

package
v4.2.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const LegacyTokenIssuer = "v2board"

LegacyTokenIssuer is the iss claim of the kernel's HS256 tokens.

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 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"`
	// SessionID (sid) identifies one login, so a single session can be
	// revoked. Tokens issued before it was added have none.
	SessionID string `json:"sid,omitempty"`
	// TokenVersion (tv) is set by the identity module's tokens; HS256 tokens
	// carry none, which counts as 0.
	TokenVersion uint64 `json:"tv,omitempty"`
	jwt.RegisteredClaims
}

func ParseTokenWithSecret

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

ParseTokenWithSecret verifies an HS256 token signed with secret. The algorithm is pinned, so a token signed with another HMAC variant or any other algorithm is rejected, and iss, exp and iat are required. Revocation is checked by internal/authn, which every request path uses.

type LogSafe

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

LogSafe 生成安全的日志字符串

func NewLogSafe

func NewLogSafe() *LogSafe

NewLogSafe 创建安全日志对象

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 格式

Jump to

Keyboard shortcuts

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