utils

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 10 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 request information from echo.Context.

This function:

  • Preserves the original context (including OpenTelemetry span context)
  • Adds business-related values (RequestID, UserID, StartTime) to context
  • Does NOT inject TraceID/SpanID as they are already available via OpenTelemetry

Usage:

func handler(c echo.Context) error {
    ctx := utils.BuildContext(c)
    requestID := utils.GetRequestID(ctx)
    traceID := utils.GetTraceID(ctx) // From OpenTelemetry
    // ...
}

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID returns RequestID from context. The RequestID is set by BuildContext() which extracts it from Echo middleware. Returns empty string if not found.

func GetRequestIDFromEcho added in v0.0.3

func GetRequestIDFromEcho(c echo.Context) string

GetRequestIDFromEcho is a convenience function to get RequestID directly from echo.Context. This is useful when you don't have a context.Context but have echo.Context.

func GetSpanID

func GetSpanID(ctx context.Context) string

GetSpanID returns SpanID from context. It extracts from OpenTelemetry span context if available.

func GetStartTime

func GetStartTime(ctx context.Context) time.Time

GetStartTime returns start time from context. The StartTime should be set by BuildContext().

Returns current time if not found in context.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID returns TraceID from context. It extracts from OpenTelemetry span context if available.

Returns empty string if no valid span context is found.

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID returns UserID from context. The UserID should be set by BuildContext() which extracts it from echo.Context.

Returns empty string if not found in context.

func LoadPepperFromEnv

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

LoadPepperFromEnv loads pepper from environment variable.

func WithRequestInfo added in v0.0.3

func WithRequestInfo(ctx context.Context, requestID, userID string) context.Context

WithRequestInfo adds request information to context. This is useful for testing or when you need to manually set request context.

Note: TraceID and SpanID should be managed by OpenTelemetry, not manually set.

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 (
	// KeyRequestID is the context key for request ID.
	KeyRequestID ContextKey = "request_id"
	// KeyUserID is the context key for user ID.
	KeyUserID ContextKey = "user_id"
	// KeyStartTime is the context key for request start time.
	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    // TraceID from OpenTelemetry span context
	SpanID    string    // SpanID from OpenTelemetry span context
	RequestID string    // RequestID from Echo middleware (middleware.RequestID)
	UserID    string    // UserID from authenticated user (if available)
	StartTime time.Time // Request start time
}

TraceContext contains trace and request information from a request. TraceID and SpanID are extracted from OpenTelemetry context, RequestID is extracted from Echo middleware.

func ExtractTraceContext

func ExtractTraceContext(c echo.Context) *TraceContext

ExtractTraceContext extracts trace and request information from echo.Context.

This function extracts:

  • TraceID and SpanID from OpenTelemetry span context (requires otelecho middleware)
  • RequestID from Echo middleware (requires middleware.RequestID)
  • UserID from echo.Context if authenticated
  • StartTime as current time

Jump to

Keyboard shortcuts

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