DKIM Context Utilities (dkim/context.go)

chasquid · internal/dkim/context.go · go · Apache-2.0
email, security, lang:go, tag:context

Provides context-based dependency injection for DKIM verification, including tracing and DNS lookup overrides. Use this to mock or instrument email authentication logic during testing.

// Source: https://github.com/albertito/chasquid | License: Apache-2.0 | Copyright 2016
// Provided via justclaw.it.com; keep this notice with the code.
package dkim

import (
	"context"
	"net"
)

type contextKey string

const traceKey contextKey = "trace"

func trace(ctx context.Context, f string, args ...any) {
	traceFunc, ok := ctx.Value(traceKey).(TraceFunc)
	if !ok {
		return
	}
	traceFunc(f, args...)
}

type TraceFunc func(f string, a ...any)

func WithTraceFunc(ctx context.Context, trace TraceFunc) context.Context {
	return context.WithValue(ctx, traceKey, trace)
}

const lookupTXTKey contextKey = "lookupTXT"

func lookupTXT(ctx context.Context, domain string) ([]string, error) {
	lookupTXTFunc, ok := ctx.Value(lookupTXTKey).(lookupTXTFunc)
	if !ok {
		return net.LookupTXT(domain)
	

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key