utils

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package utils provides common utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildContext

func BuildContext(c echo.Context) context.Context

BuildContext creates a context.Context with trace information from echo.Context.

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID returns RequestID from context.

func GetSpanID

func GetSpanID(ctx context.Context) string

GetSpanID returns SpanID from context.

func GetStartTime

func GetStartTime(ctx context.Context) time.Time

GetStartTime returns start time from context.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID returns TraceID from context.

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID returns UserID from context.

func LoadPepperFromEnv

func LoadPepperFromEnv(envKey string) ([]byte, error)

LoadPepperFromEnv loads pepper from environment variable.

func WithTraceInfo

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

WithTraceInfo adds trace information to context.

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock provides time operations (for testability).

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.

func (*PasswordHasher) Verify

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

Verify checks if plaintext matches stored hash. Returns: ok (match), needRehash (should upgrade cost), err.

type RealClock

type RealClock struct{}

RealClock is the default Clock implementation.

func (RealClock) Now

func (RealClock) Now() time.Time

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.

Jump to

Keyboard shortcuts

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