Documentation
¶
Overview ¶
Package utils provides common utility functions.
Index ¶
- func BuildContext(c echo.Context) context.Context
- func GetRequestID(ctx context.Context) string
- func GetSpanID(ctx context.Context) string
- func GetStartTime(ctx context.Context) time.Time
- func GetTraceID(ctx context.Context) string
- func GetUserID(ctx context.Context) string
- func LoadPepperFromEnv(envKey string) ([]byte, error)
- func WithTraceInfo(ctx context.Context, traceID, spanID, requestID, userID string) context.Context
- type Clock
- type ContextKey
- type EncryptConfig
- type PasswordHasher
- func (h *PasswordHasher) Hash(plaintext string) (string, error)
- func (h *PasswordHasher) NeedsRehash(storedHash string) (bool, error)
- func (h *PasswordHasher) RehashIfNeeded(storedHash, plaintext string) (newHash string, changed bool, err error)
- func (h *PasswordHasher) TokenSafeNow() int64
- func (h *PasswordHasher) Verify(storedHash, plaintext string) (ok bool, needRehash bool, err error)
- type RealClock
- type TraceContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildContext ¶
BuildContext creates a context.Context with trace information from echo.Context.
func GetRequestID ¶
GetRequestID returns RequestID from context.
func GetStartTime ¶
GetStartTime returns start time from context.
func GetTraceID ¶
GetTraceID returns TraceID from context.
func LoadPepperFromEnv ¶
LoadPepperFromEnv loads pepper from environment variable.
Types ¶
type ContextKey ¶
type ContextKey string
ContextKey is the type for context keys (prevents collisions).
const ( KeyTraceID ContextKey = "trace_id" KeySpanID ContextKey = "span_id" KeyRequestID ContextKey = "request_id" KeyUserID ContextKey = "user_id" KeyStartTime ContextKey = "start_time" )
type EncryptConfig ¶
type EncryptConfig struct {
// BcryptCost: recommended 10-14; default 12.
BcryptCost int
// EnablePrehash: SHA-256 the password before bcrypt to avoid 72-byte truncation.
EnablePrehash bool
// Pepper: server-side secret (optional). Use Base64 encoded environment variable.
Pepper []byte
}
EncryptConfig holds password hashing configuration.
func DefaultEncryptConfig ¶
func DefaultEncryptConfig() EncryptConfig
DefaultEncryptConfig returns the default encryption configuration.
type PasswordHasher ¶
type PasswordHasher struct {
// contains filtered or unexported fields
}
PasswordHasher handles password hashing with dependency injection.
func NewPasswordHasher ¶
func NewPasswordHasher(config EncryptConfig) *PasswordHasher
NewPasswordHasher creates a new password hasher.
func NewPasswordHasherWithClock ¶
func NewPasswordHasherWithClock(config EncryptConfig, clock Clock) *PasswordHasher
NewPasswordHasherWithClock creates a new password hasher with custom clock.
func (*PasswordHasher) Hash ¶
func (h *PasswordHasher) Hash(plaintext string) (string, error)
Hash generates a bcrypt password hash.
func (*PasswordHasher) NeedsRehash ¶
func (h *PasswordHasher) NeedsRehash(storedHash string) (bool, error)
NeedsRehash checks if a stored hash needs to be re-hashed with current config.
func (*PasswordHasher) RehashIfNeeded ¶
func (h *PasswordHasher) RehashIfNeeded(storedHash, plaintext string) (newHash string, changed bool, err error)
RehashIfNeeded verifies password and re-hashes if needed.
func (*PasswordHasher) TokenSafeNow ¶
func (h *PasswordHasher) TokenSafeNow() int64
TokenSafeNow returns current unix timestamp using injected clock.
type TraceContext ¶
type TraceContext struct {
TraceID string
SpanID string
RequestID string
UserID string
StartTime time.Time
}
TraceContext contains trace information from a request.
func ExtractTraceContext ¶
func ExtractTraceContext(c echo.Context) *TraceContext
ExtractTraceContext extracts trace information from echo.Context.