utils

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package utils 中的上下文辅助函数已迁移至 context_support.go。

Deprecated: 请更新依赖以直接引用 context_support.go 中的实现。

  • Context工具函数支持
  • 提供从 echo.Context 提取链路追踪信息并构建标准 context.Context 的能力。

Index

Constants

View Source
const (
	DefaultBcryptCost = 12
	EnvPepperBase64   = "PASS_PEPPER_B64" // 环境变量名(可自定义)

)

Variables

This section is empty.

Functions

func BuildContext

func BuildContext(c echo.Context) context.Context

BuildContext 构造包含链路追踪信息的标准 context.Context

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID 从 context 中获取 RequestID

func GetStartTime

func GetStartTime(ctx context.Context) time.Time

GetStartTime 从 context 中获取请求开始时间

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID 从 context 中获取 TraceID

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID 从 context 中获取 UserID

func Hash

func Hash(plaintext string) (string, error)

Hash 生成密码哈希(BCrypt)。默认先做 SHA-256 预哈希,再执行 bcrypt。 返回形如 `$2b$12$...` 的哈希字符串。

func MustRehashIfNeeded

func MustRehashIfNeeded(storedHash, plaintext string) (newHash string, changed bool, err error)

MustRehashIfNeeded 在验证通过后调用;若 needRehash 为 true,立即重算并返回新哈希。 典型用法:登录成功时检测并平滑升级 cost。

func NeedsRehash

func NeedsRehash(storedHash string, currentCfg Config) (bool, error)

NeedsRehash 判断一个存量哈希是否需要按当前策略重算。 当前策略仅依据 Bcrypt cost 判断(因为是否预哈希无法从 bcrypt 字符串反推)。

func SetBcryptCost

func SetBcryptCost(cost int)

SetBcryptCost 仅调整 cost。

func SetConfig

func SetConfig(c Config)

SetConfig 全量设置配置(线程安全)。

func SetPepper

func SetPepper(p []byte)

SetPepper 设置 Pepper。

func SetPepperFromEnv

func SetPepperFromEnv() error

SetPepperFromEnv 从环境变量 PASS_PEPPER_B64 读取 Pepper(Base64 编码)。

func SetupTestValidator

func SetupTestValidator(e *echo.Echo)

SetupTestValidator 为测试环境设置验证器

func TokenSafeNow

func TokenSafeNow() int64

TokenSafeNow:给外部调用者做时间戳(审计时可用)。

func Verify

func Verify(storedHash, plaintext string) (ok bool, needRehash bool, err error)

Verify 校验明文是否匹配存量哈希。 返回:ok(是否匹配)、needRehash(是否建议用当前策略重算哈希)、err。

func WithTraceInfo

func WithTraceInfo(ctx context.Context, traceID, spanID, requestID, userID string) context.Context

WithTraceInfo 为 context 添加链路追踪信息

Types

type Config

type Config struct {
	// BcryptCost 建议 10–14;默认 12。根据你的机器压测调整。
	BcryptCost int

	// EnablePrehash: 将明文先做 SHA-256,再喂给 bcrypt,避免 bcrypt 72字节截断问题。
	EnablePrehash bool

	// Pepper:服务端额外机密(可选)。建议用 Base64 编码放在环境变量里再注入。
	// 注意:Pepper 与盐不同;盐由 bcrypt 内部自动处理,Pepper 是全局密钥。
	Pepper []byte
}

func GetConfig

func GetConfig() Config

GetConfig 拷贝一份当前配置。

type CustomValidator

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

CustomValidator 自定义验证器结构

func (*CustomValidator) GetValidator

func (cv *CustomValidator) GetValidator() *validator.Validate

GetValidator 获取验证器实例

func (*CustomValidator) Validate

func (cv *CustomValidator) Validate(i interface{}) error

Validate 实现Echo的Validator接口

type TraceContext

type TraceContext struct {
	TraceID   string
	SpanID    string
	RequestID string
	UserID    string
	StartTime time.Time
}

TraceContext 链路追踪上下文信息

func ExtractTraceContext

func ExtractTraceContext(c echo.Context) *TraceContext

ExtractTraceContext 从 echo.Context 中提取链路追踪信息

Jump to

Keyboard shortcuts

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